11import { getCollection } from 'astro:content' ;
22import path from 'path' ;
33
4- import type { FunctionType } from './types' ;
4+ import type { FunctionType , NotesType } from './types' ;
55
66type FunctionItem = Awaited < ReturnType < typeof getCollection > > [ number ] ;
77
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+
822type FunctionsByCategory = {
923 [ folder : string ] : FunctionItem [ ] ;
1024} ;
1125type FunctionsByTypeByCategory = {
12- shared : FunctionsByCategory ;
13- client : FunctionsByCategory ;
14- server : FunctionsByCategory ;
26+ [ key in FunctionType ] : FunctionsByCategory ;
1527} ;
1628
29+
1730export type FunctionData = {
1831 shared ?: any ;
1932 client ?: any ;
2033 server ?: any ;
2134} ;
2235
36+ export type TypedFunctionData = {
37+ shared ?: FunctionDetails ;
38+ client ?: FunctionDetails ;
39+ server ?: FunctionDetails ;
40+ } ;
41+
2342export const functionTypePrettyName = {
2443 'client' : 'Client-side' ,
2544 'server' : 'Server-side' ,
2645 'shared' : 'Shared' ,
27- } ;
46+ } as const ;
2847
2948function getFunctionType ( data : FunctionData ) : FunctionType {
3049 if ( data . shared ) return 'shared' ;
@@ -33,16 +52,31 @@ function getFunctionType(data: FunctionData): FunctionType {
3352}
3453function getFunctionTypePretty ( data : FunctionData ) : string {
3554 const funcType = getFunctionType ( data ) ;
36- return functionTypePrettyName [ funcType ] ?? 'Server-side' ;
55+ return functionTypePrettyName [ funcType ] ;
3756}
3857
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+
4072 return {
41- description : data . shared ?. description || data . client ?. description || data . server ? .description || '' ,
42- type : getFunctionType ( data ) ,
73+ description : details . description || '' ,
74+ type : type ,
4375 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 || [ ] ,
4680 } ;
4781}
4882
@@ -55,15 +89,15 @@ let functionsByTypeByCategory: FunctionsByTypeByCategory = {
5589} ;
5690
5791for ( let func of functionsCollection ) {
58- const normalizedPath = path . normalize ( func . filePath || '' ) ;
92+ const normalizedPath = path . normalize ( func . id ) ;
5993 const folder = path . basename ( path . dirname ( normalizedPath ) ) ;
6094 if ( ! functionsByCategory [ folder ] ) {
6195 functionsByCategory [ folder ] = [ ] ;
6296 }
6397 functionsByCategory [ folder ] . push ( func ) ;
6498
6599 const funcType = getFunctionType ( func . data ) ;
66- if ( ! functionsByTypeByCategory [ funcType ] [ folder ] ) {
100+ if ( ! functionsByTypeByCategory [ funcType ] ?. [ folder ] ) {
67101 functionsByTypeByCategory [ funcType ] [ folder ] = [ ] ;
68102 }
69103 functionsByTypeByCategory [ funcType ] [ folder ] . push ( func ) ;
0 commit comments