|
1 | | -const isEmpty = require('lodash.isempty'); |
| 1 | +const split = require('lodash.split'); |
2 | 2 |
|
3 | | -const getResourcesByType = (type, serverless) => { |
4 | | - let result = [] |
| 3 | +const getResourcesByName = (name, serverless) => { |
5 | 4 | let resourceKeys = Object.keys(serverless.service.provider.compiledCloudFormationTemplate.Resources); |
6 | 5 | for (let resourceName of resourceKeys) { |
7 | | - let resource = serverless.service.provider.compiledCloudFormationTemplate.Resources[resourceName]; |
8 | | - if (resource.Type == type) { |
9 | | - result.push({ name: resourceName, resource }); |
| 6 | + if (resourceName == name) { |
| 7 | + return serverless.service.provider.compiledCloudFormationTemplate.Resources[resourceName]; |
10 | 8 | } |
11 | 9 | } |
12 | | - return result; |
13 | 10 | } |
14 | 11 |
|
15 | | -const getResourceForLambdaFunctionNamed = (fullFunctionName, serverless) => { |
16 | | - let lambdaResource = getResourcesByType('AWS::Lambda::Function', serverless).filter(r => r.resource.Properties.FunctionName == fullFunctionName); |
17 | | - if (isEmpty(lambdaResource)) { |
18 | | - throw new Error('Something has gone wrong'); |
19 | | - } |
20 | | - return lambdaResource[0]; |
21 | | -} |
22 | | - |
23 | | -const getApiGatewayMethodFor = (functionName, stage, serverless) => { |
24 | | - const fullFunctionName = `${serverless.service.service}-${stage}-${functionName}`; |
25 | | - const lambdaFunctionResource = getResourceForLambdaFunctionNamed(fullFunctionName, serverless); |
| 12 | +const getApiGatewayMethodNameFor = (path, httpMethod) => { |
| 13 | + const pathElements = split(path,'/'); |
| 14 | + pathElements.push(httpMethod.toLowerCase()); |
| 15 | + let gatewayResourceName = pathElements |
| 16 | + .map (element => { |
| 17 | + element = element.toLowerCase(); |
| 18 | + if(element.startsWith('{')) { |
| 19 | + element = element.substring(element.indexOf('{') + 1,element.indexOf('}')) + "Var"; |
| 20 | + } |
| 21 | + //capitalize first letter |
| 22 | + return element.charAt(0).toUpperCase() + element.slice(1); |
| 23 | + }).reduce((a, b) => a + b); |
26 | 24 |
|
27 | | - // returns the first method found which depends on this lambda |
28 | | - const methods = getResourcesByType('AWS::ApiGateway::Method', serverless); |
29 | | - for (let method of methods) { |
30 | | - let stringified = JSON.stringify(method); |
31 | | - if (stringified.lastIndexOf(`"${lambdaFunctionResource.name}"`) != -1) { |
32 | | - return method; |
33 | | - } |
34 | | - } |
| 25 | + gatewayResourceName = "ApiGatewayMethod" + gatewayResourceName; |
| 26 | + return gatewayResourceName; |
35 | 27 | } |
36 | 28 |
|
37 | 29 | const addPathParametersCacheConfig = (settings, serverless) => { |
38 | 30 | for (let endpointSettings of settings.endpointSettings) { |
39 | 31 | if (!endpointSettings.cacheKeyParameters) { |
40 | 32 | continue; |
| 33 | + } |
| 34 | + const resourceName = getApiGatewayMethodNameFor(endpointSettings.path, endpointSettings.method); |
| 35 | + const method = getResourcesByName(resourceName,serverless); |
| 36 | + if (!method) { |
| 37 | + serverless.cli.log(`[serverless-api-gateway-caching] The method ${resourceName} couldn't be found in the |
| 38 | + compiled CloudFormation template. Caching settings will not be updated for this endpoint.`); |
| 39 | + const index = settings.endpointSettings.indexOf(endpointSettings); |
| 40 | + if(index != -1) { |
| 41 | + settings.endpointSettings.splice(index,1); |
| 42 | + } |
| 43 | + return; |
41 | 44 | } |
42 | | - const method = getApiGatewayMethodFor(endpointSettings.functionName, settings.stage, serverless); |
43 | | - if (!method.resource.Properties.Integration.CacheKeyParameters) { |
44 | | - method.resource.Properties.Integration.CacheKeyParameters = []; |
| 45 | + if (!method.Properties.Integration.CacheKeyParameters) { |
| 46 | + method.Properties.Integration.CacheKeyParameters = []; |
45 | 47 | } |
46 | | - if (!method.resource.Properties.Integration.RequestParameters) { |
47 | | - method.resource.Properties.Integration.RequestParameters = {} |
| 48 | + if (!method.Properties.Integration.RequestParameters) { |
| 49 | + method.Properties.Integration.RequestParameters = {} |
48 | 50 | } |
49 | 51 |
|
50 | 52 | for (let cacheKeyParameter of endpointSettings.cacheKeyParameters) { |
51 | | - let existingValue = method.resource.Properties.RequestParameters[`method.${cacheKeyParameter.name}`]; |
52 | | - method.resource.Properties.RequestParameters[`method.${cacheKeyParameter.name}`] = (existingValue == null || existingValue == undefined) ? {} : existingValue; |
53 | | - method.resource.Properties.Integration.RequestParameters[`integration.${cacheKeyParameter.name}`] = `method.${cacheKeyParameter.name}`; |
54 | | - method.resource.Properties.Integration.CacheKeyParameters.push(`method.${cacheKeyParameter.name}`); |
| 53 | + let existingValue = method.Properties.RequestParameters[`method.${cacheKeyParameter.name}`]; |
| 54 | + method.Properties.RequestParameters[`method.${cacheKeyParameter.name}`] = (existingValue == null || existingValue == undefined) ? {} : existingValue; |
| 55 | + method.Properties.Integration.RequestParameters[`integration.${cacheKeyParameter.name}`] = `method.${cacheKeyParameter.name}`; |
| 56 | + method.Properties.Integration.CacheKeyParameters.push(`method.${cacheKeyParameter.name}`); |
55 | 57 | } |
56 | | - method.resource.Properties.Integration.CacheNamespace = `${method.name}CacheNS`; |
| 58 | + method.Properties.Integration.CacheNamespace = `${resourceName}CacheNS`; |
57 | 59 | } |
58 | 60 | } |
59 | 61 |
|
60 | | -module.exports = addPathParametersCacheConfig; |
| 62 | +module.exports = { |
| 63 | + addPathParametersCacheConfig: addPathParametersCacheConfig, |
| 64 | + getApiGatewayMethodNameFor: getApiGatewayMethodNameFor |
| 65 | +} |
0 commit comments