Skip to content

Commit eaeb1e1

Browse files
committed
refactor: merge H3Event with types
1 parent 74c9174 commit eaeb1e1

28 files changed

Lines changed: 142 additions & 154 deletions

src/_deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { withBase } from "./utils/base.ts";
1111
import { sanitizeStatusCode, sanitizeStatusMessage } from "./utils/sanitize.ts";
1212

1313
import type { NodeHandler, NodeMiddleware } from "./adapters.ts";
14-
import type { H3Event } from "./types/event.ts";
14+
import type { H3Event } from "./event.ts";
1515
import type { EventHandler } from "./types/handler.ts";
1616
import type { H3Config } from "./types/h3.ts";
1717
import type {

src/adapters.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import type {
88
ServerRequest,
99
} from "srvx";
1010
import type { H3 } from "./h3.ts";
11-
import type { H3Event, H3EventContext } from "./types/event.ts";
11+
import type { H3EventContext } from "./types/context.ts";
1212
import type { EventHandler, EventHandlerResponse } from "./types/handler.ts";
13+
import type { H3Event } from "./event.ts";
1314

1415
export type NodeHandler = (
1516
req: NodeServerRequest,

src/event.ts

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
import 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

44
import { EmptyObject } from "./utils/internal/obj.ts";
55
import { 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

63126
class H3EventResponse {
64127
status?: number;
65-
// statusText?: string;
128+
statusText?: string;
66129
_headers?: Headers;
67130
get headers(): Headers {
68131
if (!this._headers) {

src/h3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { callMiddleware, normalizeMiddleware } from "./middleware.ts";
55

66
import type { RouterContext } from "rou3";
77
import type { FetchHandler, H3Config, H3Plugin } from "./types/h3.ts";
8-
import type { H3EventContext } from "./types/event.ts";
8+
import type { H3EventContext } from "./types/context.ts";
99
import type { EventHandler, Middleware } from "./types/handler.ts";
1010
import type {
1111
H3Route,

src/handler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function handlerWithFetch<
9595
_init?: RequestInit,
9696
): Promise<Response> => {
9797
const req = toRequest(_req, _init);
98-
const event = new H3Event(req);
98+
const event = new H3Event(req) as H3Event<Req>;
9999
try {
100100
return Promise.resolve(handleResponse(handler(event), event));
101101
} catch (error: any) {

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export { definePlugin } from "./types/h3.ts";
1717
export { H3 } from "./h3.ts";
1818

1919
// Event
20-
export type { H3EventContext } from "./types/event.ts";
20+
export type { H3EventContext } from "./types/context.ts";
2121
export { H3Event } from "./event.ts";
2222
export { isEvent, mockEvent } from "./utils/event.ts";
2323

src/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { routeToRegExp } from "rou3";
22
import { kNotFound } from "./response.ts";
33

44
import type { H3 } from "./h3.ts";
5-
import type { H3Event } from "./types/event.ts";
5+
import type { H3Event } from "./event.ts";
66
import type { MiddlewareOptions } from "./types/h3.ts";
77
import type { EventHandler, Middleware } from "./types/handler.ts";
88

src/response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { HTTPError } from "./error.ts";
33
import { isJSONSerializable } from "./utils/internal/object.ts";
44

55
import type { H3Config } from "./types/h3.ts";
6-
import type { H3Event } from "./types/event.ts";
6+
import type { H3Event } from "./event.ts";
77

88
export const kNotFound: symbol = /* @__PURE__ */ Symbol.for("h3.notFound");
99
export const kHandled: symbol = /* @__PURE__ */ Symbol.for("h3.handled");

src/types/context.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Session } from "../utils/session.ts";
2+
import type { H3Route } from "./h3.ts";
3+
4+
export interface H3EventContext extends Record<string, any> {
5+
/* Matched router parameters */
6+
params?: Record<string, string>;
7+
8+
/* Matched middleware parameters */
9+
middlewareParams?: Record<string, string>;
10+
11+
/**
12+
* Matched router Node
13+
*
14+
* @experimental The object structure may change in non-major version.
15+
*/
16+
matchedRoute?: H3Route;
17+
18+
/* Cached session data */
19+
sessions?: Record<string, Session>;
20+
21+
/* Trusted IP Address of client */
22+
clientAddress?: string;
23+
24+
/* Basic authentication data */
25+
basicAuth?: {
26+
username?: string;
27+
password?: string;
28+
realm?: string;
29+
};
30+
}

src/types/event.ts

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)