UNPKG

node-js-api-response

Version:

Unified API response and error handling for Express.js in TypeScript. This package provides a middleware for consistent API responses and error handling in Express applications, making it easier to manage API responses and errors in a standardized way.

19 lines (18 loc) 878 B
import { NextFunction } from "express"; import { BaseError } from "./BaseError"; /** * Represents an HTTP-specific error with a status code. * Extend this for specific HTTP error types (e.g., NotFoundError, UnauthorizedError). */ export declare class HttpErrorResponse<T = any> extends BaseError<T> { constructor(statusCode?: number, message?: string, errorCode?: string); } /** * Helper function to create and forward an HttpError to the next middleware. * * @param next - The Express next function to pass the error to. * @param statusCode - The HTTP status code for the error. * @param message - The error message. Defaults to 'Internal server error'. * @throws {HttpError} If next is not a function, throws the created HttpError. */ export declare const ErrorResponse: <T>(next: NextFunction, statusCode: number, message?: string, errorCode?: string) => void;