Skip to content

Commit a21af36

Browse files
authored
Merge pull request #105 from JaredCE/add-cors
Add CORS
2 parents 2a9f4f2 + ae2e302 commit a21af36

4 files changed

Lines changed: 83 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ functions:
403403
- http:
404404
path: create
405405
method: post
406+
cors: true
406407
summary:
407408
documentation:
408409
summary: "Create User"
@@ -645,6 +646,12 @@ responseHeaders:
645646
type: integer
646647
```
647648

649+
###### CORS
650+
651+
You can automatically generate CORS response headers by setting `cors` at the function level. Serverless allows you to modify how CORS is setup, so you can have the default options with `cors: true`, or you can modify the settings as shown in the [serverless documentation for CORS](https://www.serverless.com/framework/docs/providers/aws/events/apigateway#enabling-cors).
652+
653+
The generator will interpret your settings for CORS and automatically add the response headers. If for whatever reason you wish to override these, you can set them via the above `responseHeaders` setting and it'll apply your overrides.
654+
648655
## Example configuration
649656

650657
Please view the example [serverless.yml](test/serverless-tests/serverless%202/serverless.yml).

‎package-lock.json‎

Lines changed: 2 additions & 2 deletions
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.50",
3+
"version": "0.0.51",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

‎src/definitionGenerator.js‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,24 @@ class DefinitionGenerator {
3535
securitySchemes: 'securitySchemes'
3636
}
3737

38+
this.DEFAULT_CORS_HEADERS = {
39+
'Access-Control-Allow-Origin': {
40+
description: 'The Access-Control-Allow-Origin response header indicates whether the response can be shared with requesting code from the given origin.',
41+
schema: {
42+
type: 'string',
43+
default: '*',
44+
example: 'https://developer.mozilla.org'
45+
}
46+
},
47+
'Access-Control-Allow-Credentials': {
48+
description: `The Access-Control-Allow-Credentials response header tells browsers whether to expose the response to the frontend JavaScript code when the request's credentials mode (Request.credentials) is include`,
49+
schema: {
50+
type: 'boolean',
51+
default: true
52+
}
53+
}
54+
}
55+
3856
try {
3957
this.refParserOptions = require(path.resolve('options', 'ref-parser.js'))
4058
} catch (err) {
@@ -123,6 +141,7 @@ class DefinitionGenerator {
123141
for (const httpFunction of httpFunctions) {
124142
for (const event of httpFunction.event) {
125143
if (event?.http?.documentation || event?.httpApi?.documentation) {
144+
this.currentEvent = event?.http || event?.httpApi
126145
const documentation = event?.http?.documentation || event?.httpApi?.documentation
127146

128147
this.currentFunctionName = httpFunction.functionInfo.name
@@ -328,14 +347,68 @@ class DefinitionGenerator {
328347
})
329348
}
330349

350+
351+
const corsHeaders = await this.corsHeaders()
352+
.catch(err => {
353+
throw err;
354+
})
355+
356+
if (obj.headers) {
357+
for (const key in corsHeaders) {
358+
if (!(key in obj.headers) && (obj.headers[key] = {})) {
359+
obj.headers[key] = corsHeaders[key]
360+
}
361+
}
362+
} else {
363+
obj.headers = corsHeaders
364+
}
365+
331366
Object.assign(responses,{[response.statusCode]: obj})
332367
}
333368

334369
return responses
335370
}
336371

372+
async corsHeaders() {
373+
let headers = {}
374+
if (this.currentEvent?.cors === true) {
375+
headers = await this.createResponseHeaders(this.DEFAULT_CORS_HEADERS)
376+
.catch(err => {
377+
throw err;
378+
})
379+
} else if (this.currentEvent.cors) {
380+
const newHeaders = {}
381+
for (const key of Object.keys(this.DEFAULT_CORS_HEADERS)) {
382+
if (key === 'Access-Control-Allow-Credentials' &&
383+
this.currentEvent.cors.allowCredentials === undefined || this.currentEvent.cors?.allowCredentials === false) {
384+
continue
385+
}
386+
387+
const obj = JSON.parse(JSON.stringify(this.DEFAULT_CORS_HEADERS[key]))
388+
389+
if (key === 'Access-Control-Allow-Origin') {
390+
if (this.currentEvent.cors?.origins || this.currentEvent.cors?.origin) {
391+
obj.schema.example = this.currentEvent.cors?.origins?.toString() || this.currentEvent.cors?.origin?.toString()
392+
} else if (this.currentEvent.cors?.allowedOrigins) {
393+
obj.schema.example = this.currentEvent.cors.allowedOrigins.toString()
394+
}
395+
}
396+
397+
Object.assign(newHeaders, {[key]: obj})
398+
}
399+
400+
headers = await this.createResponseHeaders(newHeaders)
401+
.catch(err => {
402+
throw err;
403+
})
404+
}
405+
406+
return headers;
407+
}
408+
337409
async createResponseHeaders(headers) {
338410
const obj = {}
411+
339412
for (const header of Object.keys(headers)) {
340413
const newHeader = {}
341414
newHeader.description = headers[header].description || ''

0 commit comments

Comments
 (0)