serverless-openapi-documenter 0.0.51 → 0.0.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -0
- package/package.json +1 -1
- package/src/definitionGenerator.js +42 -35
package/README.md
CHANGED
|
@@ -295,12 +295,22 @@ custom:
|
|
|
295
295
|
contentType: "application/json"
|
|
296
296
|
schema:
|
|
297
297
|
$schema: "http://json-schema.org/draft-04/schema#"
|
|
298
|
+
type: object
|
|
298
299
|
properties:
|
|
299
300
|
SomeObject:
|
|
300
301
|
type: "object"
|
|
301
302
|
properties:
|
|
302
303
|
SomeAttribute:
|
|
303
304
|
type: "string"
|
|
305
|
+
examples:
|
|
306
|
+
- name: someObjectInlineExample
|
|
307
|
+
summary: an example of a request
|
|
308
|
+
description: a longer string than the summary
|
|
309
|
+
value: {SomeObject: {SomeAttribute: 'attribute'}}
|
|
310
|
+
- name: someObjectExternalExample
|
|
311
|
+
summary: an example of a request external
|
|
312
|
+
description: a longer string than the summary
|
|
313
|
+
externalValue: https://example.com/external.json
|
|
304
314
|
```
|
|
305
315
|
|
|
306
316
|
The Second way of writing the models:
|
|
@@ -329,12 +339,22 @@ custom:
|
|
|
329
339
|
application/json:
|
|
330
340
|
schema:
|
|
331
341
|
$schema: "http://json-schema.org/draft-04/schema#"
|
|
342
|
+
type: object
|
|
332
343
|
properties:
|
|
333
344
|
SomeObject:
|
|
334
345
|
type: "object"
|
|
335
346
|
properties:
|
|
336
347
|
SomeAttribute:
|
|
337
348
|
type: "string"
|
|
349
|
+
examples:
|
|
350
|
+
- name: someObjectInlineExample
|
|
351
|
+
summary: an example of a request
|
|
352
|
+
description: a longer string than the summary
|
|
353
|
+
value: {SomeObject: {SomeAttribute: 'attribute'}}
|
|
354
|
+
- name: someObjectExternalExample
|
|
355
|
+
summary: an example of a request external
|
|
356
|
+
description: a longer string than the summary
|
|
357
|
+
externalValue: https://example.com/external.json
|
|
338
358
|
```
|
|
339
359
|
|
|
340
360
|
##### Model re-use
|
package/package.json
CHANGED
|
@@ -79,7 +79,7 @@ class DefinitionGenerator {
|
|
|
79
79
|
|
|
80
80
|
if (this.serverless.service.custom.documentation.servers) {
|
|
81
81
|
const servers = this.createServers(this.serverless.service.custom.documentation.servers)
|
|
82
|
-
Object.assign(this.openAPI, {servers: servers})
|
|
82
|
+
Object.assign(this.openAPI, { servers: servers })
|
|
83
83
|
}
|
|
84
84
|
|
|
85
85
|
if (this.serverless.service.custom.documentation.tags) {
|
|
@@ -88,7 +88,7 @@ class DefinitionGenerator {
|
|
|
88
88
|
|
|
89
89
|
if (this.serverless.service.custom.documentation.externalDocumentation) {
|
|
90
90
|
const extDoc = this.createExternalDocumentation(this.serverless.service.custom.documentation.externalDocumentation)
|
|
91
|
-
Object.assign(this.openAPI, {externalDocs: extDoc})
|
|
91
|
+
Object.assign(this.openAPI, { externalDocs: extDoc })
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
|
|
@@ -113,7 +113,7 @@ class DefinitionGenerator {
|
|
|
113
113
|
contactObj.url = documentation.contact.url
|
|
114
114
|
|
|
115
115
|
contactObj.email = documentation.contact.email || ''
|
|
116
|
-
Object.assign(info, {contact: contactObj})
|
|
116
|
+
Object.assign(info, { contact: contactObj })
|
|
117
117
|
}
|
|
118
118
|
|
|
119
119
|
if (documentation.license && documentation.license.name) {
|
|
@@ -123,16 +123,16 @@ class DefinitionGenerator {
|
|
|
123
123
|
if (documentation.license.url)
|
|
124
124
|
licenseObj.url = documentation.license.url || ''
|
|
125
125
|
|
|
126
|
-
Object.assign(info, {license: licenseObj})
|
|
126
|
+
Object.assign(info, { license: licenseObj })
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
for (const key of Object.keys(documentation)) {
|
|
130
130
|
if (/^[x\-]/i.test(key)) {
|
|
131
|
-
Object.assign(info, {[key]: documentation[key]})
|
|
131
|
+
Object.assign(info, { [key]: documentation[key] })
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
134
|
|
|
135
|
-
Object.assign(this.openAPI, {info})
|
|
135
|
+
Object.assign(this.openAPI, { info })
|
|
136
136
|
}
|
|
137
137
|
|
|
138
138
|
async createPaths() {
|
|
@@ -173,18 +173,18 @@ class DefinitionGenerator {
|
|
|
173
173
|
let slashPath = (event?.http?.path || event.httpApi?.path) ?? '/'
|
|
174
174
|
const pathStart = new RegExp(/^\//, 'g')
|
|
175
175
|
if (pathStart.test(slashPath) === false) {
|
|
176
|
-
slashPath = `/${(event?.http?.path||event.httpApi?.path)?? ''}`
|
|
176
|
+
slashPath = `/${(event?.http?.path || event.httpApi?.path) ?? ''}`
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
if (paths[slashPath]) {
|
|
180
180
|
Object.assign(paths[slashPath], path);
|
|
181
181
|
} else {
|
|
182
|
-
Object.assign(paths, {[slashPath]: path});
|
|
182
|
+
Object.assign(paths, { [slashPath]: path });
|
|
183
183
|
}
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
|
-
Object.assign(this.openAPI, {paths})
|
|
187
|
+
Object.assign(this.openAPI, { paths })
|
|
188
188
|
}
|
|
189
189
|
|
|
190
190
|
createServers(servers) {
|
|
@@ -227,7 +227,7 @@ class DefinitionGenerator {
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
createExternalDocumentation(docs) {
|
|
230
|
-
return {...docs}
|
|
230
|
+
return { ...docs }
|
|
231
231
|
// const documentation = this.serverless.service.custom.documentation
|
|
232
232
|
// if (documentation.externalDocumentation) {
|
|
233
233
|
// // Object.assign(this.openAPI, {externalDocs: {...documentation.externalDocumentation}})
|
|
@@ -251,7 +251,7 @@ class DefinitionGenerator {
|
|
|
251
251
|
}
|
|
252
252
|
tags.push(obj)
|
|
253
253
|
}
|
|
254
|
-
Object.assign(this.openAPI, {tags: tags})
|
|
254
|
+
Object.assign(this.openAPI, { tags: tags })
|
|
255
255
|
}
|
|
256
256
|
|
|
257
257
|
async createOperationObject(method, documentation, name = uuid()) {
|
|
@@ -323,7 +323,7 @@ class DefinitionGenerator {
|
|
|
323
323
|
obj.servers = servers
|
|
324
324
|
}
|
|
325
325
|
|
|
326
|
-
return {[method.toLowerCase()]: obj}
|
|
326
|
+
return { [method.toLowerCase()]: obj }
|
|
327
327
|
}
|
|
328
328
|
|
|
329
329
|
async createResponses(documentation) {
|
|
@@ -363,7 +363,7 @@ class DefinitionGenerator {
|
|
|
363
363
|
obj.headers = corsHeaders
|
|
364
364
|
}
|
|
365
365
|
|
|
366
|
-
Object.assign(responses,{[response.statusCode]: obj})
|
|
366
|
+
Object.assign(responses, { [response.statusCode]: obj })
|
|
367
367
|
}
|
|
368
368
|
|
|
369
369
|
return responses
|
|
@@ -380,7 +380,7 @@ class DefinitionGenerator {
|
|
|
380
380
|
const newHeaders = {}
|
|
381
381
|
for (const key of Object.keys(this.DEFAULT_CORS_HEADERS)) {
|
|
382
382
|
if (key === 'Access-Control-Allow-Credentials' &&
|
|
383
|
-
this.currentEvent.cors.allowCredentials === undefined || this.currentEvent.cors?.allowCredentials === false) {
|
|
383
|
+
(this.currentEvent.cors.allowCredentials === undefined || this.currentEvent.cors?.allowCredentials === false)) {
|
|
384
384
|
continue
|
|
385
385
|
}
|
|
386
386
|
|
|
@@ -394,7 +394,7 @@ class DefinitionGenerator {
|
|
|
394
394
|
}
|
|
395
395
|
}
|
|
396
396
|
|
|
397
|
-
Object.assign(newHeaders, {[key]: obj})
|
|
397
|
+
Object.assign(newHeaders, { [key]: obj })
|
|
398
398
|
}
|
|
399
399
|
|
|
400
400
|
headers = await this.createResponseHeaders(newHeaders)
|
|
@@ -423,7 +423,7 @@ class DefinitionGenerator {
|
|
|
423
423
|
}
|
|
424
424
|
}
|
|
425
425
|
|
|
426
|
-
Object.assign(obj, {[header]: newHeader})
|
|
426
|
+
Object.assign(obj, { [header]: newHeader })
|
|
427
427
|
}
|
|
428
428
|
|
|
429
429
|
return obj
|
|
@@ -458,16 +458,22 @@ class DefinitionGenerator {
|
|
|
458
458
|
}
|
|
459
459
|
const obj = {}
|
|
460
460
|
|
|
461
|
-
if (mediaTypeDocumentation.example)
|
|
462
|
-
obj.example = mediaTypeDocumentation.example
|
|
463
|
-
|
|
464
|
-
if (mediaTypeDocumentation.examples)
|
|
465
|
-
obj.examples = this.createExamples(mediaTypeDocumentation.examples)
|
|
466
|
-
|
|
467
461
|
let schema
|
|
468
462
|
if (mediaTypeDocumentation?.content) {
|
|
463
|
+
if (mediaTypeDocumentation.content[contentKey]?.example)
|
|
464
|
+
obj.example = mediaTypeDocumentation.content[contentKey].example
|
|
465
|
+
|
|
466
|
+
if (mediaTypeDocumentation.content[contentKey]?.examples)
|
|
467
|
+
obj.examples = this.createExamples(mediaTypeDocumentation.content[contentKey].examples)
|
|
468
|
+
|
|
469
469
|
schema = mediaTypeDocumentation.content[contentKey].schema
|
|
470
470
|
} else if (mediaTypeDocumentation?.contentType && mediaTypeDocumentation.schema) {
|
|
471
|
+
if (mediaTypeDocumentation?.example)
|
|
472
|
+
obj.example = mediaTypeDocumentation.example
|
|
473
|
+
|
|
474
|
+
if (mediaTypeDocumentation?.examples)
|
|
475
|
+
obj.examples = this.createExamples(mediaTypeDocumentation.examples)
|
|
476
|
+
|
|
471
477
|
schema = mediaTypeDocumentation.schema
|
|
472
478
|
}
|
|
473
479
|
|
|
@@ -479,7 +485,7 @@ class DefinitionGenerator {
|
|
|
479
485
|
$ref: schemaRef
|
|
480
486
|
}
|
|
481
487
|
|
|
482
|
-
Object.assign(mediaTypeObj, {[contentKey]: obj})
|
|
488
|
+
Object.assign(mediaTypeObj, { [contentKey]: obj })
|
|
483
489
|
}
|
|
484
490
|
}
|
|
485
491
|
return mediaTypeObj
|
|
@@ -551,10 +557,10 @@ class DefinitionGenerator {
|
|
|
551
557
|
const oldRef = originalSchema.$ref
|
|
552
558
|
const path = oldRef.split('/')
|
|
553
559
|
|
|
554
|
-
const pathTitle = path[path.length-1]
|
|
560
|
+
const pathTitle = path[path.length - 1]
|
|
555
561
|
const referencedProperties = deReferencedSchema.definitions[pathTitle]
|
|
556
562
|
|
|
557
|
-
Object.assign(deReferencedSchema, {...referencedProperties})
|
|
563
|
+
Object.assign(deReferencedSchema, { ...referencedProperties })
|
|
558
564
|
|
|
559
565
|
delete deReferencedSchema.$ref
|
|
560
566
|
deReferencedSchema = await this.dereferenceSchema(deReferencedSchema)
|
|
@@ -612,7 +618,7 @@ class DefinitionGenerator {
|
|
|
612
618
|
if (this.openAPI.components[type]) {
|
|
613
619
|
Object.assign(this.openAPI.components[type], schemaObj)
|
|
614
620
|
} else {
|
|
615
|
-
Object.assign(this.openAPI.components, {[type]: schemaObj})
|
|
621
|
+
Object.assign(this.openAPI.components, { [type]: schemaObj })
|
|
616
622
|
}
|
|
617
623
|
} else {
|
|
618
624
|
const components = {
|
|
@@ -633,30 +639,30 @@ class DefinitionGenerator {
|
|
|
633
639
|
if (securityScheme.description)
|
|
634
640
|
schema.description = securityScheme.description
|
|
635
641
|
|
|
636
|
-
switch(securityScheme.type.toLowerCase()) {
|
|
642
|
+
switch (securityScheme.type.toLowerCase()) {
|
|
637
643
|
case 'apikey':
|
|
638
644
|
const apiKeyScheme = this.createAPIKeyScheme(securityScheme)
|
|
639
645
|
schema.type = 'apiKey'
|
|
640
646
|
Object.assign(schema, apiKeyScheme)
|
|
641
|
-
|
|
647
|
+
break;
|
|
642
648
|
|
|
643
649
|
case 'http':
|
|
644
650
|
const HTTPScheme = this.createHTTPScheme(securityScheme)
|
|
645
651
|
schema.type = 'http'
|
|
646
652
|
Object.assign(schema, HTTPScheme)
|
|
647
|
-
|
|
653
|
+
break;
|
|
648
654
|
|
|
649
655
|
case 'openidconnect':
|
|
650
656
|
const openIdConnectScheme = this.createOpenIDConnectScheme(securityScheme)
|
|
651
657
|
schema.type = 'openIdConnect'
|
|
652
658
|
Object.assign(schema, openIdConnectScheme)
|
|
653
|
-
|
|
659
|
+
break;
|
|
654
660
|
|
|
655
661
|
case 'oauth2':
|
|
656
662
|
const oAuth2Scheme = this.createOAuth2Scheme(securityScheme)
|
|
657
663
|
schema.type = 'oauth2'
|
|
658
664
|
Object.assign(schema, oAuth2Scheme)
|
|
659
|
-
|
|
665
|
+
break;
|
|
660
666
|
}
|
|
661
667
|
|
|
662
668
|
this.addToComponents(this.componentTypes.securitySchemes, schema, scheme)
|
|
@@ -706,7 +712,7 @@ class DefinitionGenerator {
|
|
|
706
712
|
const schema = {}
|
|
707
713
|
if (securitySchema.flows) {
|
|
708
714
|
const flows = this.createOAuthFlows(securitySchema.flows)
|
|
709
|
-
Object.assign(schema, {flows: flows})
|
|
715
|
+
Object.assign(schema, { flows: flows })
|
|
710
716
|
} else
|
|
711
717
|
throw new Error('Security Scheme for "oauth2" requires flows')
|
|
712
718
|
|
|
@@ -738,7 +744,7 @@ class DefinitionGenerator {
|
|
|
738
744
|
else
|
|
739
745
|
throw new Error(`oAuth2 ${flow} flow requires scopes`)
|
|
740
746
|
|
|
741
|
-
Object.assign(obj, {[flow]: schema})
|
|
747
|
+
Object.assign(obj, { [flow]: schema })
|
|
742
748
|
}
|
|
743
749
|
return obj
|
|
744
750
|
}
|
|
@@ -746,8 +752,9 @@ class DefinitionGenerator {
|
|
|
746
752
|
createExamples(examples) {
|
|
747
753
|
const examplesObj = {}
|
|
748
754
|
|
|
749
|
-
for(const example of examples) {
|
|
750
|
-
Object.assign(examplesObj, {[example.name]: example})
|
|
755
|
+
for (const example of examples) {
|
|
756
|
+
Object.assign(examplesObj, { [example.name]: example })
|
|
757
|
+
delete examplesObj[example.name].name
|
|
751
758
|
}
|
|
752
759
|
|
|
753
760
|
return examplesObj;
|