UNPKG

@catbee/utils

Version:

A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.

72 lines 2.81 kB
/** * Type definitions for Express-compatible middleware */ export type Request = { headers: Record<string, string | string[] | undefined>; method: string; url: string; ip?: string; body?: any; query?: Record<string, any>; params?: Record<string, any>; [key: string]: any; }; export type Response = { status: (code: number) => Response; json: (data: any) => void; send: (data: any) => void; setHeader: (name: string, value: string | string[]) => void; end: (data?: any) => void; on: (event: string, callback: (...args: any[]) => void) => void; [key: string]: any; }; export type NextFunction = (err?: Error | any) => void; export type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<void>; /** * Attaches a unique request ID to each request. * Useful for request tracing and correlation between logs. * * @param {object} [options] - Configuration options * @param {string} [options.headerName='X-Request-ID'] - Header name for request ID * @param {boolean} [options.exposeHeader=true] - Whether to expose the header in response * @returns {Middleware} Express-compatible middleware */ export declare function requestId(options?: { headerName?: string; exposeHeader?: boolean; }): Middleware; /** * Measures request processing time and logs or adds it to response headers. * * @param {object} [options] - Configuration options * @param {boolean} [options.addHeader=true] - Whether to add X-Response-Time header * @param {boolean} [options.logOnComplete=false] - Whether to log timing info * @returns {Middleware} Express-compatible middleware */ export declare function responseTime(options?: { addHeader?: boolean; logOnComplete?: boolean; }): Middleware; /** * Request timeout middleware. * Aborts requests that take too long to process. * * @param {number} [timeoutMs=30000] - Timeout in milliseconds * @returns {Middleware} Express-compatible middleware */ export declare function timeout(timeoutMs?: number): Middleware; /** * Global error handling middleware with enhanced features. * * @param {object} [options] - Error handler options * @param {boolean} [options.logErrors=true] - Whether to log errors * @param {boolean} [options.includeDetails=false] - Whether to include error details in non-production * @param {Function} [options.logger=getLogger().error] - Custom logging function * @returns {(err: any, req: Request, res: Response, next: NextFunction) => void} Error middleware */ export declare function errorHandler(options?: { logErrors?: boolean; includeDetails?: boolean; logger?: (message: string, error: any) => void; }): (err: any, req: Request, res: Response, _next: NextFunction) => void; //# sourceMappingURL=middleware.utils.d.ts.map