mirror of
https://github.com/serverless/serverless.git
synced 2026-01-18 14:58:43 +00:00
add api key object format
This commit is contained in:
parent
d4f2161db3
commit
e58ddd0614
@ -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();
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user