@@ -3,16 +3,16 @@ import * as c from 'chalk';
3
3
import * as openApiValidator from 'swagger2openapi/validate' ;
4
4
5
5
import * as uuid from 'uuid' ;
6
- import { ILog , IParameterConfig , IServerlessFunctionConfig , IServiceDescription } from './types' ;
6
+ import { IDefinitionConfig , ILog , IParameterConfig , IServerlessFunctionConfig } from './types' ;
7
7
import { clone , merge } from './utils' ;
8
8
9
- export class DocumentGenerator {
9
+ export class DefinitionGenerator {
10
10
// The OpenAPI version we currently validate against
11
- private openapiVersion = '3.0.0-RC1' ;
11
+ public version = '3.0.0-RC1' ;
12
12
13
13
// Base configuration object
14
- private config = {
15
- openapi : this . openapiVersion ,
14
+ public definition = {
15
+ openapi : this . version ,
16
16
description : '' ,
17
17
version : '0.0.0' ,
18
18
title : '' ,
@@ -22,65 +22,64 @@ export class DocumentGenerator {
22
22
} ,
23
23
} ;
24
24
25
- private serviceDescriptor : IServiceDescription ;
25
+ private config : IDefinitionConfig ;
26
26
private log : ILog ;
27
27
28
28
/**
29
29
* Constructor
30
30
* @param serviceDescriptor IServiceDescription
31
31
*/
32
- constructor ( { log, serviceDescriptor } : {
32
+ constructor ( { log, config } : {
33
33
log : ILog ,
34
- serviceDescriptor : IServiceDescription ,
34
+ config : IDefinitionConfig ,
35
35
} ) {
36
- this . serviceDescriptor = clone ( serviceDescriptor ) ;
36
+ this . config = clone ( config ) ;
37
37
this . log = log ;
38
+ }
39
+
40
+ public parse ( ) {
41
+ const {
42
+ title = '' ,
43
+ description = '' ,
44
+ version = uuid . v4 ( ) ,
45
+ models,
46
+ } = this . config ;
38
47
39
- merge ( this . config , {
40
- openapi : this . openapiVersion ,
48
+ merge ( this . definition , {
49
+ openapi : this . version ,
41
50
servers : [ ] ,
42
- info : {
43
- title : serviceDescriptor . summary || '' ,
44
- description : serviceDescriptor . description || '' ,
45
- version : serviceDescriptor . version || uuid . v4 ( ) ,
46
- } ,
51
+ info : { title, description, version } ,
47
52
paths : { } ,
48
53
components : {
49
54
schemas : { } ,
50
55
securitySchemes : { } ,
51
56
} ,
52
57
} ) ;
53
58
54
- for ( const model of serviceDescriptor . models ) {
55
- this . config . components . schemas [ model . name ] = this . cleanSchema ( dereference ( model . schema ) ) ;
59
+ if ( models ) {
60
+ for ( const model of models ) {
61
+ this . definition . components . schemas [ model . name ] = this . cleanSchema (
62
+ dereference ( model . schema ) ,
63
+ ) ;
64
+ }
56
65
}
57
- }
58
-
59
- public generate ( ) {
60
- const result : any = { } ;
61
- this . log ( `${ c . bold . yellow ( '[VALIDATION]' ) } Validating OpenAPI generated output\n` ) ;
62
66
63
- try {
64
- openApiValidator . validateSync ( this . config , result ) ;
67
+ return this ;
68
+ }
65
69
66
- this . log ( `${ c . bold . green ( '[VALIDATION]' ) } OpenAPI valid: ${ c . bold . green ( 'true' ) } \n\n` ) ;
70
+ public validate ( ) : { valid : boolean , context : string [ ] , warnings : any [ ] } {
71
+ const payload : any = { } ;
67
72
68
- return this . config ;
69
- } catch ( e ) {
70
- this . log (
71
- `${ c . bold . red ( '[VALIDATION]' ) } Failed to validate OpenAPI document: \n\n${ c . yellow ( e . message ) } \n\n` +
72
- `${ c . bold . green ( 'Path:' ) } ${ JSON . stringify ( result , null , 2 ) } \n` ,
73
- ) ;
73
+ openApiValidator . validateSync ( this . definition , payload ) ;
74
74
75
- throw new Error ( 'Failed to validate OpenAPI document' ) ;
76
- }
75
+ return payload ;
77
76
}
78
77
79
78
/**
80
79
* Add Paths to OpenAPI Configuration from Serverless function documentation
81
80
* @param config Add
82
81
*/
83
- public addPathsFromFunctionConfig ( config : IServerlessFunctionConfig [ ] ) : void {
82
+ public readFunctions ( config : IServerlessFunctionConfig [ ] ) : void {
84
83
// loop through function configurations
85
84
for ( const funcConfig of config ) {
86
85
// loop through http events
@@ -102,7 +101,7 @@ export class DocumentGenerator {
102
101
} ,
103
102
} ;
104
103
// merge path configuration into main configuration
105
- merge ( this . config . paths , pathConfig ) ;
104
+ merge ( this . definition . paths , pathConfig ) ;
106
105
}
107
106
}
108
107
}
@@ -179,7 +178,6 @@ export class DocumentGenerator {
179
178
: parameter . style === 'form' ;
180
179
}
181
180
182
- // console.log(parameter);
183
181
if ( parameter . schema ) {
184
182
parameterConfig . schema = this . cleanSchema ( parameter . schema ) ;
185
183
}
@@ -190,7 +188,6 @@ export class DocumentGenerator {
190
188
parameterConfig . examples = parameter . examples ;
191
189
}
192
190
193
- // Add parameter config to parameters array
194
191
parameters . push ( parameterConfig ) ;
195
192
}
196
193
}
@@ -210,7 +207,7 @@ export class DocumentGenerator {
210
207
// For each request model type (Sorted by "Content-Type")
211
208
for ( const requestModelType of Object . keys ( documentationConfig . requestModels ) ) {
212
209
// get schema reference information
213
- const requestModel = this . serviceDescriptor . models . filter (
210
+ const requestModel = this . config . models . filter (
214
211
( model ) => model . name === documentationConfig . requestModels [ requestModelType ] ,
215
212
) . pop ( ) ;
216
213
@@ -287,7 +284,7 @@ export class DocumentGenerator {
287
284
private getResponseContent ( response ) {
288
285
const content = { } ;
289
286
for ( const responseKey of Object . keys ( response ) ) {
290
- const responseModel = this . serviceDescriptor . models . filter (
287
+ const responseModel = this . config . models . filter (
291
288
( model ) => model . name === response [ responseKey ] ,
292
289
) . pop ( ) ;
293
290
if ( responseModel ) {
0 commit comments