11import type { ServerRequest , ServerRuntimeContext } from "srvx" ;
2- import type { H3Event as H3EventT , H3EventContext } from "./types/event .ts" ;
2+ import type { H3EventContext } from "./types/context .ts" ;
33
44import { EmptyObject } from "./utils/internal/obj.ts" ;
55import { FastURL } from "srvx" ;
6+ import type {
7+ EventHandlerRequest ,
8+ TypedServerRequest ,
9+ } from "./types/handler.ts" ;
610
7- export class H3Event implements H3EventT {
8- static __is_event__ = true ;
11+ export class H3Event <
12+ _RequestT extends EventHandlerRequest = EventHandlerRequest ,
13+ > {
14+ /**
15+ * Incoming HTTP request info.
16+ *
17+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Request)
18+ */
19+ readonly req : TypedServerRequest < _RequestT > ;
920
10- req : ServerRequest ;
21+ /**
22+ * Access to the parsed request URL.
23+ *
24+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/URL)
25+ */
1126 url : URL ;
12- context : H3EventContext ;
27+
28+ /**
29+ * Event context.
30+ */
31+ readonly context : H3EventContext ;
32+
33+ /**
34+ * @internal
35+ */
36+ static __is_event__ = true ;
37+
38+ /**
39+ * @internal
40+ */
1341 _res ?: H3EventResponse ;
1442
1543 constructor ( req : ServerRequest , context ?: H3EventContext ) {
@@ -20,49 +48,84 @@ export class H3Event implements H3EventT {
2048 this . url = _url && _url instanceof URL ? _url : new FastURL ( req . url ) ;
2149 }
2250
51+ /**
52+ * Prepared HTTP response.
53+ */
2354 get res ( ) : H3EventResponse {
2455 if ( ! this . _res ) {
2556 this . _res = new H3EventResponse ( ) ;
2657 }
2758 return this . _res ;
2859 }
2960
30- get path ( ) : string {
31- return this . url . pathname + this . url . search ;
61+ /**
62+ * Access to runtime specific additional context.
63+ *
64+ */
65+ get runtime ( ) : ServerRuntimeContext | undefined {
66+ return this . req . runtime ;
3267 }
3368
34- get method ( ) : string {
35- return this . req . method ;
69+ /**
70+ * Tell the runtime about an ongoing operation that shouldn't close until the promise resolves.
71+ */
72+ waitUntil ( promise : Promise < any > ) : void {
73+ this . req . waitUntil ?.( promise ) ;
3674 }
3775
38- get headers ( ) : Headers {
39- return this . req . headers ;
76+ toString ( ) : string {
77+ return `[ ${ this . req . method } ] ${ this . req . url } ` ;
4078 }
4179
42- get runtime ( ) : ServerRuntimeContext | undefined {
43- return this . req . runtime ;
80+ toJSON ( ) : string {
81+ return this . toString ( ) ;
4482 }
4583
84+ // ------------- deprecated ---------------
85+
86+ /**
87+ * Access to the raw Node.js req/res objects.
88+ *
89+ * @deprecated Use `event.runtime.{node|deno|bun|...}.` instead.
90+ */
4691 get node ( ) : ServerRuntimeContext [ "node" ] | undefined {
4792 return this . req . runtime ?. node ;
4893 }
4994
50- waitUntil ( promise : Promise < any > ) : void {
51- this . req . waitUntil ?.( promise ) ;
95+ /**
96+ * Access to the incoming request headers.
97+ *
98+ * @deprecated Use `event.req.headers` instead.
99+ *
100+ */
101+ get headers ( ) : Headers {
102+ return this . req . headers ;
52103 }
53104
54- toString ( ) : string {
55- return `[${ this . req . method } ] ${ this . req . url } ` ;
105+ /**
106+ * Access to the incoming request url (pathname+search).
107+ *
108+ * @deprecated Use `event.url.pathname + event.url.search` instead.
109+ *
110+ * Example: `/api/hello?name=world`
111+ * */
112+ get path ( ) : string {
113+ return this . url . pathname + this . url . search ;
56114 }
57115
58- toJSON ( ) : string {
59- return this . toString ( ) ;
116+ /**
117+ * Access to the incoming request method.
118+ *
119+ * @deprecated Use `event.req.method` instead.
120+ */
121+ get method ( ) : string {
122+ return this . req . method ;
60123 }
61124}
62125
63126class H3EventResponse {
64127 status ?: number ;
65- // statusText?: string;
128+ statusText ?: string ;
66129 _headers ?: Headers ;
67130 get headers ( ) : Headers {
68131 if ( ! this . _headers ) {
0 commit comments