1
1
import { getCollection } from 'astro:content' ;
2
2
import path from 'path' ;
3
3
4
- import type { FunctionType } from './types' ;
4
+ import type { FunctionType , NotesType } from './types' ;
5
5
6
6
type FunctionItem = Awaited < ReturnType < typeof getCollection > > [ number ] ;
7
7
8
+ type FunctionParameter = {
9
+ name : string ;
10
+ type : string ;
11
+ description ?: string ;
12
+ } ;
13
+
14
+ type FunctionDetails = {
15
+ description ?: string ;
16
+ pair ?: boolean ;
17
+ examples ?: { code : string ; description ?: string } [ ] ;
18
+ notes ?: NotesType ;
19
+ parameters ?: FunctionParameter [ ] ;
20
+ } ;
21
+
8
22
type FunctionsByCategory = {
9
23
[ folder : string ] : FunctionItem [ ] ;
10
24
} ;
11
25
type FunctionsByTypeByCategory = {
12
- shared : FunctionsByCategory ;
13
- client : FunctionsByCategory ;
14
- server : FunctionsByCategory ;
26
+ [ key in FunctionType ] : FunctionsByCategory ;
15
27
} ;
16
28
29
+
17
30
export type FunctionData = {
18
31
shared ?: any ;
19
32
client ?: any ;
20
33
server ?: any ;
21
34
} ;
22
35
36
+ export type TypedFunctionData = {
37
+ shared ?: FunctionDetails ;
38
+ client ?: FunctionDetails ;
39
+ server ?: FunctionDetails ;
40
+ } ;
41
+
23
42
export const functionTypePrettyName = {
24
43
'client' : 'Client-side' ,
25
44
'server' : 'Server-side' ,
26
45
'shared' : 'Shared' ,
27
- } ;
46
+ } as const ;
28
47
29
48
function getFunctionType ( data : FunctionData ) : FunctionType {
30
49
if ( data . shared ) return 'shared' ;
@@ -33,16 +52,31 @@ function getFunctionType(data: FunctionData): FunctionType {
33
52
}
34
53
function getFunctionTypePretty ( data : FunctionData ) : string {
35
54
const funcType = getFunctionType ( data ) ;
36
- return functionTypePrettyName [ funcType ] ?? 'Server-side' ;
55
+ return functionTypePrettyName [ funcType ] ;
37
56
}
38
57
39
- export function getFunctionInfo ( data : FunctionData ) : any {
58
+ export type FunctionInfo = {
59
+ description : string ;
60
+ type : FunctionType ;
61
+ typePretty : string ;
62
+ pair : boolean ;
63
+ examples : { code : string ; description ?: string } [ ] ;
64
+ notes ?: NotesType ;
65
+ parameters ?: FunctionParameter [ ] ;
66
+ } ;
67
+
68
+ export function getFunctionInfo ( data : TypedFunctionData ) : FunctionInfo {
69
+ const type = getFunctionType ( data ) ;
70
+ const details = data [ type ] ?? { } ;
71
+
40
72
return {
41
- description : data . shared ?. description || data . client ?. description || data . server ? .description || '' ,
42
- type : getFunctionType ( data ) ,
73
+ description : details . description || '' ,
74
+ type : type ,
43
75
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 || [ ] ,
76
+ pair : details . pair || false ,
77
+ examples : details . examples || [ ] ,
78
+ notes : details . notes || [ ] ,
79
+ parameters : details . parameters || [ ] ,
46
80
} ;
47
81
}
48
82
@@ -55,15 +89,15 @@ let functionsByTypeByCategory: FunctionsByTypeByCategory = {
55
89
} ;
56
90
57
91
for ( let func of functionsCollection ) {
58
- const normalizedPath = path . normalize ( func . filePath || '' ) ;
92
+ const normalizedPath = path . normalize ( func . id ) ;
59
93
const folder = path . basename ( path . dirname ( normalizedPath ) ) ;
60
94
if ( ! functionsByCategory [ folder ] ) {
61
95
functionsByCategory [ folder ] = [ ] ;
62
96
}
63
97
functionsByCategory [ folder ] . push ( func ) ;
64
98
65
99
const funcType = getFunctionType ( func . data ) ;
66
- if ( ! functionsByTypeByCategory [ funcType ] [ folder ] ) {
100
+ if ( ! functionsByTypeByCategory [ funcType ] ?. [ folder ] ) {
67
101
functionsByTypeByCategory [ funcType ] [ folder ] = [ ] ;
68
102
}
69
103
functionsByTypeByCategory [ funcType ] [ folder ] . push ( func ) ;
0 commit comments