exstack
Version:
A utility library designed to simplify and enhance Express.js applications.
26 lines (24 loc) • 904 B
TypeScript
import { Handler } from "./types.js";
import { NextFunction, Request, Response } from "express";
//#region src/handler.d.ts
/**
* Wraps a route handler (sync or async) and automatically:
* - Invokes the handler with `(req, res, next)`
* - Catches synchronous and asynchronous errors
* - Passes any returned value into {@link handleResult}
*
* @param callback - A function that handles a request, returning a value or `Promise`.
* @returns An Express-compatible request handler.
*
* @example
* app.get('/ping', handler(async () => ApiRes.ok({ alive: true }).msg('Pong')));
*
* @example
* app.post('/login', handler(async (req, res) => {
* const { username, password } = req.body;
* return new ApiRes(200, { user: username });
* }));
*/
declare const handler: (callback: Handler) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
//#endregion
export { handler };