@@ -19,10 +19,14 @@ export const serviceActions = createActions(
19
19
) as BlobType
20
20
21
21
for ( const [ key , value ] of Object . entries ( serviceOptions . actions ?? { } ) ) {
22
- if ( value . rest ) {
23
- aliases [
24
- typeof value . rest === 'boolean' ? `${ toMethod ( key ) } /` : ( value . rest as string )
25
- ] = async ( req : IncomingRequest , res : GatewayResponse ) => {
22
+ if ( value . meta . rest ) {
23
+ // Create alias for redirecting
24
+ const alias =
25
+ typeof value . meta . rest === 'boolean'
26
+ ? `${ toMethod ( key ) } /`
27
+ : ( value . meta . rest as string )
28
+
29
+ aliases [ alias ] = async ( req : IncomingRequest , res : GatewayResponse ) => {
26
30
const result = await group ( ( ) =>
27
31
// biome-ignore lint/style/noNonNullAssertion: Redundant
28
32
serviceOptions . actions ! [ key ] ?. handler ! ( req . $ctx )
@@ -42,6 +46,70 @@ export const serviceActions = createActions(
42
46
res . end ( result . error . stringify ( ) )
43
47
}
44
48
}
49
+
50
+ // Extract method and path from alias
51
+ const [ method , path ] = alias . split ( ' ' ) as [ string , string ]
52
+
53
+ // Format param variables
54
+ const formattedPath = path
55
+ . substring ( 1 )
56
+ . split ( '/' )
57
+ . map ( val => ( val . startsWith ( ':' ) ? `\{${ val . substring ( 1 ) } \}` : val ) )
58
+ . join ( '/' )
59
+
60
+ // Get the paths object of swagger
61
+ const paths = context . api . gateway . settings . swagger . paths
62
+
63
+ // Original path
64
+ const oriPath = `/${ serviceOptions . name } \/${ serviceOptions . version } \/${ formattedPath } `
65
+
66
+ // Omit $$async to get body content
67
+ // and create swagger struct
68
+ const { $$async : _$b , ...omittedBody } = value . meta . body ?? { }
69
+ // biome-ignore lint/complexity/noForEach: <explanation>
70
+ Object . keys ( omittedBody ) . forEach ( key => {
71
+ omittedBody [ key ] = {
72
+ type : omittedBody [ key ] ,
73
+ }
74
+ } )
75
+
76
+ // Omit $$async to get params
77
+ // and create swagger struct
78
+ const { $$async : _$p , ...omittedParams } = value . meta . queries ?? { }
79
+ // biome-ignore lint/complexity/noForEach: <explanation>
80
+ Object . keys ( omittedParams ) . forEach ( key => {
81
+ omittedParams [ key ] = {
82
+ name : key ,
83
+ in : 'path' ,
84
+ required : true ,
85
+ schema : {
86
+ type : omittedParams [ key ] ,
87
+ } ,
88
+ }
89
+ } )
90
+
91
+ // Create request body struct for swagger
92
+ const bodyDef = value . meta . body
93
+ ? {
94
+ required : true ,
95
+ content : {
96
+ 'application/json' : {
97
+ schema : {
98
+ type : 'object' ,
99
+ properties : omittedBody ,
100
+ } ,
101
+ } ,
102
+ } ,
103
+ }
104
+ : { }
105
+
106
+ // Add struct of the endpoint to swagger
107
+ paths [ oriPath ] = { }
108
+ paths [ oriPath ] [ method . toLowerCase ( ) ] = {
109
+ ...( value . meta . description ? { description : value . meta . description } : { } ) ,
110
+ parameters : Object . values ( omittedParams ) ,
111
+ requestBody : bodyDef ,
112
+ }
45
113
}
46
114
}
47
115
0 commit comments