Skip to content

Commit 5b6aa4b

Browse files
authored
Merge pull request #57 from JaredCE/correctly-document-headers
Correctly document headers
2 parents 8c79a06 + 979a847 commit 5b6aa4b

File tree

4 files changed

+69
-23
lines changed

4 files changed

+69
-23
lines changed

README.md

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,24 @@ cookieParams:
350350
type: "string"
351351
```
352352

353+
#### `headerParams` - Request Headers
354+
355+
Request Headers can be described as follow:
356+
357+
* `name`: the name of the query variable
358+
* `description`: a description of the query variable
359+
* `required`: whether the query parameter is mandatory (boolean)
360+
* `schema`: JSON schema (inline, file or externally hosted)
361+
362+
```yml
363+
headerParams:
364+
- name: "Content-Type"
365+
description: "The content type"
366+
required: true
367+
schema:
368+
type: "string"
369+
```
370+
353371
#### `requestModels`
354372

355373
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 +387,24 @@ For an example of a `methodResponses` configuration for an event see below:
369387
```yml
370388
methodResponse:
371389
- statusCode: 200
372-
responseHeaders:
373-
- name: "Content-Type"
374-
description: "Content Type header"
375-
schema:
376-
type: "string"
390+
responseBody:
391+
description: Success
377392
responseModels:
378393
application/json: "CreateResponse"
379394
application/xml: "CreateResponseXML"
395+
responseHeaders:
396+
X-Rate-Limit-Limit:
397+
description: The number of allowed requests in the current period
398+
schema:
399+
type: integer
400+
X-Rate-Limit-Remaining:
401+
description: The number of remaining requests in the current period
402+
schema:
403+
type: integer
404+
X-Rate-Limit-Reset:
405+
description: The number of seconds left in the current period
406+
schema:
407+
type: integer
380408
```
381409

382410
##### `responseModels`
@@ -389,27 +417,16 @@ responseModels:
389417
application/xml: "CreateResponseXML"
390418
```
391419

392-
##### `responseHeaders` and `requestHeaders`
393-
394-
The `responseHeaders/requestHeaders` section of the configuration allows you to define the HTTP headers for the function event.
420+
##### `responseHeaders`
395421

396-
The attributes for a header are as follow:
397-
398-
* `name`: the name of the HTTP Header
399-
* `description`: a description of the HTTP Header
400-
* `schema`: JSON schema (inline, file or externally hosted)
422+
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).
401423

402424
```yml
403425
responseHeaders:
404-
- name: "Content-Type"
405-
description: "Content Type header"
426+
X-Rate-Limit-Limit:
427+
description: The number of allowed requests in the current period
406428
schema:
407-
type: "string"
408-
requestHeaders:
409-
- name: "Content-Type"
410-
description: "Content Type header"
411-
schema:
412-
type: "string"
429+
type: integer
413430
```
414431

415432
## Example configuration

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.25",
3+
"version": "0.0.26",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

src/definitionGenerator.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,41 @@ class DefinitionGenerator {
271271
throw err
272272
})
273273

274+
if (response.responseHeaders) {
275+
obj.headers = await this.createResponseHeaders(response.responseHeaders)
276+
.catch(err => {
277+
throw err
278+
})
279+
}
280+
274281
Object.assign(responses,{[response.statusCode]: obj})
275282
}
276283

277284
return responses
278285
}
279286

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+
280309
async createRequestBody(documentation) {
281310
const obj = {
282311
description: documentation.requestBody.description,

0 commit comments

Comments
 (0)