Skip to content

Commit 47cf67b

Browse files
committed
Refactor function parameter and details structures for better type safety
1 parent 283cf4a commit 47cf67b

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

web/src/utils/functions.ts

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,84 @@ import type { FunctionType } from './types';
55

66
type FunctionItem = Awaited<ReturnType<typeof getCollection>>[number];
77

8+
// Define a structure for function parameters
9+
type FunctionParameter = {
10+
name: string;
11+
type: string; // Adjust type as needed (e.g., string | string[])
12+
description?: string;
13+
optional?: boolean;
14+
};
15+
16+
// Define a structure for the details expected within shared/client/server
17+
type FunctionDetails = {
18+
description?: string;
19+
pair?: boolean;
20+
examples?: { code: string; description?: string }[];
21+
notes?: string;
22+
parameters?: FunctionParameter[];
23+
};
24+
825
type FunctionsByCategory = {
926
[folder: string]: FunctionItem[];
1027
};
1128
type FunctionsByTypeByCategory = {
12-
shared: FunctionsByCategory;
13-
client: FunctionsByCategory;
14-
server: FunctionsByCategory;
29+
[key in FunctionType]: FunctionsByCategory;
1530
};
1631

32+
1733
export type FunctionData = {
1834
shared?: any;
1935
client?: any;
2036
server?: any;
2137
};
2238

39+
// Use the specific FunctionDetails type
40+
export type TypedFunctionData = {
41+
shared?: FunctionDetails;
42+
client?: FunctionDetails;
43+
server?: FunctionDetails;
44+
};
45+
2346
export const functionTypePrettyName = {
2447
'client': 'Client-side',
2548
'server': 'Server-side',
2649
'shared': 'Shared',
27-
};
50+
} as const; // Use 'as const' for stricter typing of keys
2851

2952
function getFunctionType(data: FunctionData): FunctionType {
3053
if (data.shared) return 'shared';
3154
if (data.client) return 'client';
3255
return 'server';
3356
}
3457
function getFunctionTypePretty(data: FunctionData): string {
58+
// No need for fallback, getFunctionType guarantees a valid FunctionType
3559
const funcType = getFunctionType(data);
36-
return functionTypePrettyName[funcType] ?? 'Server-side';
60+
return functionTypePrettyName[funcType];
3761
}
3862

39-
export function getFunctionInfo(data: FunctionData): any {
63+
// Define a return type for getFunctionInfo
64+
export type FunctionInfo = {
65+
description: string;
66+
type: FunctionType;
67+
typePretty: string;
68+
pair: boolean;
69+
examples: { code: string; description?: string }[];
70+
notes?: string; // Added notes
71+
parameters?: FunctionParameter[]; // Added parameters
72+
};
73+
74+
export function getFunctionInfo(data: TypedFunctionData): FunctionInfo {
75+
const type = getFunctionType(data);
76+
const details = data[type] ?? {}; // Get details based on type, default to empty object
77+
4078
return {
41-
description: data.shared?.description || data.client?.description || data.server?.description || '',
42-
type: getFunctionType(data),
79+
description: details.description || '',
80+
type: type,
4381
typePretty: getFunctionTypePretty(data),
44-
pair: data.shared?.pair || data.client?.pair || data.server?.pair || false,
45-
examples: data.shared?.examples || data.client?.examples || data.server?.examples || [ ],
82+
pair: details.pair || false,
83+
examples: details.examples || [],
84+
notes: details.notes, // Extract notes (will be undefined if not present)
85+
parameters: details.parameters || [], // Extract parameters
4686
};
4787
}
4888

@@ -55,15 +95,17 @@ let functionsByTypeByCategory: FunctionsByTypeByCategory = {
5595
};
5696

5797
for (let func of functionsCollection) {
58-
const normalizedPath = path.normalize(func.filePath || '');
98+
// Assuming func.filePath exists, handle potential undefined if necessary
99+
const normalizedPath = path.normalize(func.id); // Use func.id which includes the path relative to content dir
59100
const folder = path.basename(path.dirname(normalizedPath));
60101
if (!functionsByCategory[folder]) {
61102
functionsByCategory[folder] = [];
62103
}
63104
functionsByCategory[folder].push(func);
64105

65106
const funcType = getFunctionType(func.data);
66-
if (!functionsByTypeByCategory[funcType][folder]) {
107+
// Ensure the folder exists for the specific type
108+
if (!functionsByTypeByCategory[funcType]?.[folder]) {
67109
functionsByTypeByCategory[funcType][folder] = [];
68110
}
69111
functionsByTypeByCategory[funcType][folder].push(func);

0 commit comments

Comments
 (0)