Repository: HyperBrain/serverless-aws-alias
Pull request #81 (2017-11-21 10:48:23)

Support ANY type endpoints with stage configuration
Fixes: #80

According to AWS, stage configurations have to refer to '*' to catch all methods or paths.

In case of endpoints defined with `ANY` as method, we have to use `*` as method.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14302862/badge)](https://coveralls.io/builds/14302862)

Coverage decreased (-0.08%) to 74.224% when pulling **7cec90bbae88c2ce77baad556e97a393845b34ae on support-any-in-stage-config** into **e0215d2694b743567e58a48746129b33af13cc01 on master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/14303137/badge)](https://coveralls.io/builds/14303137)

Coverage increased (+0.09%) to 74.397% when pulling **b0f92954513740885cb62272b6b8255d3e0605a7 on support-any-in-stage-config** into **e0215d2694b743567e58a48746129b33af13cc01 on master**.


Patch:
diff --git a/.gitignore b/.gitignore
index 7bbe1a3..0fd21d3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 /node_modules
 /coverage
+/.nyc_output
diff --git a/lib/stackops/apiGateway.js b/lib/stackops/apiGateway.js
index 181d5ad..d497aa0 100644
--- a/lib/stackops/apiGateway.js
+++ b/lib/stackops/apiGateway.js
@@ -80,6 +80,16 @@ const internal = {
 				if (!_.isEmpty(eventStageConfig)) {
 					const methodType = _.toUpper(httpEvent.method);
 					const methodSetting = {};
+					const methods = methodType === 'ANY' ? [
+						'DELETE',
+						'GET',
+						'HEAD',
+						'OPTIONS',
+						'PATCH',
+						'POST',
+						'PUT'
+					]: [ methodType ];
+
 					_.forOwn(eventStageConfig, (value, key) => {
 						if (!_.has(stageMethodConfigMappings, key)) {
 							throw new this.serverless.classes.Error(`Invalid stage config '${key}' at method '${methodType} /${httpEvent.path}'`);
@@ -91,9 +101,11 @@ const internal = {
 						}
 					});
 					if (!_.isEmpty(methodSetting)) {
-						methodSetting.HttpMethod = methodType;
 						methodSetting.ResourcePath = '/' + _.replace('/' + _.trimStart(httpEvent.path, '/'), /\//g, '~1');
-						methodSettings.push(methodSetting);
+						_.forEach(methods, method => {
+							methodSetting.HttpMethod = method;
+							methodSettings.push(_.clone(methodSetting));
+						});
 					}
 				}
 			});


Test patch:
diff --git a/test/stackops/apiGateway.test.js b/test/stackops/apiGateway.test.js
index 4e5a859..3b9b450 100644
--- a/test/stackops/apiGateway.test.js
+++ b/test/stackops/apiGateway.test.js
@@ -223,6 +223,17 @@ describe('API Gateway', () => {
 							}
 						]
 					},
+					functionC: {
+						handler: 'functionB.handler',
+						events: [
+							{
+								http: {
+									method: 'ANY',
+									path: '/funcC'
+								}
+							},
+						]
+					},
 				}
 			};
 			const expectedMethodSettings = [
@@ -246,6 +257,41 @@ describe('API Gateway', () => {
 					HttpMethod: 'UPDATE',
 					ResourcePath: '/~1funcB~1update'
 				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'DELETE',
+					ResourcePath: '/~1funcC'
+				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'GET',
+					ResourcePath: '/~1funcC'
+				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'HEAD',
+					ResourcePath: '/~1funcC'
+				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'OPTIONS',
+					ResourcePath: '/~1funcC'
+				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'PATCH',
+					ResourcePath: '/~1funcC'
+				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'POST',
+					ResourcePath: '/~1funcC'
+				},
+				{
+					LoggingLevel: 'INFO',
+					HttpMethod: 'PUT',
+					ResourcePath: '/~1funcC'
+				},
 			];
 
 			awsAlias.serverless.service = new awsAlias.serverless.classes.Service(awsAlias.serverless, service);