@@ -5,44 +5,84 @@ import type { FunctionType } from './types';
5
5
6
6
type FunctionItem = Awaited < ReturnType < typeof getCollection > > [ number ] ;
7
7
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
+
8
25
type FunctionsByCategory = {
9
26
[ folder : string ] : FunctionItem [ ] ;
10
27
} ;
11
28
type FunctionsByTypeByCategory = {
12
- shared : FunctionsByCategory ;
13
- client : FunctionsByCategory ;
14
- server : FunctionsByCategory ;
29
+ [ key in FunctionType ] : FunctionsByCategory ;
15
30
} ;
16
31
32
+
17
33
export type FunctionData = {
18
34
shared ?: any ;
19
35
client ?: any ;
20
36
server ?: any ;
21
37
} ;
22
38
39
+ // Use the specific FunctionDetails type
40
+ export type TypedFunctionData = {
41
+ shared ?: FunctionDetails ;
42
+ client ?: FunctionDetails ;
43
+ server ?: FunctionDetails ;
44
+ } ;
45
+
23
46
export const functionTypePrettyName = {
24
47
'client' : 'Client-side' ,
25
48
'server' : 'Server-side' ,
26
49
'shared' : 'Shared' ,
27
- } ;
50
+ } as const ; // Use 'as const' for stricter typing of keys
28
51
29
52
function getFunctionType ( data : FunctionData ) : FunctionType {
30
53
if ( data . shared ) return 'shared' ;
31
54
if ( data . client ) return 'client' ;
32
55
return 'server' ;
33
56
}
34
57
function getFunctionTypePretty ( data : FunctionData ) : string {
58
+ // No need for fallback, getFunctionType guarantees a valid FunctionType
35
59
const funcType = getFunctionType ( data ) ;
36
- return functionTypePrettyName [ funcType ] ?? 'Server-side' ;
60
+ return functionTypePrettyName [ funcType ] ;
37
61
}
38
62
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
+
40
78
return {
41
- description : data . shared ?. description || data . client ?. description || data . server ? .description || '' ,
42
- type : getFunctionType ( data ) ,
79
+ description : details . description || '' ,
80
+ type : type ,
43
81
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
46
86
} ;
47
87
}
48
88
@@ -55,15 +95,17 @@ let functionsByTypeByCategory: FunctionsByTypeByCategory = {
55
95
} ;
56
96
57
97
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
59
100
const folder = path . basename ( path . dirname ( normalizedPath ) ) ;
60
101
if ( ! functionsByCategory [ folder ] ) {
61
102
functionsByCategory [ folder ] = [ ] ;
62
103
}
63
104
functionsByCategory [ folder ] . push ( func ) ;
64
105
65
106
const funcType = getFunctionType ( func . data ) ;
66
- if ( ! functionsByTypeByCategory [ funcType ] [ folder ] ) {
107
+ // Ensure the folder exists for the specific type
108
+ if ( ! functionsByTypeByCategory [ funcType ] ?. [ folder ] ) {
67
109
functionsByTypeByCategory [ funcType ] [ folder ] = [ ] ;
68
110
}
69
111
functionsByTypeByCategory [ funcType ] [ folder ] . push ( func ) ;
0 commit comments