UNPKG

express-service-bootstrap

Version:

This is a convenience package for starting a express API with security, health checks, process exits etc.

115 lines 5.66 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Convenience = void 0; const body_parser_1 = __importDefault(require("body-parser")); const bootstrap_constructor_1 = require("./bootstrap-constructor"); const helmet_1 = __importDefault(require("helmet")); const express_1 = require("express"); const swaggerUi = __importStar(require("swagger-ui-express")); const compression_1 = __importDefault(require("compression")); /** * A convenience class that provides a way to create middleware instances without the need to manually create them. */ class Convenience { /** * Creates a new instance of the convenience class. * @param DIConstructor The dependency injection constructor to use to create instances of middleware. */ constructor(customConstructor = new bootstrap_constructor_1.BootstrapConstructor()) { this.customConstructor = customConstructor; } /** * Creates a new instance of the body parser middleware for url encoding. * @param urlEncodingOptions The options to use for the url encoding middleware. * @returns {ApplicationBuilderMiddleware} A new instance of the body parser middleware for url encoding. */ bodyParserURLEncodingMiddleware(urlEncodingOptions = { extended: true }) { return this.customConstructor.createInstanceWithoutConstructor(body_parser_1.default.urlencoded, [urlEncodingOptions]); } /** * Creates a new instance of the body parser middleware for JSON encoding. * @param jsonOptions The options to use for the JSON encoding middleware. * @returns {ApplicationBuilderMiddleware} A new instance of the body parser middleware for JSON encoding. */ bodyParserJSONEncodingMiddleware(jsonOptions = { limit: '1mb' }) { return this.customConstructor.createInstanceWithoutConstructor(body_parser_1.default.json, [jsonOptions]); } /** * Creates a new instance of the body parser middleware for raw encoding. * @param helmetOptions The options to use for the raw encoding middleware. * @returns {ApplicationBuilderMiddleware} A new instance of the helmet middleware. */ helmetMiddleware(helmetOptions) { return this.customConstructor.createInstanceWithoutConstructor(helmet_1.default, [helmetOptions]); } /** * Creates a new instance of the swagger API documentation middleware. * @param swaggerDocument The swagger document to use for the API documentation, typically a json object. * @param hostPath The host path to use for the swagger API documentation. * @returns {ApplicationRouter} A new instance of the swagger API documentation middleware. */ swaggerAPIDocs(swaggerDocument, hostPath = '/api-docs') { const swaggerRouter = this.customConstructor.createInstanceWithoutConstructor(express_1.Router); swaggerRouter.use(swaggerUi.serve, swaggerUi.setup(swaggerDocument)); return { hostingPath: hostPath, router: swaggerRouter }; } /** * Injects a specified object into the request under a given property name. * @param requestPropertyName - The name of the property to add to the request object. * @param object - The object to inject into the request. * @returns {ApplicationBuilderMiddleware} A middleware function that injects the object into the request. */ injectInRequestMiddleware(requestPropertyName, object) { const middleware = (req, res, next) => { req[requestPropertyName] = object; next(); }; return this.customConstructor.createInstanceWithoutConstructor(() => middleware); } /** * Creates a new instance of the compression middleware. * @param compressionOptions The options to use for the compression middleware. * @returns {ApplicationBuilderMiddleware} A new instance of the compression middleware. */ compressionMiddleware(compressionOptions) { return this.customConstructor.createInstanceWithoutConstructor(compression_1.default, [compressionOptions]); } } exports.Convenience = Convenience; //# sourceMappingURL=convenience.js.map