UNPKG

openapi-connect

Version:
226 lines 7.41 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSystemRegistrators = void 0; const body_parser_1 = require("body-parser"); const cors = require("cors"); const express_unless_1 = require("express-unless"); const helmet = require("helmet"); const url = require("url"); const metrics_1 = require("../metrics"); const defaultLogger_1 = require("../../helpers/defaultLogger"); const getSystemRegistrators = () => [ helmetRegistrator, corsRegistrator, requestLoggingRegistrator, metricsRegistrator, oauthRegistrator, bodyParserRegistrator, swaggerMetadataRegistrator, apmRegistrator, swaggerValidatorRegistrator, securityRegistrator, routerRegistrator, healthRegistrator, swaggerUIRegistrator, errorHandlingRegistrator, errorLoggingRegistrator ]; exports.getSystemRegistrators = getSystemRegistrators; const helmetRegistrator = { kind: 'helmet', register: context => { const helmetOptions = context.options.helmet; if (!helmetOptions.enable) { return; } context.app.use(helmet(helmetOptions)); } }; Object.freeze(helmetRegistrator); const corsRegistrator = { kind: 'cors', register: context => { const corsOptions = context.options.cors; if (!corsOptions.enable) { return; } context.app.use(cors(corsOptions)); } }; Object.freeze(corsRegistrator); const requestLoggingRegistrator = { kind: 'requestLogging', register: context => { const unlessOptions = context.options.unless; const logger = context.options.logger; const requestLoggingOptions = context.options.requestLogging; if (!requestLoggingOptions.enable) { return; } context.app.use(withUnless(unlessOptions, requestLoggingOptions.handler(logger, requestLoggingOptions))); } }; Object.freeze(requestLoggingRegistrator); const oauthRegistrator = { kind: 'oauth', register: context => { const unlessOptions = context.options.unless; const oauthOptions = context.options.oauth; if (!oauthOptions.enable) { return; } context.app.use(withUnless({ ...unlessOptions, ...oauthOptions.unless }, oauthOptions.handler(oauthOptions))); } }; Object.freeze(oauthRegistrator); const bodyParserRegistrator = { kind: 'bodyParser', register: context => { const bodyParserOptions = context.options.bodyParser; if (!bodyParserOptions.enable) { return; } if (bodyParserOptions.json) { context.app.use((0, body_parser_1.json)(bodyParserOptions.json)); } if (bodyParserOptions.text) { context.app.use((0, body_parser_1.text)(bodyParserOptions.text)); } if (bodyParserOptions.urlencoded) { context.app.use((0, body_parser_1.urlencoded)(bodyParserOptions.urlencoded)); } } }; Object.freeze(bodyParserRegistrator); const swaggerMetadataRegistrator = { register: context => { context.app.use(context.swagger.swaggerMetadata()); } }; Object.freeze(swaggerMetadataRegistrator); const apmRegistrator = Object.freeze({ register: context => { context.app.use((req, _, next) => { var _a, _b, _c, _d, _e; const { apm, logger } = context.options; if (apm) { try { const swaggerRequest = req; const transactionName = (_c = (_b = (_a = swaggerRequest.swagger) === null || _a === void 0 ? void 0 : _a.operation) === null || _b === void 0 ? void 0 : _b.operationId) !== null && _c !== void 0 ? _c : `${req.method} ${(_e = (_d = swaggerRequest.swagger) === null || _d === void 0 ? void 0 : _d.apiPath) !== null && _e !== void 0 ? _e : req.url}`; apm.setTransactionName(transactionName); } catch (e) { logger.error(`APM: Failed to set transaction name`, e); } } next(); }); } }); const swaggerValidatorRegistrator = { register: context => { const unlessOptions = context.options.unless; context.app.use(withUnless(unlessOptions, context.swagger.swaggerValidator())); } }; Object.freeze(swaggerValidatorRegistrator); const securityRegistrator = { kind: 'security', register: context => { const unlessOptions = context.options.unless; const securityOptions = context.options.security; if (!securityOptions.enable) { return; } context.app.use(withUnless(unlessOptions, context.swagger.swaggerSecurity(securityOptions.handlers))); } }; Object.freeze(securityRegistrator); const routerRegistrator = { kind: 'router', register: context => { const routerOptions = context.options.router; if (!routerOptions.enable) { return; } context.app.use(context.swagger.swaggerRouter(routerOptions)); } }; Object.freeze(routerRegistrator); const healthRegistrator = { kind: 'health', register: context => { const healthOptions = context.options.health; if (!healthOptions.enable) { return; } context.app.use(healthOptions.path, healthOptions.handler); } }; Object.freeze(healthRegistrator); const swaggerUIRegistrator = { kind: 'swaggerUI', register: context => { const basePath = context.options.basePath; const swaggerUiOptions = context.options.swaggerUI; if (!swaggerUiOptions.enable) { return; } context.app.use(context.swagger.swaggerUi({ apiDocs: url.resolve(basePath, 'api-docs'), swaggerUi: url.resolve(basePath, 'docs') })); } }; Object.freeze(swaggerUIRegistrator); const errorLoggingRegistrator = { kind: 'errorLogging', register: context => { const logger = context.options.logger; const errorLoggingOptions = context.options.errorLogging; if (!errorLoggingOptions.enable) { return; } (0, defaultLogger_1.setDefaultLogger)(logger); context.app.use(errorLoggingOptions.handler(logger, errorLoggingOptions)); } }; Object.freeze(errorLoggingRegistrator); const errorHandlingRegistrator = { kind: 'errorHandling', register: context => { const errorHandlingOptions = context.options.errorHandling; if (!errorHandlingOptions.enable) { return; } context.app.use(errorHandlingOptions.handler); } }; Object.freeze(errorHandlingRegistrator); const metricsRegistrator = { kind: 'collectMetrics', register: context => { const middlewares = (0, metrics_1.collectMetricsMiddlewares)(context.options); if (middlewares) { const { errorHandler, handler } = middlewares; context.app.use(handler); context.app.use(errorHandler); } } }; Object.freeze(metricsRegistrator); function withUnless(unlessOptions, handler) { if (!handler.unless) { handler.unless = express_unless_1.unless; } if (unlessOptions.enable) { return handler.unless(unlessOptions); } else { return handler; } } //# sourceMappingURL=systemRegistrator.js.map