UNPKG

exp-core

Version:

Core utilities and middleware for building robust Express applications, including standard API responses, async error handling, request sanitization, and extended request types.

119 lines (115 loc) 4.34 kB
// Generated by dts-bundle-generator v9.5.1 import { NextFunction, Request as Request$1, RequestHandler, Response as Response$1 } from 'express'; /** * Sends a standardized successful API response. * * @function successResponse * @param {Response} res - Express response object. * @param {Object} options - Response options. * @param {string} options.message - Descriptive success message. * @param {number} [options.statusCode=200] - Optional HTTP status code (defaults to 200). * @param {object|null} [options.data=null] - Optional data payload (can be object or null). * @param {object|null} [options.meta=null] - Optional meta information (e.g., pagination, environment). * @returns {void} * * @example * successResponse(res, { * message: "Users fetched successfully.", * data: { users: [...] }, * meta: { totalCount: 50 } * }); */ export declare const successResponse: (res: Response, options: { message: string; statusCode?: number; data?: object | null; meta?: object | null; }) => void; /** * Sends a standardized error API response. * * @function errorResponse * @param {Response} res - Express response object. * @param {Object} options - Error response options. * @param {string} options.message - Error message describing what went wrong. * @param {number} [options.statusCode=400] - Optional HTTP status code (defaults to 400). * @param {Record<string, string[]> | null} [options.errors=null] - Optional validation or field-specific errors. * @returns {void} * * @example * errorResponse(res, { * message: "Validation failed.", * statusCode: 422, * errors: { * email: ["Email is required."], * password: ["Password must be at least 8 characters."] * } * }); */ export declare const errorResponse: (res: Response, options: { message: string; statusCode?: number; errors?: Record<string, string[]> | null; }) => void; /** * Wraps an async Express route handler to catch errors and forward them to a standardized error response. * * Prevents the need for repetitive try/catch blocks in each async controller by automatically * handling rejected promises and formatting the error using a consistent API response shape. * * @function aw * @param {RequestHandler} requestHandler - The async route/controller function to wrap. * @returns {(req: Request, res: Response, next: NextFunction) => void} A new Express-compatible middleware function. * * @example * // usage in a route * router.get('/users', aw(async (req, res) => { * const users = await userService.getAllUsers(); * return successResponse(res, { * message: "Users fetched successfully.", * data: { users } * }); * })); */ export declare const aw: (requestHandler: RequestHandler) => ((req: Request, res: Response, next: NextFunction) => void); /** * Custom error class for handling API-specific errors with support for HTTP status codes * and detailed validation or field-level errors. * * Extends the built-in `Error` class to include additional properties commonly used * in API error responses. * * @class ApiError * @extends {Error} * * @property {number} statusCode - HTTP status code associated with the error (default: 400). * @property {Record<string, string[]> | null} errors - Optional detailed validation or field-level errors. * * @example * throw new ApiError("Invalid input data", 422, { * email: ["Email is required."], * password: ["Password must be at least 8 characters."] * }); */ export declare class ApiError extends Error { statusCode: number; errors: Record<string, string[]> | null; /** * Creates a new ApiError instance. * * @param {string} message - A human-readable error message. * @param {number} [statusCode=400] - Optional HTTP status code. * @param {Record<string, string[]> | null} [errors=null] - Optional detailed errors. */ constructor(message: string, statusCode?: number, errors?: Record<string, string[]> | null); } /** * Middleware to ensure `req.body` is always initialized as an object. * * In Express 5, `req.body` is `undefined` by default if no body-parsing middleware * is used. This middleware sets `req.body` to `{}` if it's undefined. * * @function initRequestBody */ export declare const initRequestBody: (req: Request, res: Response, next: NextFunction) => void; export {};