serverless-reqvalidator-plugin
Advanced tools
Comparing version 1.0.0 to 1.0.1
{ | ||
"name": "serverless-reqvalidator-plugin", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Serverless plugin for setting request validation", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
'use strict'; | ||
/** | ||
* | ||
* Adds the posibility to configure AWS_IAM for your API Gateway endpoints | ||
* and "Invoke with caller credentials" | ||
* Adds using of custom created request validator on specific functions by | ||
* adding `reqValidatorName` | ||
* | ||
@@ -23,2 +22,3 @@ * Usage: | ||
*/ | ||
class ServerlessReqValidatorPlugin { | ||
@@ -33,3 +33,3 @@ constructor(serverless, options) { | ||
this.getMethodLogicalId = naming.getMethodLogicalId.bind(naming); | ||
this.normalizePath = naming.normalizePath.bind(naming); | ||
this.normalizePath = naming.normalizePath.bind(naming); | ||
@@ -42,56 +42,47 @@ this._beforeDeploy = this.beforeDeploy.bind(this) | ||
this.documentationParts = []; | ||
} | ||
beforeDeploy() { | ||
if (!(this.serverless.service.custom['aaa'] && this.serverless.service.custom )) return; | ||
let validatorsMap = this.serverless.service.custom['aaa'] | ||
console.log( JSON.stringify(validatorsMap) ) | ||
console.log('-----------------') | ||
console.log( JSON.stringify(this.serverless.service.functions) ) | ||
console.log('-----------------') | ||
const resources = this.serverless.service.provider.compiledCloudFormationTemplate.Resources | ||
console.log('Begin Attach plugin...') | ||
// Filter for any IAM Roles defined in CFT and apply our Managed Policies. | ||
// && resources[resourceName].Properties.HttpMethod === 'POST' | ||
Object.keys(resources) | ||
.filter(resourceName => resources[resourceName].Type === 'AWS::ApiGateway::Method') | ||
.forEach(roleResource => | ||
{ | ||
this.serverless.service.getAllFunctions().forEach((functionName) => { | ||
const functionObject = this.serverless.service.functions[functionName]; | ||
functionObject.events.forEach( event => { | ||
// Here we iterate every method | ||
console.log(JSON.stringify(resources[roleResource])) | ||
resources[roleResource].Properties.RequestValidatorId = {"Ref":"xMyRequestValidator"}; | ||
console.log(JSON.stringify(resources[roleResource])) | ||
} | ||
) | ||
if (!event.http) {return;} | ||
if (event.http.reqValidatorName) { | ||
var xcfTemplate = this.serverless.service.provider.compiledCloudFormationTemplate; | ||
let path; | ||
let method; | ||
//console.log(JSON.stringify(xcfTemplate)) | ||
// Add models to method resources | ||
this.serverless.service.getAllFunctions() | ||
.forEach(functionName => | ||
{ | ||
//console.log(`This is ${functionName}`) | ||
if (typeof event.http === 'object') { | ||
path = event.http.path; | ||
method = event.http.method; | ||
} else if (typeof event.http === 'string') { | ||
path = event.http.split(' ')[1]; | ||
method = event.http.split(' ')[0]; | ||
} | ||
// const func = this.serverless.service.getFunction(functionName); | ||
// func.events.forEach(this.updateCfTemplateFromHttp.bind(this)); | ||
} | ||
); | ||
const resourcesArray = path.split('/'); | ||
// resource name is the last element in the endpoint. It's not unique. | ||
const resourceName = path.split('/')[path.split('/').length - 1]; | ||
const normalizedResourceName = resourcesArray.map(this.normalizePath).join(''); | ||
const normalizedMethod = method[0].toUpperCase() + method.substr(1).toLowerCase(); | ||
const methodName = `ApiGatewayMethod${normalizedResourceName}${normalizedMethod}`; | ||
resources[methodName].Properties.RequestValidatorId = {"Ref": `${event.http.reqValidatorName}`}; | ||
} | ||
}); | ||
} | ||
) | ||
} | ||
} | ||
} | ||
module.exports = ServerlessReqValidatorPlugin; |
4123
62