Skip to content

Commit 45d808e

Browse files
author
diana.ionita
committed
Merge branch 'release/1.1.6'
2 parents dacd06e + c8c0e84 commit 45d808e

File tree

7 files changed

+86
-14
lines changed

7 files changed

+86
-14
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-api-gateway-caching",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"description": "A plugin for the serverless framework which helps with configuring caching for API Gateway endpoints.",
55
"main": "src/apiGatewayCachingPlugin.js",
66
"scripts": {

src/apiGatewayCachingPlugin.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const ApiGatewayCachingSettings = require('./ApiGatewayCachingSettings');
44
const addPathParametersCacheConfig = require('./pathParametersCache');
55
const updateStageCacheSettings = require('./stageCache');
6-
const { outputRestApiIdTo } = require('./restApiId');
6+
const { restApiExists, outputRestApiIdTo } = require('./restApiId');
77

88
class ApiGatewayCachingPlugin {
99
constructor(serverless, options) {
@@ -22,7 +22,7 @@ class ApiGatewayCachingPlugin {
2222
}
2323

2424
updateCloudFormationTemplate() {
25-
this.thereIsARestApi = this.restApiExists();
25+
this.thereIsARestApi = restApiExists(this.serverless);
2626
if (!this.thereIsARestApi) {
2727
this.serverless.cli.log(`[serverless-api-gateway-caching] No Rest API found. Caching settings will not be updated.`);
2828
return;
@@ -45,14 +45,6 @@ class ApiGatewayCachingPlugin {
4545
}
4646
return updateStageCacheSettings(this.settings, this.serverless);
4747
}
48-
49-
restApiExists() {
50-
let resource = this.serverless.service.provider.compiledCloudFormationTemplate.Resources['ApiGatewayRestApi'];
51-
if (resource) {
52-
return true;
53-
}
54-
return false;
55-
}
5648
}
5749

5850
module.exports = ApiGatewayCachingPlugin;

src/restApiId.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,24 @@
33
const get = require('lodash.get');
44
const REST_API_ID_KEY = 'RestApiIdForApigCaching';
55

6+
const getConfiguredRestApiId = (serverless) => {
7+
return get(serverless, 'service.provider.apiGateway.restApiId')
8+
}
9+
10+
const restApiExists = (serverless) => {
11+
const configuredRestApiId = getConfiguredRestApiId(serverless);
12+
if (configuredRestApiId) {
13+
return true;
14+
}
15+
const resource = serverless.service.provider.compiledCloudFormationTemplate.Resources['ApiGatewayRestApi'];
16+
if (resource) {
17+
return true;
18+
}
19+
return false;
20+
}
21+
622
const outputRestApiIdTo = (serverless) => {
7-
const configuredRestApiId = get(serverless, 'service.provider.apiGateway.restApiId');
23+
const configuredRestApiId = getConfiguredRestApiId(serverless);
824
const autoGeneratedRestApiId = { Ref: 'ApiGatewayRestApi' };
925

1026
serverless.service.provider.compiledCloudFormationTemplate.Outputs[REST_API_ID_KEY] = {
@@ -27,6 +43,7 @@ const retrieveRestApiId = async (serverless, settings) => {
2743
};
2844

2945
module.exports = {
46+
restApiExists,
3047
outputRestApiIdTo,
3148
retrieveRestApiId
3249
};

test/configuring-rest-api-id.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
const APP_ROOT = '..';
2+
const given = require(`${APP_ROOT}/test/steps/given`);
3+
const expect = require('chai').expect;
4+
const { restApiExists } = require(`${APP_ROOT}/src/restApiId`);
5+
6+
describe('Finding the Rest API', () => {
7+
let result;
8+
describe('when the Rest API Id has already been defined in serverless configuration', () => {
9+
before(() => {
10+
let serverless = given.a_serverless_instance()
11+
.withPredefinedRestApiId(given.a_rest_api_id());
12+
result = restApiExists(serverless);
13+
});
14+
15+
it('should return that the Rest API exists', () => {
16+
expect(result).to.be.true;
17+
});
18+
});
19+
20+
describe('when the Rest API has not been defined in serverless configuration', () => {
21+
describe('and there are HTTP handler functions', () => {
22+
before(() => {
23+
let functionWithHttpEndpoint = given.a_serverless_function('get-cat-by-paw-id')
24+
.withHttpEndpoint('get', '/cat/{pawId}');
25+
serverless = given.a_serverless_instance()
26+
.withFunction(functionWithHttpEndpoint);
27+
28+
result = restApiExists(serverless);
29+
});
30+
31+
it('should return that the Rest API does exist', () => {
32+
expect(result).to.be.true;
33+
});
34+
});
35+
36+
describe('and there are no HTTP handler functions', () => {
37+
before(() => {
38+
serverless = given.a_serverless_instance();
39+
40+
result = restApiExists(serverless);
41+
});
42+
43+
it('should return that the Rest API does not exist', () => {
44+
expect(result).to.be.false;
45+
});
46+
});
47+
});
48+
});

test/model/Serverless.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ class Serverless {
5858
functionResourceName,
5959
methodResourceName
6060
}
61+
// when a function with an http endpoint is defined, serverless creates an ApiGatewayRestApi resource
62+
this.service.provider.compiledCloudFormationTemplate.Resources['ApiGatewayRestApi'] = {};
63+
return this;
64+
}
65+
66+
withPredefinedRestApiId(restApiId) {
67+
if (!this.service.provider.apiGateway) {
68+
this.service.provider.apiGateway = {}
69+
}
70+
this.service.provider.apiGateway.restApiId = restApiId;
6171
return this;
6272
}
6373

test/steps/given.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ const a_serverless_function = name => {
1111
return new ServerlessFunction(name);
1212
}
1313

14+
const a_rest_api_id = () => {
15+
return chance.guid();
16+
}
17+
1418
const a_rest_api_id_for_deployment = async (serverless, settings) => {
15-
let restApiId = chance.guid();
19+
let restApiId = a_rest_api_id();
1620
serverless.setRestApiId(restApiId, settings);
1721

1822
return restApiId;
@@ -21,5 +25,6 @@ const a_rest_api_id_for_deployment = async (serverless, settings) => {
2125
module.exports = {
2226
a_serverless_instance,
2327
a_serverless_function,
28+
a_rest_api_id,
2429
a_rest_api_id_for_deployment
2530
}

0 commit comments

Comments
 (0)