serverless-openapi-documenter 0.0.25 → 0.0.27
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/.github/workflows/node.yml +1 -1
- package/README.md +96 -27
- package/package.json +1 -1
- package/src/definitionGenerator.js +29 -0
package/README.md
CHANGED
|
@@ -63,6 +63,7 @@ Options:
|
|
|
63
63
|
| externalDocs.url | custom.documentation.externalDocumentation.url |
|
|
64
64
|
| servers[].description | custom.documentation.servers.description |
|
|
65
65
|
| servers[].url | custom.documentation.servers.url |
|
|
66
|
+
| servers[].variables | custom.documentation.servers.variables |
|
|
66
67
|
| tags[].name | custom.documentation.tags.name |
|
|
67
68
|
| tags[].description | custom.documentation.tags.description |
|
|
68
69
|
| tags[].externalDocs.url | custom.documentation.tags.externalDocumentation.url |
|
|
@@ -121,8 +122,15 @@ custom:
|
|
|
121
122
|
url: https://google.com
|
|
122
123
|
description: A link to google
|
|
123
124
|
servers:
|
|
124
|
-
url: https://example.com
|
|
125
|
+
url: https://example.com:{port}/
|
|
125
126
|
description: The server
|
|
127
|
+
variables:
|
|
128
|
+
port:
|
|
129
|
+
enum:
|
|
130
|
+
- 4000
|
|
131
|
+
- 3000
|
|
132
|
+
default: 3000
|
|
133
|
+
description: The port the server operates on
|
|
126
134
|
tags:
|
|
127
135
|
- name: tag1
|
|
128
136
|
description: this is a tag
|
|
@@ -222,6 +230,39 @@ custom:
|
|
|
222
230
|
type: "string"
|
|
223
231
|
```
|
|
224
232
|
|
|
233
|
+
##### Model re-use
|
|
234
|
+
|
|
235
|
+
Through the magic of YAML, you can re-use models:
|
|
236
|
+
|
|
237
|
+
```yml
|
|
238
|
+
custom:
|
|
239
|
+
documentation:
|
|
240
|
+
...
|
|
241
|
+
models:
|
|
242
|
+
- name: "ErrorResponse"
|
|
243
|
+
description: "This is an error"
|
|
244
|
+
content:
|
|
245
|
+
application/json:
|
|
246
|
+
schema: &ErrorItem
|
|
247
|
+
type: object
|
|
248
|
+
properties:
|
|
249
|
+
message:
|
|
250
|
+
type: string
|
|
251
|
+
code:
|
|
252
|
+
type: integer
|
|
253
|
+
|
|
254
|
+
- name: "PutDocumentResponse"
|
|
255
|
+
description: "PUT Document response model (external reference example)"
|
|
256
|
+
content:
|
|
257
|
+
application/json:
|
|
258
|
+
schema:
|
|
259
|
+
type: array
|
|
260
|
+
items: *ErrorItem
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
`&ErrorItem` in the above example creates a node anchor (&ErrorItem) to the `ErrorResponse` schema which then can be used in the `PutDocumentResponse` schema via the reference (*ErrorItem). The node anchor needs to be declared first before it can be used elsewhere via the reference, swapping the above example around would result in an error.
|
|
264
|
+
|
|
265
|
+
|
|
225
266
|
#### Functions
|
|
226
267
|
|
|
227
268
|
To define the documentation for a given function event, you need to create a `documentation` attribute for your http event in your `serverless.yml` file.
|
|
@@ -238,6 +279,7 @@ The `documentation` section of the event configuration can contain the following
|
|
|
238
279
|
* `queryParams`: a list of query parameters (see [queryParams](#queryparams) below)
|
|
239
280
|
* `pathParams`: a list of path parameters (see [pathParams](#pathparams) below)
|
|
240
281
|
* `cookieParams`: a list of cookie parameters (see [cookieParams](#cookieparams) below)
|
|
282
|
+
* `headerParams`: a list of headers (see [headerParams](#headerparams---request-headers) below)
|
|
241
283
|
* `methodResponses`: an array of response models and applicable status codes
|
|
242
284
|
* `statusCode`: applicable http status code (ie. 200/404/500 etc.)
|
|
243
285
|
* `responseBody`: contains description of the response
|
|
@@ -285,12 +327,22 @@ functions:
|
|
|
285
327
|
description: "A Session ID variable"
|
|
286
328
|
schema:
|
|
287
329
|
type: "string"
|
|
330
|
+
headerParams:
|
|
331
|
+
name: "Content-Type"
|
|
332
|
+
description: "The content type"
|
|
333
|
+
schema:
|
|
334
|
+
type: "string"
|
|
288
335
|
methodResponses:
|
|
289
336
|
- statusCode: 201
|
|
290
337
|
responseBody:
|
|
291
338
|
description: "A user object along with generated API Keys"
|
|
292
339
|
responseModels:
|
|
293
340
|
application/json: "PutDocumentResponse"
|
|
341
|
+
responseHeaders:
|
|
342
|
+
X-Rate-Limit-Limit:
|
|
343
|
+
description: The number of allowed requests in the current period
|
|
344
|
+
schema:
|
|
345
|
+
type: integer
|
|
294
346
|
- statusCode: 500
|
|
295
347
|
responseBody:
|
|
296
348
|
description: "An error message when creating a new user"
|
|
@@ -320,8 +372,8 @@ queryParams:
|
|
|
320
372
|
|
|
321
373
|
Path parameters can be described as follow:
|
|
322
374
|
|
|
323
|
-
* `name`: the name of the
|
|
324
|
-
* `description`: a description of the
|
|
375
|
+
* `name`: the name of the path parameter
|
|
376
|
+
* `description`: a description of the path parameter
|
|
325
377
|
* `schema`: JSON schema (inline, file or externally hosted)
|
|
326
378
|
|
|
327
379
|
```yml
|
|
@@ -336,9 +388,9 @@ pathParams:
|
|
|
336
388
|
|
|
337
389
|
Cookie parameters can be described as follow:
|
|
338
390
|
|
|
339
|
-
* `name`: the name of the
|
|
340
|
-
* `description`: a description of the
|
|
341
|
-
* `required`: whether the
|
|
391
|
+
* `name`: the name of the cookie parameter
|
|
392
|
+
* `description`: a description of the cookie parameter
|
|
393
|
+
* `required`: whether the cookie parameter is mandatory (boolean)
|
|
342
394
|
* `schema`: JSON schema (inline, file or externally hosted)
|
|
343
395
|
|
|
344
396
|
```yml
|
|
@@ -350,6 +402,24 @@ cookieParams:
|
|
|
350
402
|
type: "string"
|
|
351
403
|
```
|
|
352
404
|
|
|
405
|
+
#### `headerParams` - Request Headers
|
|
406
|
+
|
|
407
|
+
Request Headers can be described as follow:
|
|
408
|
+
|
|
409
|
+
* `name`: the name of the header
|
|
410
|
+
* `description`: a description of the header
|
|
411
|
+
* `required`: whether the header is mandatory (boolean)
|
|
412
|
+
* `schema`: JSON schema (inline, file or externally hosted)
|
|
413
|
+
|
|
414
|
+
```yml
|
|
415
|
+
headerParams:
|
|
416
|
+
- name: "Content-Type"
|
|
417
|
+
description: "The content type"
|
|
418
|
+
required: true
|
|
419
|
+
schema:
|
|
420
|
+
type: "string"
|
|
421
|
+
```
|
|
422
|
+
|
|
353
423
|
#### `requestModels`
|
|
354
424
|
|
|
355
425
|
The `requestModels` property allows you to define models for the HTTP Request of the function event. You can define a different model for each different `Content-Type`. You can define a reference to the relevant request model named in the `models` section of your configuration (see [Defining Models](#models) section).
|
|
@@ -369,14 +439,24 @@ For an example of a `methodResponses` configuration for an event see below:
|
|
|
369
439
|
```yml
|
|
370
440
|
methodResponse:
|
|
371
441
|
- statusCode: 200
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
description: "Content Type header"
|
|
375
|
-
schema:
|
|
376
|
-
type: "string"
|
|
442
|
+
responseBody:
|
|
443
|
+
description: Success
|
|
377
444
|
responseModels:
|
|
378
445
|
application/json: "CreateResponse"
|
|
379
446
|
application/xml: "CreateResponseXML"
|
|
447
|
+
responseHeaders:
|
|
448
|
+
X-Rate-Limit-Limit:
|
|
449
|
+
description: The number of allowed requests in the current period
|
|
450
|
+
schema:
|
|
451
|
+
type: integer
|
|
452
|
+
X-Rate-Limit-Remaining:
|
|
453
|
+
description: The number of remaining requests in the current period
|
|
454
|
+
schema:
|
|
455
|
+
type: integer
|
|
456
|
+
X-Rate-Limit-Reset:
|
|
457
|
+
description: The number of seconds left in the current period
|
|
458
|
+
schema:
|
|
459
|
+
type: integer
|
|
380
460
|
```
|
|
381
461
|
|
|
382
462
|
##### `responseModels`
|
|
@@ -389,27 +469,16 @@ responseModels:
|
|
|
389
469
|
application/xml: "CreateResponseXML"
|
|
390
470
|
```
|
|
391
471
|
|
|
392
|
-
##### `responseHeaders`
|
|
393
|
-
|
|
394
|
-
The `responseHeaders/requestHeaders` section of the configuration allows you to define the HTTP headers for the function event.
|
|
395
|
-
|
|
396
|
-
The attributes for a header are as follow:
|
|
472
|
+
##### `responseHeaders`
|
|
397
473
|
|
|
398
|
-
|
|
399
|
-
* `description`: a description of the HTTP Header
|
|
400
|
-
* `schema`: JSON schema (inline, file or externally hosted)
|
|
474
|
+
The `responseHeaders` property allows you to define the headers expected in a HTTP Response of the function event. This should only contain a description and a schema, which must be a JSON schema (inline, file or externally hosted).
|
|
401
475
|
|
|
402
476
|
```yml
|
|
403
477
|
responseHeaders:
|
|
404
|
-
-
|
|
405
|
-
description:
|
|
406
|
-
schema:
|
|
407
|
-
type: "string"
|
|
408
|
-
requestHeaders:
|
|
409
|
-
- name: "Content-Type"
|
|
410
|
-
description: "Content Type header"
|
|
478
|
+
X-Rate-Limit-Limit:
|
|
479
|
+
description: The number of allowed requests in the current period
|
|
411
480
|
schema:
|
|
412
|
-
type:
|
|
481
|
+
type: integer
|
|
413
482
|
```
|
|
414
483
|
|
|
415
484
|
## Example configuration
|
package/package.json
CHANGED
|
@@ -271,12 +271,41 @@ class DefinitionGenerator {
|
|
|
271
271
|
throw err
|
|
272
272
|
})
|
|
273
273
|
|
|
274
|
+
if (response.responseHeaders) {
|
|
275
|
+
obj.headers = await this.createResponseHeaders(response.responseHeaders)
|
|
276
|
+
.catch(err => {
|
|
277
|
+
throw err
|
|
278
|
+
})
|
|
279
|
+
}
|
|
280
|
+
|
|
274
281
|
Object.assign(responses,{[response.statusCode]: obj})
|
|
275
282
|
}
|
|
276
283
|
|
|
277
284
|
return responses
|
|
278
285
|
}
|
|
279
286
|
|
|
287
|
+
async createResponseHeaders(headers) {
|
|
288
|
+
const obj = {}
|
|
289
|
+
for (const header of Object.keys(headers)) {
|
|
290
|
+
const newHeader = {}
|
|
291
|
+
newHeader.description = headers[header].description || ''
|
|
292
|
+
|
|
293
|
+
if (headers[header].schema) {
|
|
294
|
+
const schemaRef = await this.schemaCreator(headers[header].schema, header)
|
|
295
|
+
.catch(err => {
|
|
296
|
+
throw err
|
|
297
|
+
})
|
|
298
|
+
newHeader.schema = {
|
|
299
|
+
$ref: schemaRef
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
Object.assign(obj, {[header]: newHeader})
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return obj
|
|
307
|
+
}
|
|
308
|
+
|
|
280
309
|
async createRequestBody(documentation) {
|
|
281
310
|
const obj = {
|
|
282
311
|
description: documentation.requestBody.description,
|