/
apiKeys.js
43 lines (36 loc) · 1.19 KB
/
apiKeys.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
'use strict';
const _ = require('lodash');
const BbPromise = require('bluebird');
module.exports = {
compileApiKeys() {
if (this.serverless.service.provider.apiKeys) {
if (this.serverless.service.provider.apiKeys.constructor !== Array) {
throw new this.serverless.classes.Error('apiKeys property must be an array');
}
this.serverless.service.provider.apiKeys.forEach((apiKey, index) => {
if (typeof apiKey !== 'string') {
throw new this.serverless.classes.Error('API Keys must be strings');
}
const apiKeysTemplate = `
{
"Type" : "AWS::ApiGateway::ApiKey",
"Properties" : {
"Enabled" : true,
"Name" : "${apiKey}",
"StageKeys" : [{
"RestApiId": { "Ref": "ApiGatewayRestApi" },
"StageName": "${this.options.stage}"
}]
}
}
`;
const newApiKeyObject = {
[`ApiGatewayApiKey${index}`]: JSON.parse(apiKeysTemplate),
};
_.merge(this.serverless.service.provider.compiledCloudFormationTemplate.Resources,
newApiKeyObject);
});
}
return BbPromise.resolve();
},
};