Skip to content

Commit 25e3417

Browse files
author
Diana Ionita
committed
Merge branch 'release/1.7.2'
2 parents 010310d + 058f950 commit 25e3417

File tree

8 files changed

+118
-37
lines changed

8 files changed

+118
-37
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ custom:
246246
enabled: true
247247
248248
functions:
249-
# Cache responses for POST requests based on the whole request body
249+
# Cache responses for POST requests based on a part of the request body
250250
cats-graphql:
251251
handler: graphql/handler.handle
252252
events:

package-lock.json

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-api-gateway-caching",
3-
"version": "1.7.1",
3+
"version": "1.7.2",
44
"description": "A plugin for the serverless framework which helps with configuring caching for API Gateway endpoints.",
55
"main": "src/apiGatewayCachingPlugin.js",
66
"scripts": {
@@ -19,8 +19,7 @@
1919
"license": "ISC",
2020
"dependencies": {
2121
"lodash.get": "^4.4.2",
22-
"lodash.isempty": "^4.4.0",
23-
"lodash.split": "^4.4.0"
22+
"lodash.isempty": "^4.4.0"
2423
},
2524
"bugs": {
2625
"url": "https://github.com/DianaIonita/serverless-api-gateway-caching/issues"
@@ -34,6 +33,7 @@
3433
"chance": "^1.0.16",
3534
"mocha": "^8.1.3",
3635
"mocha-junit-reporter": "^1.18.0",
37-
"proxyquire": "^2.1.0"
36+
"proxyquire": "^2.1.0",
37+
"lodash.split": "^4.4.0"
3838
}
3939
}

src/ApiGatewayCachingSettings.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ const isApiGatewayEndpoint = event => {
2222
return event.http ? true : false;
2323
}
2424

25+
const getApiGatewayResourceNameFor = (path, httpMethod) => {
26+
const pathElements = path.split('/');
27+
pathElements.push(httpMethod.toLowerCase());
28+
let gatewayResourceName = pathElements
29+
.map(element => {
30+
element = element.toLowerCase();
31+
element = element.replaceAll('+', '');
32+
element = element.replaceAll('_', '');
33+
element = element.replaceAll('.', '');
34+
element = element.replaceAll('-', 'Dash');
35+
if (element.startsWith('{')) {
36+
element = element.substring(element.indexOf('{') + 1, element.indexOf('}')) + "Var";
37+
}
38+
//capitalize first letter
39+
return element.charAt(0).toUpperCase() + element.slice(1);
40+
}).reduce((a, b) => a + b);
41+
42+
gatewayResourceName = "ApiGatewayMethod" + gatewayResourceName;
43+
return gatewayResourceName;
44+
}
45+
2546
class PerKeyInvalidationSettings {
2647
constructor(cachingSettings) {
2748
let { perKeyInvalidation } = cachingSettings;
@@ -57,6 +78,8 @@ class ApiGatewayEndpointCachingSettings {
5778
this.path = this.path.slice(0, -1);
5879
}
5980

81+
this.gatewayResourceName = getApiGatewayResourceNameFor(this.path, this.method);
82+
6083
let { basePath } = globalSettings;
6184
if (basePath) {
6285
if (!basePath.startsWith('/')) {

src/apiGatewayCachingPlugin.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ class ApiGatewayCachingPlugin {
108108
apiGatewayCaching: {
109109
properties: {
110110
enabled: { type: 'boolean' },
111+
apiGatewayIsShared: { type: 'boolean' },
112+
basePath: { type: 'string' },
113+
restApiId: { type: 'string' },
111114
clusterSize: { type: 'string' },
112115
ttlInSeconds: { type: 'number' },
113116
dataEncrypted: { type: 'boolean' },

src/pathParametersCache.js

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const split = require('lodash.split');
2-
31
const getResourcesByName = (name, serverless) => {
42
let resourceKeys = Object.keys(serverless.service.provider.compiledCloudFormationTemplate.Resources);
53
for (let resourceName of resourceKeys) {
@@ -9,36 +7,14 @@ const getResourcesByName = (name, serverless) => {
97
}
108
}
119

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-
element = element.replaceAll('+', '');
19-
element = element.replaceAll('_', '');
20-
element = element.replaceAll('.', '');
21-
element = element.replaceAll('-', 'Dash');
22-
if (element.startsWith('{')) {
23-
element = element.substring(element.indexOf('{') + 1, element.indexOf('}')) + "Var";
24-
}
25-
//capitalize first letter
26-
return element.charAt(0).toUpperCase() + element.slice(1);
27-
}).reduce((a, b) => a + b);
28-
29-
gatewayResourceName = "ApiGatewayMethod" + gatewayResourceName;
30-
return gatewayResourceName;
31-
}
32-
3310
const addPathParametersCacheConfig = (settings, serverless) => {
3411
for (let endpointSettings of settings.endpointSettings) {
3512
if (!endpointSettings.cacheKeyParameters) {
3613
continue;
3714
}
38-
const resourceName = getApiGatewayMethodNameFor(endpointSettings.path, endpointSettings.method);
39-
const method = getResourcesByName(resourceName, serverless);
15+
const method = getResourcesByName(endpointSettings.gatewayResourceName, serverless);
4016
if (!method) {
41-
serverless.cli.log(`[serverless-api-gateway-caching] The method ${resourceName} couldn't be found in the
17+
serverless.cli.log(`[serverless-api-gateway-caching] The method ${endpointSettings.gatewayResourceName} couldn't be found in the
4218
compiled CloudFormation template. Caching settings will not be updated for this endpoint.`);
4319
continue;
4420
}
@@ -74,11 +50,10 @@ const addPathParametersCacheConfig = (settings, serverless) => {
7450
method.Properties.Integration.CacheKeyParameters.push(cacheKeyParameter.name)
7551
}
7652
}
77-
method.Properties.Integration.CacheNamespace = `${resourceName}CacheNS`;
53+
method.Properties.Integration.CacheNamespace = `${endpointSettings.gatewayResourceName}CacheNS`;
7854
}
7955
}
8056

8157
module.exports = {
82-
addPathParametersCacheConfig: addPathParametersCacheConfig,
83-
getApiGatewayMethodNameFor: getApiGatewayMethodNameFor
58+
addPathParametersCacheConfig: addPathParametersCacheConfig
8459
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
const given = require('../test/steps/given');
2+
const expect = require('chai').expect;
3+
const ApiGatewayCachingSettings = require('../src/ApiGatewayCachingSettings');
4+
5+
describe('Determining API Gateway resource names', () => {
6+
const serviceName = 'cat-api';
7+
const functionName = 'get-cat-by-paw-id';
8+
9+
const scenarios = [
10+
{
11+
path: '/',
12+
method: 'GET',
13+
expectedGatewayResourceName: 'ApiGatewayMethodGet'
14+
},
15+
{
16+
path: '/',
17+
method: 'POST',
18+
expectedGatewayResourceName: 'ApiGatewayMethodPost'
19+
},
20+
{
21+
path: '/cat/{pawId}',
22+
method: 'GET',
23+
expectedGatewayResourceName: 'ApiGatewayMethodCatPawidVarGet'
24+
},
25+
{
26+
path: '/{id}',
27+
method: 'PATCH',
28+
expectedGatewayResourceName: 'ApiGatewayMethodIdVarPatch'
29+
}
30+
];
31+
for (const scenario of scenarios) {
32+
33+
34+
describe('when a base path is not specified', () => {
35+
before(() => {
36+
const endpoint = given
37+
.a_serverless_function(functionName)
38+
.withHttpEndpoint(scenario.method, scenario.path, { enabled: true });
39+
40+
const serverless = given
41+
.a_serverless_instance(serviceName)
42+
.withApiGatewayCachingConfig({ enabled: true })
43+
.withFunction(endpoint);
44+
45+
settings = new ApiGatewayCachingSettings(serverless);
46+
});
47+
48+
it('determines the resource name based on endpoint path and method', () => {
49+
expect(gatewayResourceNameOf(functionName, settings)).to.equal(scenario.expectedGatewayResourceName);
50+
});
51+
});
52+
53+
describe('when a base path is specified', () => {
54+
before(() => {
55+
const endpoint = given
56+
.a_serverless_function(functionName)
57+
.withHttpEndpoint(scenario.method, scenario.path, { enabled: true });
58+
59+
const serverless = given
60+
.a_serverless_instance(serviceName)
61+
.withApiGatewayCachingConfig({ enabled: true, basePath: '/animals' })
62+
.withFunction(endpoint);
63+
64+
settings = new ApiGatewayCachingSettings(serverless);
65+
});
66+
67+
it('is not included in the API Gateway resource name', () => {
68+
expect(gatewayResourceNameOf(functionName, settings)).to.equal(scenario.expectedGatewayResourceName);
69+
});
70+
});
71+
}
72+
});
73+
74+
const gatewayResourceNameOf = (functionName, settings) => {
75+
return settings
76+
.endpointSettings
77+
.find(x => x.functionName === functionName)
78+
.gatewayResourceName;
79+
}

test/model/Serverless.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class Serverless {
130130
const clone = object => JSON.parse(JSON.stringify(object));
131131

132132
const createMethodResourceNameFor = (path, method) => {
133-
const pathElements = split(path, '/');
133+
const pathElements = path.split('/');
134134
pathElements.push(method.toLowerCase());
135135
let gatewayResourceName = pathElements
136136
.map(element => {

0 commit comments

Comments
 (0)