Skip to content

Commit 27d71a2

Browse files
author
diana.ionita
committed
Merge branch 'release/1.1.2'
2 parents bc151da + 6ca29e4 commit 27d71a2

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
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.1",
3+
"version": "1.1.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": {

src/apiGatewayCachingPlugin.js

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

78
class ApiGatewayCachingPlugin {
89
constructor(serverless, options) {
@@ -21,17 +22,14 @@ class ApiGatewayCachingPlugin {
2122
}
2223

2324
updateCloudFormationTemplate() {
24-
let restApiId = {
25-
Ref: 'ApiGatewayRestApi',
26-
};
27-
if (this.serverless.service.provider.apiGateway && this.serverless.service.provider.apiGateway.restApiId) {
28-
restApiId = this.serverless.service.provider.apiGateway.restApiId
25+
this.thereIsARestApi = this.restApiExists();
26+
if (!this.thereIsARestApi) {
27+
this.serverless.cli.log(`[serverless-api-gateway-caching] No Rest API found. Caching settings will not be updated.`);
28+
return;
2929
}
30-
this.serverless.service.provider.compiledCloudFormationTemplate.Outputs.RestApiIdForApiGwCaching = {
31-
Description: 'Rest API Id',
32-
Value: restApiId,
33-
};
34-
30+
31+
outputRestApiIdTo(this.serverless);
32+
3533
// if caching is not defined or disabled
3634
if (!this.settings.cachingEnabled) {
3735
return;
@@ -41,8 +39,20 @@ class ApiGatewayCachingPlugin {
4139
}
4240

4341
updateStage() {
42+
if (!this.thereIsARestApi) {
43+
this.serverless.cli.log(`[serverless-api-gateway-caching] No Rest API found. Caching settings will not be updated.`);
44+
return;
45+
}
4446
return updateStageCacheSettings(this.settings, this.serverless);
4547
}
48+
49+
restApiExists() {
50+
let resource = this.serverless.service.provider.compiledCloudFormationTemplate.Resources['ApiGatewayRestApi'];
51+
if (resource) {
52+
return true;
53+
}
54+
return false;
55+
}
4656
}
4757

4858
module.exports = ApiGatewayCachingPlugin;

src/restApiId.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
const get = require('lodash.get');
4+
const REST_API_ID_KEY = 'RestApiIdForApigCaching';
5+
6+
const outputRestApiIdTo = (serverless) => {
7+
const configuredRestApiId = get(serverless, 'service.provider.apiGateway.restApiId');
8+
const autoGeneratedRestApiId = { Ref: 'ApiGatewayRestApi' };
9+
10+
serverless.service.provider.compiledCloudFormationTemplate.Outputs[REST_API_ID_KEY] = {
11+
Description: 'Rest API Id',
12+
Value: configuredRestApiId || autoGeneratedRestApiId,
13+
};
14+
};
15+
16+
const retrieveRestApiId = async (serverless, settings) => {
17+
const stackName = serverless.providers.aws.naming.getStackName(settings.stage);
18+
19+
const cloudFormation = await serverless.providers.aws.request('CloudFormation', 'describeStacks', { StackName: stackName },
20+
settings.stage,
21+
settings.region
22+
);
23+
const outputs = cloudFormation.Stacks[0].Outputs;
24+
const restApiKey = outputs.find(({ OutputKey }) => OutputKey === REST_API_ID_KEY).OutputValue;
25+
26+
return restApiKey;
27+
};
28+
29+
module.exports = {
30+
outputRestApiIdTo,
31+
retrieveRestApiId
32+
};

src/stageCache.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
const isEmpty = require('lodash.isempty');
2-
3-
const getRestApiId = async (settings, serverless) => {
4-
const stackName = serverless.providers.aws.naming.getStackName(settings.stage);
5-
6-
let stack = await serverless.providers.aws.request('CloudFormation', 'describeStacks', { StackName: stackName },
7-
settings.stage,
8-
settings.region
9-
);
10-
11-
return stack.Stacks[0].Outputs
12-
.filter(output => output.OutputKey === 'RestApiIdForApiGwCaching')
13-
.map(output => output.OutputValue)[0];
14-
}
2+
const { retrieveRestApiId } = require('./restApiId');
153

164
String.prototype.replaceAll = function (search, replacement) {
175
let target = this;
@@ -119,7 +107,7 @@ const updateStageCacheSettings = async (settings, serverless) => {
119107
return;
120108
}
121109

122-
let restApiId = await getRestApiId(settings, serverless);
110+
let restApiId = await retrieveRestApiId(serverless, settings);
123111

124112
let patchOps = createPatchForStage(settings);
125113

test/model/Serverless.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class Serverless {
8888
Stacks: [
8989
{
9090
Outputs: [{
91-
OutputKey: 'RestApiIdForApiGwCaching',
91+
OutputKey: 'RestApiIdForApigCaching',
9292
OutputValue: restApiId
9393
}]
9494
}

0 commit comments

Comments
 (0)