@@ -23,22 +23,27 @@ type CommandsDefinition = Record<
23
23
}
24
24
> ;
25
25
26
+ interface Schema {
27
+ schema ?: JSONSchema7 ;
28
+ name : string ;
29
+ description ?: string ;
30
+ }
31
+
26
32
interface HttpEvent {
27
33
path : string ;
28
34
method : HttpMethod ;
29
35
authorizer ?: any ;
30
36
cors ?: any ;
31
37
operationId : string ;
32
38
integration ?: string | undefined ;
33
- responseSchemas : {
34
- [ key : string ] : {
35
- 'application/json' : {
36
- schema ?: JSONSchema7 ;
37
- name : string ;
38
- description ?: string ;
39
- } ;
39
+ request : {
40
+ schemas : {
41
+ 'application/json' : Schema ;
40
42
} ;
41
43
} ;
44
+ responseSchemas : {
45
+ [ key : string ] : { 'application/json' : Schema } ;
46
+ } ;
42
47
}
43
48
44
49
export class ServerlessPlugin {
@@ -150,31 +155,24 @@ export class ServerlessPlugin {
150
155
openApi . paths [ path ] = { } ;
151
156
}
152
157
153
- const responseSchemas = httpEvent . responseSchemas ;
154
-
155
- const responses : OpenAPIV3 . ResponsesObject = { } ;
156
-
157
- for ( const code of Object . keys ( responseSchemas ) ) {
158
- const schema = responseSchemas [ code ] [ 'application/json' ] ;
159
- openApi . components . schemas [ schema . name ] = schema . schema as any ;
160
- responses [ code ] = {
161
- description : schema . description ,
162
- } ;
163
- if ( schema . schema ) {
164
- responses [ code ] [ 'content' ] = {
165
- 'application/json' : {
166
- schema : {
167
- $ref : '#/components/schemas/' + schema . name ,
168
- } ,
169
- } ,
170
- } ;
171
- }
172
- }
158
+ const responses = this . handleResponses (
159
+ httpEvent . responseSchemas ,
160
+ openApi
161
+ ) ;
173
162
174
- openApi . paths [ path ] [ this . getMethod ( httpEvent . method ) ] = {
163
+ const operation : OpenAPIV3 . OperationObject = {
175
164
operationId : httpEvent . operationId ,
176
165
responses : responses ,
177
- } as OpenAPIV3 . OperationObject ;
166
+ } ;
167
+
168
+ if ( httpEvent . request && httpEvent . request . schemas ) {
169
+ operation . requestBody = this . handleRequestBody (
170
+ httpEvent . request . schemas ,
171
+ openApi
172
+ ) ;
173
+ }
174
+
175
+ openApi . paths [ path ] [ this . getMethod ( httpEvent . method ) ] = operation ;
178
176
}
179
177
}
180
178
@@ -190,6 +188,60 @@ export class ServerlessPlugin {
190
188
191
189
writeFileSync ( out , output ) ;
192
190
}
191
+
192
+ private handleRequestBody (
193
+ requestSchemas : { 'application/json' : Schema } ,
194
+ openApi : OpenAPIV3 . Document
195
+ ) {
196
+ const request : OpenAPIV3 . RequestBodyObject = {
197
+ content : { } ,
198
+ required : true ,
199
+ } ;
200
+
201
+ const schemaJSON = requestSchemas [ 'application/json' ] ;
202
+ request . description = schemaJSON . description ;
203
+
204
+ if ( schemaJSON . schema ) {
205
+ request [ 'content' ] = {
206
+ 'application/json' : {
207
+ schema : {
208
+ $ref : '#/components/schemas/' + schemaJSON . name ,
209
+ } ,
210
+ } ,
211
+ } ;
212
+ delete schemaJSON . schema [ '$schema' ] ;
213
+ openApi . components . schemas [ schemaJSON . name ] = schemaJSON . schema as any ;
214
+ }
215
+ return request ;
216
+ }
217
+
218
+ private handleResponses (
219
+ responseSchemas : { [ key : string ] : { 'application/json' : Schema } } ,
220
+ openApi : OpenAPIV3 . Document
221
+ ) {
222
+ const responses : OpenAPIV3 . ResponsesObject = { } ;
223
+
224
+ for ( const code of Object . keys ( responseSchemas ) ) {
225
+ const schemaJSON = responseSchemas [ code ] [ 'application/json' ] ;
226
+ responses [ code ] = {
227
+ description : schemaJSON . description ,
228
+ } ;
229
+ if ( schemaJSON . schema ) {
230
+ responses [ code ] [ 'content' ] = {
231
+ 'application/json' : {
232
+ schema : {
233
+ $ref : '#/components/schemas/' + schemaJSON . name ,
234
+ } ,
235
+ } ,
236
+ } ;
237
+ delete schemaJSON . schema [ '$schema' ] ;
238
+ openApi . components . schemas [ schemaJSON . name ] = schemaJSON . schema as any ;
239
+ }
240
+ }
241
+
242
+ return responses ;
243
+ }
244
+
193
245
getMethod ( method : HttpMethod ) : OpenAPIV3 . HttpMethods {
194
246
switch ( method ) {
195
247
case 'get' :
0 commit comments