express-service-bootstrap
Version:
This is a convenience package for starting a express API with security, health checks, process exits etc.
120 lines (119 loc) • 6.79 kB
TypeScript
import { IRouter, NextFunction, Request, Response } from "express";
import { ApplicationShutdownStatus, ApplicationStartupStatus, ApplicationStatus } from "./enum-application-life-cycle-status";
import { DisposableSingletonContainer } from "./disposable-singleton-container";
import { IProbe } from "./i-probe";
import { IProbeResult } from "./i-probe-result";
export type ApplicationBuilderMiddleware = (request: Request, response: Response, next: NextFunction) => Promise<void> | void;
export type HostingPath = string | "*";
export declare enum ApplicationTypes {
Main = 0,
Health = 1,
Both = 2
}
declare module 'express' {
interface Express {
[Symbol.asyncDispose]?: () => Promise<void>;
}
}
/**
* The ApplicationBuilder class is responsible for building an express application.
* It sets up the application's health status, ports, middlewares, routers, and error handling.
*/
export declare class ApplicationBuilder {
applicationName: string;
startupHandler: (rootRouter: IRouter, DIContainer: DisposableSingletonContainer, applicationBuilder: ApplicationBuilder) => Promise<IProbeResult<ApplicationStartupStatus>>;
shutdownHandler: () => Promise<IProbeResult<ApplicationShutdownStatus>>;
livenessProbe: IProbe<ApplicationStatus>;
readinessProbe: IProbe<ApplicationStatus>;
private readonly currentProcess;
private readonly exitSignals;
private readonly container;
static readonly DINAME_ApplicationExpress = "AE";
static readonly DINAME_HealthExpress = "HE";
private applicationStatus;
private applicationPort;
private healthPort;
private appHandlers;
private healthHandlers;
private catchAllErrorResponseTransformer;
private readonly exitHandler;
/**
* Creates an instance of ApplicationBuilder.
*
* @param {string} applicationName - The name of the application.
* @param startupHandler - The startup handler that has to be invoked before application starts, used to indicate the application's startup status.
* @param shutdownHandler - The shutdown handler that has to be invoked before application shutdowns, used to indicate the application's liveliness status.
* @param {IProbe} livenessProbe - The liveness probe used to indicate the application's liveness status.
* @param {IProbe} readinessProbe - The readiness probe used to indicate the application's readiness status.
* @param {NodeJS.Process} currentProcess - The current process.
* @param {NodeJS.Signals[]} exitSignals - The exit signals.
* @param {DisposableSingletonContainer} container - The container for disposable singletons.
*/
constructor(applicationName?: string, startupHandler?: (rootRouter: IRouter, DIContainer: DisposableSingletonContainer, applicationBuilder: ApplicationBuilder) => Promise<IProbeResult<ApplicationStartupStatus>>, shutdownHandler?: () => Promise<IProbeResult<ApplicationShutdownStatus>>, livenessProbe?: IProbe<ApplicationStatus>, readinessProbe?: IProbe<ApplicationStatus>, currentProcess?: NodeJS.Process, exitSignals?: NodeJS.Signals[], container?: DisposableSingletonContainer);
/**
* Used to override the startup handler.
* @param startupHandler Handler to be invoked before application starts, used to indicate the application's startup status.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideStartupHandler(startupHandler: (rootRouter: IRouter, DIContainer: DisposableSingletonContainer, applicationBuilder: ApplicationBuilder) => Promise<IProbeResult<ApplicationStartupStatus>>): ApplicationBuilder;
/**
* Used to override the shutdown handler.
* @param shutdownHandler Handler to be invoked before application shutdowns, used to cleanup resources.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideShutdownHandler(shutdownHandler: () => Promise<IProbeResult<ApplicationShutdownStatus>>): ApplicationBuilder;
/**
* Used to override the liveness probe.
* @param livenessProbe Probe used to indicate the application's liveness status.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideLivenessProbe(livenessProbe: IProbe<ApplicationStatus>): ApplicationBuilder;
/**
* Used to override the readiness probe.
* @param readinessProbe Probe used to indicate the application's readiness status.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideReadinessProbe(readinessProbe: IProbe<ApplicationStatus>): ApplicationBuilder;
/**
* Used to overrides the application port default(3000).
* @param {number} port new application port.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideAppPort(port: number): ApplicationBuilder;
/**
* Used to overrides the health port default(5678).
* @param {number} port new health port.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideHealthPort(port: number): ApplicationBuilder;
/**
* Used to overrides the catchAllErrorResponseTransformer configuration.
* @param {(request: Request, error: unknown) => unknown} transformer new catchAllErrorResponseTransformer configured middleware.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
overrideCatchAllErrorResponseTransformer(transformer: (request: Request, error: unknown) => unknown): ApplicationBuilder;
/**
* Used to register a SYNC/ASYNC middleware.
* @param {ApplicationBuilderMiddleware} handler handler to be registered.
* @param {HostingPath} hostingPath path where the handler has to be registered, use "*" for global.
* @param {number} order order in which the handler has to be registered.
* @param {ApplicationTypes} appliesTo type of application to which the handler has to be registered.
* @returns {ApplicationBuilder} ApplicationBuilder instance.
*/
registerApplicationHandler(handler: ApplicationBuilderMiddleware | IRouter, hostingPath: HostingPath, order?: number | undefined, appliesTo?: ApplicationTypes): ApplicationBuilder;
/**
* Used to start the application using the configured parameters.
* @returns {Promise<void>} Promise that resolves when the application is started.
*/
start(): Promise<void>;
/**
* Used to stop the application. This clears all the middlewares and routers.(all config is reset to default)
* @returns {Promise<void>} Promise that resolves when the application is stopped.
*/
[Symbol.asyncDispose](): Promise<void>;
private appExpressListen;
private healthExpressListen;
private checkHealthStatus;
private errorHandler;
private setRoutes;
}