serverless-reqvalidator-plugin
Advanced tools
Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "serverless-reqvalidator-plugin", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Serverless plugin for setting request validation", | ||
@@ -19,2 +19,5 @@ "main": "src/index.js", | ||
"author": "RafPe < me@rafpe.ninja >", | ||
"contributors": [ | ||
"pj035 pm.jaecks@gmail.com (http://pjaecks.de)" | ||
], | ||
"license": "MIT", | ||
@@ -21,0 +24,0 @@ "bugs": { |
# serverless-reqvalidator-plugin | ||
Serverless plugin to set specific validator request on method | ||
# Installation | ||
## Installation | ||
``` | ||
@@ -9,7 +9,15 @@ npm install serverless-reqvalidator-plugin | ||
# Using plugin | ||
## Requirements | ||
This require you to have documentation plugin installed | ||
``` | ||
serverless-aws-documentation | ||
``` | ||
## Using plugin | ||
Specify plugin | ||
``` | ||
plugins: | ||
- serverless-reqvalidator-plugin | ||
- serverless-reqvalidator-plugin | ||
- serverless-aws-documentation | ||
``` | ||
@@ -46,2 +54,53 @@ | ||
### Use Validator specified in different Stack | ||
The serverless framework allows us to share resources among several stacks. Therefore a CloudFormation Output has to be specified in one stack. This Output can be imported in another stack to make use of it. For more information see | ||
[here](https://serverless.com/framework/docs/providers/aws/guide/variables/#reference-cloudformation-outputs). | ||
Specify a request validator in a different stack: | ||
``` | ||
plugins: | ||
- serverless-reqvalidator-plugin | ||
service: my-service-a | ||
functions: | ||
hello: | ||
handler: handler.myHandler | ||
events: | ||
- http: | ||
path: hello | ||
reqValidatorName: 'myReqValidator' | ||
resources: | ||
Resource: | ||
xMyRequestValidator: | ||
Type: "AWS::ApiGateway::RequestValidator" | ||
Properties: | ||
Name: 'my-req-validator' | ||
RestApiId: | ||
Ref: ApiGatewayRestApi | ||
ValidateRequestBody: true | ||
ValidateRequestParameters: false | ||
Outputs: | ||
xMyRequestValidator: | ||
Value: | ||
Ref: my-req-validator | ||
Export: | ||
Name: myReqValidator | ||
``` | ||
Make use of the exported request validator in stack b: | ||
``` | ||
plugins: | ||
- serverless-reqvalidator-plugin | ||
service: my-service-b | ||
functions: | ||
hello: | ||
handler: handler.myHandler | ||
events: | ||
- http: | ||
path: hello | ||
reqValidatorName: | ||
Fn::ImportValue: 'myReqValidator' | ||
``` |
@@ -19,2 +19,15 @@ 'use strict'; | ||
* | ||
* Alternative usage: | ||
* | ||
* myFuncGetItem: | ||
* handler: myFunc.get | ||
* name: ${self:provider.stage}-myFunc-get-item | ||
* events: | ||
* - http: | ||
* method: GET | ||
* path: mypath | ||
* cors: true | ||
* reqValidatorName: | ||
* Fn::ImportValue: 'my-import-value' | ||
* | ||
* Resources used: | ||
@@ -30,6 +43,6 @@ * - https://www.snip2code.com/Snippet/1467589/adds-the-posibility-to-configure-AWS_IAM/ | ||
this.provider = this.serverless.getProvider('aws'); | ||
const naming = this.serverless.providers.aws.naming; | ||
const naming = this.serverless.providers.aws.naming; | ||
this.getMethodLogicalId = naming.getMethodLogicalId.bind(naming); | ||
this.normalizePath = naming.normalizePath.bind(naming); | ||
this.normalizePath = naming.normalizePath.bind(naming); | ||
@@ -51,9 +64,9 @@ this._beforeDeploy = this.beforeDeploy.bind(this) | ||
functionObject.events.forEach( event => { | ||
functionObject.events.forEach(event => { | ||
if (!event.http) {return;} | ||
if (!event.http) { return; } | ||
const reqValidatorName = event.http.reqValidatorName; | ||
if (reqValidatorName) { | ||
if (event.http.reqValidatorName) { | ||
let path; | ||
@@ -63,23 +76,33 @@ let method; | ||
if (typeof event.http === 'object') { | ||
path = event.http.path; | ||
path = event.http.path; | ||
method = event.http.method; | ||
} else if (typeof event.http === 'string') { | ||
path = event.http.split(' ')[1]; | ||
path = event.http.split(' ')[1]; | ||
method = event.http.split(' ')[0]; | ||
} | ||
const resourcesArray = path.split('/'); | ||
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 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}`; | ||
const normalizedMethod = method[0].toUpperCase() + method.substr(1).toLowerCase(); | ||
const methodName = `ApiGatewayMethod${normalizedResourceName}${normalizedMethod}`; | ||
resources[methodName].Properties.RequestValidatorId = {"Ref": `${event.http.reqValidatorName}`}; | ||
switch (typeof reqValidatorName) { | ||
case 'object': | ||
if (reqValidatorName['Fn::ImportValue']) { | ||
resources[methodName].Properties.RequestValidatorId = reqValidatorName; | ||
} else { // other use cases should be added here | ||
resources[methodName].Properties.RequestValidatorId = reqValidatorName; | ||
} | ||
break; | ||
case 'string': | ||
default: | ||
resources[methodName].Properties.RequestValidatorId = { "Ref": `${reqValidatorName}` }; | ||
break; | ||
} | ||
} | ||
}); | ||
} | ||
) | ||
) | ||
} | ||
@@ -86,0 +109,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6690
4
88
104