serverless-plugin-swagger-api
Version:
Define your API Endpoints in OpenAPI format (swagger) and create APIGW integration automatically
57 lines • 2.2 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const PLUGIN_CONFIG_ATTRIBUTE_KEY = 'x-attr-serverless';
exports.mapSwaggerFileToFunctionEvents = (swagger, log = console.log) => {
const paths = swagger.paths;
const securityDefinitions = swagger.securityDefinitions || {};
const getSecurityDefinition = (name) => {
const def = securityDefinitions[name] || {};
if (def['x-attr-name']) {
return {
name: def['x-attr-name'],
};
}
else if (def['x-attr-arn']) {
return {
arn: def['x-attr-arn'],
};
}
};
if (!paths) {
log('WARNING: Swaggerfile is missing property: `paths`');
return {};
}
return Object.keys(paths).reduce((handlers, path) => {
const routes = paths[path];
Object.keys(routes).forEach((method) => {
const route = routes[method];
const serverless = route[PLUGIN_CONFIG_ATTRIBUTE_KEY];
// if has serverless definition, let's add the events
if (serverless) {
if (!serverless.functionName) {
return log(`Warning: Missing functionName: '[${method}]: ${path}'`);
}
if (!handlers[serverless.functionName]) {
handlers[serverless.functionName] = [];
}
const params = {};
if (Array.isArray(route.security) && route.security[0]) {
for (let key in route.security[0]) {
const def = getSecurityDefinition(key);
if (def) {
params.authorizer = def;
break;
}
}
}
handlers[serverless.functionName].push({
http: Object.assign(Object.assign({ method,
path }, params), lodash_1.omit(serverless, 'functionName')),
});
}
});
return handlers;
}, {});
};
//# sourceMappingURL=swagger-mapper.js.map
;