-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from yndlingsfar/feature/resonse-params
add responseParameters template
- Loading branch information
Showing
8 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '''*''' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: '''*''' |