1- import _ = require( ' lodash' ) ;
1+ import _ = require( " lodash" ) ;
22// tslint:disable-next-line no-submodule-imports
3- import { validateSync as openApiValidatorSync } from 'swagger2openapi/validate' ;
4- import * as uuid from 'uuid' ;
5-
6- import { parseModels } from './parse' ;
7- import { IDefinition , IDefinitionConfig , IOperation , IParameterConfig , IServerlessFunctionConfig } from './types' ;
8- import { cleanSchema } from './utils' ;
3+ import { validateSync as openApiValidatorSync } from "swagger2openapi/validate" ;
4+ import * as uuid from "uuid" ;
5+
6+ import { parseModels } from "./parse" ;
7+ import {
8+ Definition ,
9+ DefinitionConfig ,
10+ Operation ,
11+ ParameterConfig ,
12+ ServerlessFunctionConfig
13+ } from "./types" ;
14+ import { cleanSchema } from "./utils" ;
915
1016export class DefinitionGenerator {
1117 // The OpenAPI version we currently validate against
12- public version = ' 3.0.0' ;
18+ public version = " 3.0.0" ;
1319
1420 // Base configuration object
15- public definition = < IDefinition > {
21+ public definition : Definition = {
1622 openapi : this . version ,
17- components : { } ,
23+ components : { }
1824 } ;
1925
20- public config : IDefinitionConfig ;
26+ public config : DefinitionConfig ;
2127
2228 private root : string ;
2329
2430 /**
2531 * Constructor
2632 */
27- constructor ( config : IDefinitionConfig , root : string ) {
33+ public constructor ( config : DefinitionConfig , root : string ) {
2834 this . config = _ . cloneDeep ( config ) ;
2935 this . root = root ;
3036 }
3137
32- public async parse ( ) {
38+ public async parse ( ) {
3339 const {
34- title = '' ,
35- description = '' ,
40+ title = "" ,
41+ description = "" ,
3642 version = uuid . v4 ( ) ,
37- models,
43+ models
3844 } = this . config ;
3945
4046 _ . merge ( this . definition , {
@@ -43,16 +49,21 @@ export class DefinitionGenerator {
4349 paths : { } ,
4450 components : {
4551 schemas : { } ,
46- securitySchemes : { } ,
47- } ,
52+ securitySchemes : { }
53+ }
4854 } ) ;
4955
5056 this . definition . components . schemas = await parseModels ( models , this . root ) ;
5157
5258 return this ;
5359 }
5460
55- public validate ( ) : { valid : boolean , context : string [ ] , warnings : any [ ] , error ?: any [ ] } {
61+ public validate ( ) : {
62+ valid : boolean ;
63+ context : Array < string > ;
64+ warnings : Array < any > ;
65+ error ?: Array < any > ;
66+ } {
5667 const payload : any = { } ;
5768
5869 try {
@@ -68,7 +79,7 @@ export class DefinitionGenerator {
6879 * Add Paths to OpenAPI Configuration from Serverless function documentation
6980 * @param config Add
7081 */
71- public readFunctions ( config : IServerlessFunctionConfig [ ] ) : void {
82+ public readFunctions ( config : Array < ServerlessFunctionConfig > ) : void {
7283 // loop through function configurations
7384 for ( const funcConfig of config ) {
7485 // loop through http events
@@ -81,9 +92,9 @@ export class DefinitionGenerator {
8192 [ `/${ httpEventConfig . path } ` ] : {
8293 [ httpEventConfig . method . toLowerCase ( ) ] : this . getOperationFromConfig (
8394 funcConfig . _functionName ,
84- httpEventConfig . documentation ,
85- ) ,
86- } ,
95+ httpEventConfig . documentation
96+ )
97+ }
8798 } ;
8899
89100 // merge path configuration into main configuration
@@ -100,9 +111,12 @@ export class DefinitionGenerator {
100111 * @param funcName
101112 * @param documentationConfig
102113 */
103- private getOperationFromConfig ( funcName : string , documentationConfig ) : IOperation {
104- const operationObj : IOperation = {
105- operationId : funcName ,
114+ private getOperationFromConfig (
115+ funcName : string ,
116+ documentationConfig
117+ ) : Operation {
118+ const operationObj : Operation = {
119+ operationId : funcName
106120 } ;
107121
108122 if ( documentationConfig . summary ) {
@@ -122,7 +136,9 @@ export class DefinitionGenerator {
122136 }
123137
124138 if ( documentationConfig . requestBody ) {
125- operationObj . requestBody = this . getRequestBodiesFromConfig ( documentationConfig ) ;
139+ operationObj . requestBody = this . getRequestBodiesFromConfig (
140+ documentationConfig
141+ ) ;
126142 }
127143
128144 operationObj . parameters = this . getParametersFromConfig ( documentationConfig ) ;
@@ -136,54 +152,54 @@ export class DefinitionGenerator {
136152 * Derives Path, Query and Request header parameters from Serverless documentation
137153 * @param documentationConfig
138154 */
139- private getParametersFromConfig ( documentationConfig ) : IParameterConfig [ ] {
140- const parameters : IParameterConfig [ ] = [ ] ;
155+ private getParametersFromConfig ( documentationConfig ) : Array < ParameterConfig > {
156+ const parameters : Array < ParameterConfig > = [ ] ;
141157
142158 // Build up parameters from configuration for each parameter type
143- for ( const type of [ ' path' , ' query' , ' header' , ' cookie' ] ) {
159+ for ( const type of [ " path" , " query" , " header" , " cookie" ] ) {
144160 let paramBlock ;
145- if ( type === ' path' && documentationConfig . pathParams ) {
161+ if ( type === " path" && documentationConfig . pathParams ) {
146162 paramBlock = documentationConfig . pathParams ;
147- } else if ( type === ' query' && documentationConfig . queryParams ) {
163+ } else if ( type === " query" && documentationConfig . queryParams ) {
148164 paramBlock = documentationConfig . queryParams ;
149- } else if ( type === ' header' && documentationConfig . requestHeaders ) {
165+ } else if ( type === " header" && documentationConfig . requestHeaders ) {
150166 paramBlock = documentationConfig . requestHeaders ;
151- } else if ( type === ' cookie' && documentationConfig . cookieParams ) {
167+ } else if ( type === " cookie" && documentationConfig . cookieParams ) {
152168 paramBlock = documentationConfig . cookieParams ;
153169 } else {
154170 continue ;
155171 }
156172
157173 // Loop through each parameter in a parameter block and add parameters to array
158174 for ( const parameter of paramBlock ) {
159- const parameterConfig : IParameterConfig = {
175+ const parameterConfig : ParameterConfig = {
160176 name : parameter . name ,
161177 in : type ,
162- description : parameter . description || '' ,
163- required : parameter . required || false , // Note: all path parameters must be required
178+ description : parameter . description || "" ,
179+ required : parameter . required || false // Note: all path parameters must be required
164180 } ;
165181
166182 // if type is path, then required must be true (@see OpenAPI 3.0-RC1)
167- if ( type === ' path' ) {
183+ if ( type === " path" ) {
168184 parameterConfig . required = true ;
169- } else if ( type === ' query' ) {
170- parameterConfig . allowEmptyValue = parameter . allowEmptyValue || false ; // OpenAPI default is false
185+ } else if ( type === " query" ) {
186+ parameterConfig . allowEmptyValue = parameter . allowEmptyValue || false ; // OpenAPI default is false
171187
172- if ( ' allowReserved' in parameter ) {
188+ if ( " allowReserved" in parameter ) {
173189 parameterConfig . allowReserved = parameter . allowReserved || false ;
174190 }
175191 }
176192
177- if ( ' deprecated' in parameter ) {
193+ if ( " deprecated" in parameter ) {
178194 parameterConfig . deprecated = parameter . deprecated ;
179195 }
180196
181- if ( ' style' in parameter ) {
197+ if ( " style" in parameter ) {
182198 parameterConfig . style = parameter . style ;
183199
184200 parameterConfig . explode = parameter . explode
185201 ? parameter . explode
186- : parameter . style === ' form' ;
202+ : parameter . style === " form" ;
187203 }
188204
189205 if ( parameter . schema ) {
@@ -211,39 +227,56 @@ export class DefinitionGenerator {
211227 * Derives request body schemas from event documentation configuration
212228 * @param documentationConfig
213229 */
214- private getRequestBodiesFromConfig ( documentationConfig ) {
230+ private getRequestBodiesFromConfig ( documentationConfig ) {
215231 const requestBodies = { } ;
216232
217233 if ( ! documentationConfig . requestModels ) {
218- throw new Error ( `Required requestModels in: ${ JSON . stringify ( documentationConfig , null , 2 ) } ` ) ;
234+ throw new Error (
235+ `Required requestModels in: ${ JSON . stringify (
236+ documentationConfig ,
237+ null ,
238+ 2
239+ ) } `
240+ ) ;
219241 }
220242
221243 // Does this event have a request model?
222244 if ( documentationConfig . requestModels ) {
223245 // For each request model type (Sorted by "Content-Type")
224- for ( const requestModelType of Object . keys ( documentationConfig . requestModels ) ) {
246+ for ( const requestModelType of Object . keys (
247+ documentationConfig . requestModels
248+ ) ) {
225249 // get schema reference information
226- const requestModel = this . config . models . filter (
227- ( model ) => model . name === documentationConfig . requestModels [ requestModelType ] ,
228- ) . pop ( ) ;
250+ const requestModel = this . config . models
251+ . filter (
252+ model =>
253+ model . name === documentationConfig . requestModels [ requestModelType ]
254+ )
255+ . pop ( ) ;
229256
230257 if ( requestModel ) {
231258 const reqModelConfig = {
232259 schema : {
233- $ref : `#/components/schemas/${ documentationConfig . requestModels [ requestModelType ] } ` ,
234- } ,
260+ $ref : `#/components/schemas/${
261+ documentationConfig . requestModels [ requestModelType ]
262+ } `
263+ }
235264 } ;
236265
237266 this . attachExamples ( requestModel , reqModelConfig ) ;
238267
239- const reqBodyConfig : { content : object , description ?: string } = {
268+ const reqBodyConfig : { content : object ; description ?: string } = {
240269 content : {
241- [ requestModelType ] : reqModelConfig ,
242- } ,
270+ [ requestModelType ] : reqModelConfig
271+ }
243272 } ;
244273
245- if ( documentationConfig . requestBody && 'description' in documentationConfig . requestBody ) {
246- reqBodyConfig . description = documentationConfig . requestBody . description ;
274+ if (
275+ documentationConfig . requestBody &&
276+ "description" in documentationConfig . requestBody
277+ ) {
278+ reqBodyConfig . description =
279+ documentationConfig . requestBody . description ;
247280 }
248281
249282 _ . merge ( requestBodies , reqBodyConfig ) ;
@@ -254,7 +287,7 @@ export class DefinitionGenerator {
254287 return requestBodies ;
255288 }
256289
257- private attachExamples ( target , config ) {
290+ private attachExamples ( target , config ) {
258291 if ( target . examples && Array . isArray ( target . examples ) ) {
259292 _ . merge ( config , { examples : _ . cloneDeep ( target . examples ) } ) ;
260293 } else if ( target . example ) {
@@ -266,65 +299,70 @@ export class DefinitionGenerator {
266299 * Gets response bodies from documentation config
267300 * @param documentationConfig
268301 */
269- private getResponsesFromConfig ( documentationConfig ) {
302+ private getResponsesFromConfig ( documentationConfig ) {
270303 const responses = { } ;
271304 if ( documentationConfig . methodResponses ) {
272305 for ( const response of documentationConfig . methodResponses ) {
273- const methodResponseConfig : { description : any , content : object , headers ?: object } = {
274- description : (
275- ( response . responseBody && 'description' in response . responseBody )
306+ const methodResponseConfig : {
307+ description : any ;
308+ content : object ;
309+ headers ?: object ;
310+ } = {
311+ description :
312+ response . responseBody && "description" in response . responseBody
276313 ? response . responseBody . description
277- : `Status ${ response . statusCode } Response`
278- ) ,
279- content : this . getResponseContent ( response . responseModels ) ,
314+ : `Status ${ response . statusCode } Response` ,
315+ content : this . getResponseContent ( response . responseModels )
280316 } ;
281317
282318 if ( response . responseHeaders ) {
283319 methodResponseConfig . headers = { } ;
284320 for ( const header of response . responseHeaders ) {
285321 methodResponseConfig . headers [ header . name ] = {
286- description : header . description || `${ header . name } header` ,
322+ description : header . description || `${ header . name } header`
287323 } ;
288324 if ( header . schema ) {
289- methodResponseConfig . headers [ header . name ] . schema = cleanSchema ( header . schema ) ;
325+ methodResponseConfig . headers [ header . name ] . schema = cleanSchema (
326+ header . schema
327+ ) ;
290328 }
291329 }
292330 }
293331
294332 _ . merge ( responses , {
295- [ response . statusCode ] : methodResponseConfig ,
333+ [ response . statusCode ] : methodResponseConfig
296334 } ) ;
297335 }
298336 }
299337
300338 return responses ;
301339 }
302340
303- private getResponseContent ( response ) {
341+ private getResponseContent ( response ) {
304342 const content = { } ;
305343
306344 for ( const responseKey of Object . keys ( response ) ) {
307- const responseModel = this . config . models . find ( ( model ) =>
308- model . name === response [ responseKey ] ,
345+ const responseModel = this . config . models . find (
346+ model => model . name === response [ responseKey ]
309347 ) ;
310348
311349 if ( responseModel ) {
312350 const resModelConfig = {
313351 schema : {
314- $ref : `#/components/schemas/${ response [ responseKey ] } ` ,
315- } ,
352+ $ref : `#/components/schemas/${ response [ responseKey ] } `
353+ }
316354 } ;
317355
318356 this . attachExamples ( responseModel , resModelConfig ) ;
319357
320- _ . merge ( content , { [ responseKey ] : resModelConfig } ) ;
358+ _ . merge ( content , { [ responseKey ] : resModelConfig } ) ;
321359 }
322360 }
323361
324362 return content ;
325363 }
326364
327- private getHttpEvents ( funcConfig ) {
328- return funcConfig . filter ( ( event ) => event . http ? true : false ) ;
365+ private getHttpEvents ( funcConfig ) {
366+ return funcConfig . filter ( event => ( event . http ? true : false ) ) ;
329367 }
330368}
0 commit comments