Skip to content

Commit

Permalink
Merge pull request #4 from yndlingsfar/feature/resonse-params
Browse files Browse the repository at this point in the history
add responseParameters template
  • Loading branch information
yndlingsfar authored May 5, 2021
2 parents 256da16 + 6c251aa commit 6a1fe8f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 2 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## 1.3.0 (2021-05-05)
- additional template and auto-generation for responseParameters if cors support enabled

## 1.2.5 (2021-04-20)
- Updated Readme

## 1.2.4 (2021-04-20)
- Updated Readme

## 1.2.3 (2021-04-20)
- Updated Readme

## 1.2.2 (2021-04-20)
- Updated Readme

## 1.2.0 (2021-04-19)
- added support for CORS generation

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ You can customize the CORS templates by placing your own files inside a director
| headers.yml | All headers required for CORS support |
| integration.yml | Contains the x-amazon-apigateway-integration block |
| path.yml| OpenApi specification for the OPTIONS method |
| response-parameters.yml| The response Parameters of the x-amazon-apigateway-integration responses |

See the EXAMPLES directory for detailed instructions.

Expand Down
3 changes: 3 additions & 0 deletions examples/openapi-integration/response-parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
method.response.header.Access-Control-Allow-Headers: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key'''
method.response.header.Access-Control-Allow-Methods: '''*'''
method.response.header.Access-Control-Allow-Origin: '''*'''
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const AddCorsHeader = require('./pipelines/add-cors-header')
const WriteOutputFile = require('./pipelines/write-file-output')
const AddIntegrations = require('./pipelines/add-integration')
const AddCorsIntegration = require('./pipelines/add-cors-integrations')
const AddCorsResponseParameters = require('./pipelines/add-cors-response-parameters')
const Pipeline = require('./pipeline')

class MergeIntegrationPlugin {
Expand Down Expand Up @@ -66,6 +67,7 @@ class MergeIntegrationPlugin {
.step(new AddCorsMethods())
.step(new AddCorsHeader())
.step(new AddCorsIntegration())
.step(new AddCorsResponseParameters())
.step(new WriteOutputFile())
.end()

Expand Down
1 change: 0 additions & 1 deletion lib/options-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class OptionsResolver {

return false;
}
console.log(this.options.cors)
return this.options.cors && (this.options.cors === true || this.options.cors === "true")
}

Expand Down
52 changes: 52 additions & 0 deletions lib/pipelines/add-cors-response-parameters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const jsYml = require('js-yaml');
const fs = require('fs');
const path = require('path');
const merge = require('lodash.merge')

class AddCorsResponseParameters {
invoke(options, content, serverless) {
try {
if (!options.cors) {
return content;
}

const cors = this.readTemplate(serverless);
Object.entries(content.paths).forEach(([key, pathContent]) => {
Object.entries(pathContent).forEach(([method, methodContent]) => {
if ((methodContent.hasOwnProperty('x-amazon-apigateway-integration'))) {
Object.entries(methodContent['x-amazon-apigateway-integration'].responses).forEach(([responses, responseContent]) => {
if (!responseContent.hasOwnProperty('responseParameters')) {
let params = {responseParameters: cors};
Object.assign(responseContent, merge(responseContent, params));
} else {
Object.assign(responseContent.responseParameters, merge(responseContent.responseParameters, cors));
}
});
}
});
});

return content;
} catch (e) {
console.log(e);
}
return content
}

readTemplate(serverless) {
const templatePath = path.resolve(process.cwd(), 'openapi-integration/response-parameters.yml')
try {
if (fs.existsSync(templatePath)) {
serverless.cli.log(`Openapi Integration: add custom CORS integration template`);
return jsYml.load(fs.readFileSync(templatePath))
}
} catch (err) {
console.error(err)
}

serverless.cli.log(`Openapi Integration: add default CORS integration template`);
return jsYml.load(fs.readFileSync(__dirname + '/../../templates/cors/response-parameters.yml'));
}
}

module.exports = AddCorsResponseParameters
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"serverless-plugin",
"x-amazon-apigateway-integration"
],
"version": "1.2.5",
"version": "1.3.0",
"description": "Serverless plugin for integrating aws gateway syntax into open api specification",
"main": "lib/index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions templates/cors/response-parameters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
method.response.header.Access-Control-Allow-Headers: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key'''
method.response.header.Access-Control-Allow-Methods: '''*'''
method.response.header.Access-Control-Allow-Origin: '''*'''

0 comments on commit 6a1fe8f

Please sign in to comment.