diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/apiKeys.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/apiKeys.js index 2135bd8d2..e2c5a852a 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/apiKeys.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/apiKeys.js @@ -4,11 +4,14 @@ const _ = require('lodash'); const BbPromise = require('bluebird'); function createApiKeyResource(that, apiKey) { + const name = _.isString(apiKey) ? apiKey : apiKey.name; + const value = _.isObject(apiKey) && apiKey.value ? apiKey.value : undefined; const resourceTemplate = { Type: 'AWS::ApiGateway::ApiKey', Properties: { Enabled: true, - Name: apiKey, + Name: name, + Value: value, StageKeys: [{ RestApiId: that.provider.getApiGatewayRestApiId(), StageName: that.provider.getStage(), @@ -20,10 +23,19 @@ function createApiKeyResource(that, apiKey) { return _.cloneDeep(resourceTemplate); } +function validateApiKey(apiKey) { + if (_.isObject(apiKey) && (!_.isNil(apiKey.name) || !_.isNil(apiKey.value))) { + return true; + } else if (!_.isString(apiKey)) { + return false; + } + return true; +} + module.exports = { compileApiKeys() { if (this.serverless.service.provider.apiKeys) { - if (!Array.isArray(this.serverless.service.provider.apiKeys)) { + if (!_.isArray(this.serverless.service.provider.apiKeys)) { throw new this.serverless.classes.Error('apiKeys property must be an array'); } @@ -36,8 +48,10 @@ module.exports = { keyNumber = 0; const name = Object.keys(apiKeyDefinition)[0]; _.forEach(apiKeyDefinition[name], (key) => { - if (!_.isString(key)) { - throw new this.serverless.classes.Error('API keys must be strings'); + if (!validateApiKey(key)) { + throw new this.serverless.classes.Error( + `API Key must be a string or an object which contains name and/or value '${JSON.stringify(key)}'` + ); } keyNumber += 1; const apiKeyLogicalId = this.provider.naming @@ -49,6 +63,11 @@ module.exports = { }); } else { keyNumber += 1; + if (!validateApiKey(apiKeyDefinition)) { + throw new this.serverless.classes.Error( + `API Key must be a string or an object which contains name and/or value '${JSON.stringify(apiKeyDefinition)}'` + ); + } const apiKeyLogicalId = this.provider.naming .getApiKeyLogicalId(keyNumber); const resourceTemplate = createApiKeyResource(this, apiKeyDefinition); @@ -59,5 +78,5 @@ module.exports = { }); } return BbPromise.resolve(); - }, + } }; diff --git a/lib/plugins/aws/package/compile/events/apiGateway/lib/usagePlanKeys.js b/lib/plugins/aws/package/compile/events/apiGateway/lib/usagePlanKeys.js index 3bc583908..0b9b6155e 100644 --- a/lib/plugins/aws/package/compile/events/apiGateway/lib/usagePlanKeys.js +++ b/lib/plugins/aws/package/compile/events/apiGateway/lib/usagePlanKeys.js @@ -41,9 +41,11 @@ module.exports = { } keyNumber = 0; _.forEach(apiKeyDefinition[name], (key) => { - if (!_.isString(key)) { + const apiKeyName = typeof key === 'string' ? key : key.name; + if (!_.isString(apiKeyName)) { throw new this.serverless.classes.Error('API keys must be strings'); } + keyNumber += 1; const usagePlanKeyLogicalId = this.provider.naming .getUsagePlanKeyLogicalId(keyNumber, name);