UNPKG

express-service-readiness-middleware

Version:

This module provides express middleware for determining whether routes are exposed based on service critical dependency health.

86 lines (85 loc) 3.38 kB
import { NextFunction, Request, Response } from "express"; /** Logger interface */ export interface ILogger { /** Logs out a message */ log: (message: string) => void; } /** Configuration interface for the middleware */ export interface IConfig { /** interval in milliseconds in which to check if a dependency is ready, default is 2000 */ retryIntervalInMilliseconds?: number; /** maximum time in milliseconds to wait for all dependencies to be ready. default 30000 */ maximumWaitTimeForServiceReadinessInMilliseconds?: number; /** paths to still route traffic to even if dependencies are not yet ready, default empty array */ whitelistedPaths?: string[]; /** indicates whether dependency information should be logged out if dependencies fail to become ready */ logOutDependenciesDataOnFailure?: boolean; } /** Dependency interface */ export interface IDependency { /** the name of the dependency */ name: string; /** informational data about the dependency */ data: { [key: string]: string; }; /** indicates whether the dependency is ready */ isReady: () => Promise<boolean>; /** indicates whether the dependency is healthy */ isHealthy?: () => Promise<boolean>; /** indicates whether the dependency is critical */ critical: boolean; /** interval in milliseconds in which to check if a dependency is ready, default is 2000 */ retryIntervalInMilliseconds?: number; } /** Dependencies Health interface */ export interface IDependenciesHealth { /** indicates whether all dependencies are healthy */ allDependenciesHealthy: boolean; /** indicates whether all critical dependencies are healthy */ allCriticalDependenciesHealthy: boolean; /** health information about all dependencies */ dependencies: IDependencyHealth[]; } /** Dependency health interface */ export interface IDependencyHealth { /** the name of the dependency */ name: string; /** informational data about the dependency */ data: { [key: string]: string; }; /** indicates whether the dependency is healthy using 'isHealthy' if defined otherwise 'isReady' */ healthy: boolean; /** indicates whether the dependency is critical */ critical: boolean; } /** Readiness middleware */ export interface ReadinessMiddleware { /** middleware function */ (req: Request, res: Response, next: NextFunction): void; } /** * Creates the service readiness middleware * @param dependencies - Array of {IDependency} objects * @param config - Optional configuration for the middleware. If not defined the DefaultConfig will be used */ export declare const createReadinessMiddleware: (dependencies: IDependency[], config?: IConfig) => ReadinessMiddleware; /** * Checks the health of all dependencies * @param dependencies - Array of {IDependency} objects */ export declare const checkDependenciesHealth: (dependencies: IDependency[]) => Promise<IDependenciesHealth>; /** * Set a logger * @param logger - {ILogger} */ export declare const setLogger: (logger: ILogger) => void; /** * Returns a boolean indicating whether all critical dependencies are ready */ export declare const criticalDependenciesReady: () => Boolean; /** * Removes any NodeJS.Timeout instances created by the middleware */ export declare const stopCheckingReadiness: () => void;