UNPKG

serverless-plugin-swagger-api

Version:

Define your API Endpoints in OpenAPI format (swagger) and create APIGW integration automatically

39 lines 1.43 kB
"use strict"; const swagger_mapper_1 = require("./swagger-mapper"); const lodash_1 = require("lodash"); const HookEvents = [ 'before:offline:start', 'before:run:run', 'before:package:createDeploymentArtifacts', 'before:deploy:function:packageFunction', 'before:invoke:local:invoke', ]; const PLUGIN_CONFIG_ROOT_PATH = 'service.custom.swaggerApi'; class ServerlessPlugin { constructor(serverless, options) { this.serverless = serverless; this.options = options; this.hooks = HookEvents.reduce((h, k) => { h[k] = this.addRoutes.bind(this); return h; }, {}); } addRoutes() { const swaggerFile = lodash_1.get(this.serverless, PLUGIN_CONFIG_ROOT_PATH + '.swagger'); if (!swaggerFile) { this.serverless.cli.log('WARNING: No swagger file found! Add swagger file to `custom.swaggerApi.swagger`'); return; } const handlerEvents = swagger_mapper_1.mapSwaggerFileToFunctionEvents(swaggerFile); Object.keys(handlerEvents).forEach(lambdaName => { try { this.serverless.service.functions[lambdaName].events = handlerEvents[lambdaName]; } catch (e) { this.serverless.cli.log(`WARNING: No such lambda: '${lambdaName}'`); } }); } } module.exports = ServerlessPlugin; //# sourceMappingURL=index.js.map