forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync-handler.ts
More file actions
29 lines (26 loc) · 916 Bytes
/
Copy pathasync-handler.ts
File metadata and controls
29 lines (26 loc) · 916 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import type { NextFunction, Request, RequestHandler, Response } from "express";
import type { ParamsDictionary, Query } from "express-serve-static-core";
export type AsyncRequestHandler<
P = ParamsDictionary,
ResBody = unknown,
ReqBody = unknown,
ReqQuery = Query,
Locals extends Record<string, unknown> = Record<string, unknown>,
> = (
request: Request<P, ResBody, ReqBody, ReqQuery, Locals>,
response: Response<ResBody, Locals>,
next: NextFunction,
) => Promise<unknown>;
export const asyncHandler = <
P = ParamsDictionary,
ResBody = unknown,
ReqBody = unknown,
ReqQuery = Query,
Locals extends Record<string, unknown> = Record<string, unknown>,
>(
handler: AsyncRequestHandler<P, ResBody, ReqBody, ReqQuery, Locals>,
): RequestHandler<P, ResBody, ReqBody, ReqQuery, Locals> => {
return (request, response, next) => {
void handler(request, response, next).catch(next);
};
};