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.
15 lines (14 loc) • 780 B
TypeScript
import { Request, Response, NextFunction, RequestHandler } from 'express';
/**
* Wraps an asynchronous route handler function and ensures that any errors are passed to the next middleware.
*
* @param {Function} fn - The asynchronous route handler function to wrap. It receives the Express `Request`, `Response`, and `NextFunction` objects.
* @returns {RequestHandler } A function that executes the handler and catches any rejected promises, forwarding errors to Express error handling middleware.
*
* * @example
* router.get('/example', asyncHandler(async (req, res) => {
* const data = await somethingAsync();
* res.json(data);
* }));
*/
export declare const asyncHandler: (fn: (req: Request, res: Response, next: NextFunction) => Promise<any>) => RequestHandler;