forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.ts
48 lines (45 loc) · 1.36 KB
/
handler.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/
/**
* Function for handling HTTP requests in a web environment.
*
* @param request - The incoming HTTP request object.
* @returns A Promise resolving to a `Response` object, `null`, or directly a `Response`,
* supporting both synchronous and asynchronous handling.
*/
export type RequestHandlerFunction = (
request: Request,
) => Promise<Response | null> | null | Response;
/**
* Annotates a request handler function with metadata, marking it as a special
* handler.
*
* @param handler - The request handler function to be annotated.
* @returns The same handler function passed in, with metadata attached.
*
* @example
* Example usage in a Hono application:
* ```ts
* const app = new Hono();
* export default createRequestHandler(app.fetch);
* ```
*
* @example
* Example usage in a H3 application:
* ```ts
* const app = createApp();
* const handler = toWebHandler(app);
* export default createRequestHandler(handler);
* ```
*/
export function createRequestHandler(handler: RequestHandlerFunction): RequestHandlerFunction {
(handler as RequestHandlerFunction & { __ng_request_handler__?: boolean })[
'__ng_request_handler__'
] = true;
return handler;
}