Repository: HyperBrain/serverless-aws-alias (js)

Issue:
Deploying an alias with authorizers overwrites dev alias.
Example:
Deploy a "newAlias" with an existing "dev" alias. The Authorizers of type "REQUEST" will overwrite the existing "dev" alias authorizers. 

Appending a stage variable as a suffix to the lambda for authorizer should fix the situation.

Workaround appears to be going into API Gateway -> Instance -> Authorizers -> Edit authorizer and save.

Pull request description:
Added support to append ${stageVariables.SERVERLESS_ALIAS} to authorizers of type REQUEST #96
Closes #96 

Added support for authorizers of type **REQUEST** to have ${stageVariables.SERVERLESS_ALIAS} appended to the arn. Currently this is only happening for authorizers that are of type **TOKEN**.

Patch:
diff --git a/lib/stackops/apiGateway.js b/lib/stackops/apiGateway.js
index 37b7918..8d7f218 100644
--- a/lib/stackops/apiGateway.js
+++ b/lib/stackops/apiGateway.js
@@ -222,7 +222,8 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac
 
 		// Audjust authorizer Uri and name (stage variables are not allowed in Uris here)
 		_.forOwn(authorizers, (authorizer, name) => {
-			if (_.get(authorizer, 'Properties.Type') === 'TOKEN') {
+			const authorizerType = _.get(authorizer, 'Properties.Type');
+			if (authorizerType === 'TOKEN' || authorizerType === 'REQUEST') {
 				const uriParts = authorizer.Properties.AuthorizerUri['Fn::Join'][1];
 				const funcIndex = _.findIndex(uriParts, part => _.has(part, 'Fn::GetAtt'));
 


Test patch:
diff --git a/test/data/auth-stack.json b/test/data/auth-stack.json
index 994543d..af00237 100644
--- a/test/data/auth-stack.json
+++ b/test/data/auth-stack.json
@@ -289,6 +289,37 @@
         "Type": "TOKEN"
       }
     },
+    "TestauthApiGatewayRequestAuthorizer": {
+      "Type": "AWS::ApiGateway::Authorizer",
+      "Properties": {
+        "AuthorizerResultTtlInSeconds": 0,
+        "IdentitySource": "method.request.header.Authorization",
+        "Name": "testauthrequest",
+        "RestApiId": {
+          "Ref": "ApiGatewayRestApi"
+        },
+        "AuthorizerUri": {
+          "Fn::Join": [
+            "",
+            [
+              "arn:aws:apigateway:",
+              {
+                "Ref": "AWS::Region"
+              },
+              ":lambda:path/2015-03-31/functions/",
+              {
+                "Fn::GetAtt": [
+                  "TestauthLambdaFunction",
+                  "Arn"
+                ]
+              },
+              "/invocations"
+            ]
+          ]
+        },
+        "Type": "REQUEST"
+      }
+    },
     "CognitoTestApiGatewayAuthorizer": {
       "Type": "AWS::ApiGateway::Authorizer",
       "Properties": {
diff --git a/test/stackops/apiGateway.test.js b/test/stackops/apiGateway.test.js
index b824471..8efee40 100644
--- a/test/stackops/apiGateway.test.js
+++ b/test/stackops/apiGateway.test.js
@@ -622,6 +622,26 @@ describe('API Gateway', () => {
 			});
 
 			it('should handle only Lambda authorizers', () => {
+				const authorizeUriTemplate = {
+					"Fn::Join": [
+						"",
+						[
+							"arn:aws:apigateway:",
+							{
+								"Ref": "AWS::Region"
+							},
+							":lambda:path/2015-03-31/functions/",
+							{
+								"Fn::GetAtt": [
+									"TestauthLambdaFunction",
+									"Arn"
+								]
+							},
+							":${stageVariables.SERVERLESS_ALIAS}",
+							"/invocations"
+						]
+					]
+				};
 				const template = serverless.service.provider.compiledCloudFormationTemplate = stackTemplate;
 				const cogAuth = _.cloneDeep(template.Resources.CognitoTestApiGatewayAuthorizer);
 				cogAuth.Properties.Name += "-myAlias";
@@ -629,28 +649,13 @@ describe('API Gateway', () => {
 				return expect(awsAlias.aliasHandleApiGateway({}, [], {})).to.be.fulfilled
 				.then(() => BbPromise.all([
 					expect(template).to.not.have.a.nested.property('Resources.TestauthApiGatewayAuthorizer'),
+					expect(template).to.not.have.a.nested.property('Resources.TestauthApiGatewayRequestAuthorizer'),
 					expect(template).to.have.a.nested.property('Resources.TestauthApiGatewayAuthorizermyAlias')
 						.that.has.a.nested.property("Properties.AuthorizerUri")
-						.that.deep.equals({
-							"Fn::Join": [
-								"",
-								[
-									"arn:aws:apigateway:",
-									{
-										"Ref": "AWS::Region"
-									},
-									":lambda:path/2015-03-31/functions/",
-									{
-										"Fn::GetAtt": [
-											"TestauthLambdaFunction",
-											"Arn"
-										]
-									},
-									":${stageVariables.SERVERLESS_ALIAS}",
-									"/invocations"
-								]
-							]
-						}),
+						.that.deep.equals(authorizeUriTemplate),
+					expect(template).to.have.a.nested.property('Resources.TestauthApiGatewayRequestAuthorizermyAlias')
+						.that.has.a.nested.property("Properties.AuthorizerUri")
+						.that.deep.equals(authorizeUriTemplate),
 					expect(template).to.have.a.nested.property('Resources.CognitoTestApiGatewayAuthorizermyAlias')
 						.that.deep.equals(cogAuth)
 				]));