import * as express from "express"; /** * patchExpressForPromises intercepts routes and checks if they return a promise. If they do, and * that promise is rejected, the patch will call `next` with the promise's error. This greatly * simplifies the use of promises in routes and makes it practical to use async/await. Any errors * thrown by the route, either explicitly or as unhandled exceptions, will reach Express's error * handling route, which is the desired behavior. This will likely become an official feature of * Express 5, but that has not yet been released. */ export function patchExpressForPromises(app) { for (const route of ["get", "post", "options", "put", "patch", "delete"]) { const asyncWrap = function asyncWrap(f) { if (Array.isArray(f)) { return f.map(asyncWrap); } return [ (req: express.Request, res: express.Response, next: express.NextFunction) => { const result = f(req, res, next); if (result && result.catch) { result.catch((err) => err && next(err)); } }, ]; }; const appRoute = app[route].bind(app); app[route] = (...args) => { if (typeof args[0] !== "string") { return appRoute(...args); } return appRoute(...args.map((v, i) => (i === 0 ? v : asyncWrap(v)))); }; } }