h3 2.0.1-rc.2 → 2.0.1-rc.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/bin/h3.mjs +35 -0
  2. package/dist/THIRD-PARTY-LICENSES.md +70 -0
  3. package/dist/_entries/bun.d.mts +3 -8
  4. package/dist/_entries/bun.mjs +3 -9
  5. package/dist/_entries/cloudflare.d.mts +4 -9
  6. package/dist/_entries/cloudflare.mjs +3 -9
  7. package/dist/_entries/deno.d.mts +3 -8
  8. package/dist/_entries/deno.mjs +3 -9
  9. package/dist/_entries/generic.d.mts +3 -8
  10. package/dist/_entries/generic.mjs +3 -9
  11. package/dist/_entries/node.d.mts +3 -8
  12. package/dist/_entries/node.mjs +3 -12
  13. package/dist/_entries/service-worker.d.mts +3 -8
  14. package/dist/_entries/service-worker.mjs +3 -9
  15. package/dist/docs/0.guide/0.index/index.md +117 -0
  16. package/dist/docs/0.guide/1.basics/0.lifecycle.md +68 -0
  17. package/dist/docs/0.guide/1.basics/1.routing.md +92 -0
  18. package/dist/docs/0.guide/1.basics/2.middleware.md +97 -0
  19. package/dist/docs/0.guide/1.basics/3.handler.md +165 -0
  20. package/dist/docs/0.guide/1.basics/4.response.md +162 -0
  21. package/dist/docs/0.guide/1.basics/5.error.md +117 -0
  22. package/dist/docs/0.guide/1.basics/6.nested-apps.md +57 -0
  23. package/dist/docs/0.guide/2.api/0.h3.md +145 -0
  24. package/dist/docs/0.guide/2.api/1.h3event.md +113 -0
  25. package/dist/docs/0.guide/3.advanced/0.plugins.md +50 -0
  26. package/dist/docs/0.guide/3.advanced/1.websocket.md +124 -0
  27. package/dist/docs/0.guide/3.advanced/2.nightly.md +13 -0
  28. package/dist/docs/1.utils/0.index/index.md +46 -0
  29. package/dist/docs/1.utils/1.request.md +355 -0
  30. package/dist/docs/1.utils/2.response.md +144 -0
  31. package/dist/docs/1.utils/3.cookie.md +33 -0
  32. package/dist/docs/1.utils/4.security.md +109 -0
  33. package/dist/docs/1.utils/5.proxy.md +31 -0
  34. package/dist/docs/1.utils/6.mcp.md +71 -0
  35. package/dist/docs/1.utils/7.more.md +78 -0
  36. package/dist/docs/1.utils/8.community.md +42 -0
  37. package/dist/docs/2.examples/0.index/index.md +16 -0
  38. package/dist/docs/2.examples/1.handle-cookie.md +67 -0
  39. package/dist/docs/2.examples/2.handle-session.md +130 -0
  40. package/dist/docs/2.examples/3.serve-static-assets.md +66 -0
  41. package/dist/docs/2.examples/4.stream-response.md +76 -0
  42. package/dist/docs/2.examples/5.validate-data.md +193 -0
  43. package/dist/docs/3.migration/0.index/index.md +200 -0
  44. package/dist/docs/README.md +35 -0
  45. package/dist/{h3.mjs → h3-CRCltuUf.mjs} +915 -1218
  46. package/dist/h3-D76FUMrE.d.mts +833 -0
  47. package/dist/h3-DagAgogP.mjs +4 -0
  48. package/dist/{h3.d.mts → h3-DiSMXP1G.d.mts} +320 -656
  49. package/dist/tracing.d.mts +24 -0
  50. package/dist/tracing.mjs +76 -0
  51. package/package.json +56 -44
@@ -0,0 +1,165 @@
1
+ # Event Handlers
2
+
3
+ > An event handler is a function that receives an H3Event and returns a response.
4
+
5
+ You can define typed event handlers using `defineHandler`.
6
+
7
+ ```js
8
+ import { H3, defineHandler } from "h3";
9
+
10
+ const app = new H3();
11
+
12
+ const handler = defineHandler((event) => "Response");
13
+
14
+ app.get("/", handler);
15
+ ```
16
+
17
+ > [!NOTE]
18
+ > Using `defineHandler` is optional.
19
+ > You can instead, simply use a function that accepts an [`H3Event`](/guide/api/h3event) and returns a response.
20
+
21
+ The callback function can be sync or async:
22
+
23
+ ```js
24
+ defineHandler(async (event) => "Response");
25
+ ```
26
+
27
+ ## Object Syntax
28
+
29
+ ### middleware
30
+
31
+ You can optionally register some [middleware](/guide/basics/middleware) to run with event handler to intercept request, response or errors.
32
+
33
+ ```js
34
+ import { basicAuth } from "h3";
35
+
36
+ defineHandler({
37
+ middleware: [basicAuth({ password: "test" })],
38
+ handler: (event) => "Hi!",
39
+ });
40
+ ```
41
+
42
+ <read-more></read-more>
43
+
44
+ <read-more></read-more>
45
+
46
+ ### meta
47
+
48
+ You can define optional route meta attached to handlers, and access them from any other middleware.
49
+
50
+ ```js
51
+ import { H3, defineHandler } from "h3";
52
+
53
+ const app = new H3();
54
+
55
+ app.use((event) => {
56
+ console.log(event.context.matchedRoute?.meta); // { tag: "admin" }
57
+ });
58
+
59
+ app.get("/admin/**", defineHandler({
60
+ meta: { tag: "admin" },
61
+ handler: (event) => "Hi!",
62
+ })
63
+ ```
64
+
65
+ <read-more>
66
+
67
+ It is also possible to add route meta when registering them to app instance.
68
+ </read-more>
69
+
70
+ ## Handler `.fetch`
71
+
72
+ Event handlers defined with `defineHandler`, can act as a web handler without even using [H3](/guide/api/h3) class.
73
+
74
+ ```js
75
+ const handler = defineHandler(async (event) => `Request: ${event.req.url}`);
76
+
77
+ const response = await handler.fetch("http://localhost/");
78
+ console.log(response, await response.text());
79
+ ```
80
+
81
+ ## Lazy Handlers
82
+
83
+ You can define lazy event handlers using `defineLazyEventHandler`. This allow you to define some one-time logic that will be executed only once when the first request matching the route is received.
84
+
85
+ A lazy event handler must return an event handler.
86
+
87
+ ```js
88
+ import { defineLazyEventHandler } from "h3";
89
+
90
+ defineLazyEventHandler(async () => {
91
+ await initSomething(); // Will be executed only once
92
+ return (event) => {
93
+ return "Response";
94
+ };
95
+ });
96
+ ```
97
+
98
+ This is useful to define some one-time logic such as configuration, class initialization, heavy computation, etc.
99
+
100
+ Another use-case is lazy loading route chunks:
101
+
102
+ ```js [app.mjs]
103
+ import { H3, defineLazyEventHandler } from "h3";
104
+
105
+ const app = new H3();
106
+
107
+ app.all(
108
+ "/route",
109
+ defineLazyEventHandler(() => import("./route.mjs").then((mod) => mod.default)),
110
+ );
111
+ ```
112
+
113
+ ```js [route.mjs]
114
+ import { defineHandler } from "h3";
115
+
116
+ export default defineHandler((event) => "Hello!");
117
+ ```
118
+
119
+ ## Converting to Handler
120
+
121
+ There are situations that you might want to convert an event handler or utility made for Node.js or another framework to H3.
122
+ There are built-in utils to do this.
123
+
124
+ ### From Web Handlers
125
+
126
+ Request handlers with [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) => [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) signuture can be converted into H3 event handlers using `fromWebHandler` utility or [H3.mount](/guide/api/h3#h3mount).
127
+
128
+ ```js
129
+ import { H3, fromWebHandler } from "h3";
130
+
131
+ export const app = new H3();
132
+
133
+ const webHandler = (request) => new Response("👋 Hello!");
134
+
135
+ // Using fromWebHandler utiliy
136
+ app.all("/web", fromWebHandler(webHandler));
137
+
138
+ // Using simple wrapper
139
+ app.all("/web", (event) => webHandler(event.req));
140
+
141
+ // Using app.mount
142
+ app.mount("/web", webHandler);
143
+ ```
144
+
145
+ ### From Node.js Handlers
146
+
147
+ If you have a legacy request handler with `(req, res) => {}` syntax made for Node.js, you can use `fromNodeHandler` to convert it to an h3 event handler.
148
+
149
+ > [!IMPORTANT]
150
+ > Node.js event handlers can only run within Node.js server runtime!
151
+
152
+ ```js
153
+ import { H3, fromNodeHandler } from "h3";
154
+
155
+ // Force using Node.js compatibility (also works with Bun and Deno)
156
+ import { serve } from "h3/node";
157
+
158
+ export const app = new H3();
159
+
160
+ const nodeHandler = (req, res) => {
161
+ res.end("Node handlers work!");
162
+ };
163
+
164
+ app.get("/web", fromNodeHandler(nodeHandler));
165
+ ```
@@ -0,0 +1,162 @@
1
+ # Sending Response
2
+
3
+ > H3 automatically converts any returned value into a web response.
4
+
5
+ Values returned from [Event Handlers](/guide/basics/handler) are automatically converted to a web [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) by H3.
6
+
7
+ **Example:** Simple event handler function.
8
+
9
+ ```js
10
+ const handler = defineHandler((event) => ({ hello: "world" }));
11
+ ```
12
+
13
+ H3 smartly converts handler into:
14
+
15
+ ```js
16
+ const handler = (event) =>
17
+ new Response(JSON.stringify({ hello: "world" }), {
18
+ headers: {
19
+ "content-type": "application/json;charset=UTF-8",
20
+ },
21
+ });
22
+ ```
23
+
24
+ > [!TIP]
25
+ > 🚀 H3 uses srvx `FastResponse` internally to optimize performances in Node.js runtime.
26
+
27
+ If the returned value from event handler is a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) or from an [async function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), H3 will wait for it to resolve before sending the response.
28
+
29
+ If an error is thrown, H3 automatically handles it with error handler.
30
+
31
+ <read-more></read-more>
32
+
33
+ ## Preparing Response
34
+
35
+ Before returning a response in main handler, you can prepare response headers and status using [`event.res`](/guide/api/h3event#eventres).
36
+
37
+ ```js
38
+ defineHandler((event) => {
39
+ event.res.status = 200;
40
+ event.res.statusText = "OK";
41
+ event.res.headers.set("Content-Type", "text/html");
42
+ return "<h1>Hello, World</h1>";
43
+ });
44
+ ```
45
+
46
+ > [!NOTE]
47
+ > If a full [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response) value is returned, prepared status is discarded and headers will be merged/overriden. For performance reasons, it is best to only set headers only from final Response in this case.
48
+
49
+ > [!NOTE]
50
+ > If an Error happens, prepared status and headers will be discarded. The recommended way to include headers in error responses is via `new HTTPError({ headers })`. As a last resort for headers that need to be set implicitly before the error is known (e.g., CORS), you can use `event.res.errHeaders` — these will be merged into error responses automatically.
51
+
52
+ ## Response Types
53
+
54
+ H3 smartly converts JavaScript values into web [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response).
55
+
56
+ ### JSON Serializable Value
57
+
58
+ Returning a [JSON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) serializable value (**object**, **array**, **number** or **boolean**), it will be stringified using [JSON.stringify()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) and sent with default `application/json` content-type.
59
+
60
+ **Example:**
61
+
62
+ ```ts
63
+ app.get("/", (event) => ({ hello: "world" }));
64
+ ```
65
+
66
+ > [!TIP]
67
+ > Returned objects with `.toJSON()` property can customize serialization behavior. Check [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) for more info.
68
+
69
+ ### String
70
+
71
+ Returning a string value, sends it as plain text body.
72
+
73
+ > [!NOTE]
74
+ > If not setting `content-type` header, it can default to `text/plain;charset=UTF-8`.
75
+
76
+ **Example:** Send HTML response.
77
+
78
+ ```ts
79
+ app.get("/", (event) => {
80
+ event.res.headers.set("Content-Type", "text/html;charset=UTF-8");
81
+ return "<h1>hello world</h1>";
82
+ });
83
+ ```
84
+
85
+ You can also use `html` utility as shortcut.
86
+
87
+ ```js
88
+ import { html } from "h3";
89
+
90
+ app.get("/", () => html("<h1>hello world</h1>"));
91
+ ```
92
+
93
+ ### `Response`
94
+
95
+ Returning a web [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response/Response), sends-it as final reponse.
96
+
97
+ **Example:**
98
+
99
+ ```ts
100
+ app.get("/", (event) => new Response("Hello, world!", { headers: { "x-powered-by": "H3" } }));
101
+ ```
102
+
103
+ > [!IMPORTANT]
104
+ > When sending a `Response`, any [prepared headers](#preparing-response) that set before, will be merged as default headers. `event.res.{status,statusText}` will be ignored. For performance reasons, it is best to only set headers only from final `Response`.
105
+
106
+ ### `ReadableStream` or `Readable`
107
+
108
+ Returning a [`ReadableStream`](https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream) or Node.js [`Readable`](https://nodejs.org/api/stream.html#readable-streams) sends it as stream.
109
+
110
+ ### `ArrayBuffer` or `Uint8Array` or `Buffer`
111
+
112
+ Send binary [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer), [Uint8Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array) or node [Buffer](https://nodejs.org/api/buffer.html#buffer).
113
+
114
+ `content-length` header will be automatically set.
115
+
116
+ ### `Blob`
117
+
118
+ Send a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob) as stream.
119
+
120
+ `Content-type` and `Content-Length` headers will be automatically set.
121
+
122
+ ### `File`
123
+
124
+ Send a [`File`](https://developer.mozilla.org/en-US/docs/Web/API/File) as stream.
125
+
126
+ `Content-type`, `Content-Length` and `Content-Disposition` headers will be automatically set.
127
+
128
+ ## Special Types
129
+
130
+ Some less commonly possible values for response types.
131
+
132
+ ### `null` or `undefined`
133
+
134
+ Sends a response with empty body.
135
+
136
+ > [!TIP]
137
+ > If there is no `return` statement in event handler, it is same as `return undefined`.
138
+
139
+ ### `Error`
140
+
141
+ Retuning an [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) instance will send it.
142
+
143
+ > [!IMPORTANT]
144
+ > It is better to `throw` errors instead of returning them. This allows proper propagation from any nested utility.
145
+
146
+ <read-more></read-more>
147
+
148
+ ### `BigInt`
149
+
150
+ Value will be sent as stringified version of BigInt number.
151
+
152
+ > [!NOTE]
153
+ > Returning a JSON object, does not allows BigInt serialization. You need to implement `.toJSON`. Check [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) for more info.
154
+
155
+ ### `Symbol` or `Function`
156
+
157
+ **Returning Symbol or Function has undetermined behavior.** Currently, H3 sends a string-like representation of unknown Symbols and Functions but this behavior might be changed to throw an error in the future versions.
158
+
159
+ There are some internal known Symbols H3 internally uses:
160
+
161
+ - `Symbol.for("h3.notFound")`: Indicate no route is found to throw a 404 error.
162
+ - `Symbol.for("h3.handled")`: Indicate request is somehow handled and H3 should not continue (Node.js specific).
@@ -0,0 +1,117 @@
1
+ # Error Handling
2
+
3
+ > Send errors by throwing an HTTPError.
4
+
5
+ H3 captures all possible errors during [request lifecycle](/guide/basics/lifecycle).
6
+
7
+ ## `HTTPError`
8
+
9
+ You can create and throw HTTP errors using `HTTPError` with different syntaxes.
10
+
11
+ ```js
12
+ import { HTTPError } from "h3";
13
+
14
+ app.get("/error", (event) => {
15
+ // Using message and details
16
+ throw new HTTPError("Invalid user input", { status: 400 });
17
+
18
+ // Using HTTPError.status(code)
19
+ throw HTTPError.status(400, "Bad Request");
20
+
21
+ // Using single object
22
+ throw new HTTPError({
23
+ status: 400,
24
+ statusText: "Bad Request",
25
+ message: "Invalid user input",
26
+ data: { field: "email" },
27
+ body: { date: new Date().toJSON() },
28
+ headers: {},
29
+ });
30
+ });
31
+ ```
32
+
33
+ This will end the request with `400 - Bad Request` status code and the following JSON response:
34
+
35
+ ```json
36
+ {
37
+ "date": "2025-06-05T04:20:00.0Z",
38
+ "status": 400,
39
+ "statusText": "Bad Request",
40
+ "message": "Invalid user input",
41
+ "data": {
42
+ "field": "email"
43
+ }
44
+ }
45
+ ```
46
+
47
+ ### `HTTPError` Fields
48
+
49
+ - `status`: HTTP status code in the range 200–599.
50
+ - `statusText`: HTTP status text to be sent in the response header.
51
+ - `message`: Error message to be included in the JSON body.
52
+ - `data`: Additional data to be attached under the `data` key in the error JSON body.
53
+ - `body`: Additional top-level properties to be attached in the error JSON body.
54
+ - `headers`: Additional HTTP headers to be sent in the error response.
55
+ - `cause`: The original error object that caused this error, useful for tracing and debugging.
56
+ - `unhandled`: Indicates whether the error was thrown for unknown reasons. See [Unhandled Errors](#unhandled-errors).
57
+
58
+ > [!TIP]
59
+ The recommended way to include headers in error responses is to use `new HTTPError({ headers })`:
60
+
61
+ > ```js
62
+ > throw new HTTPError({
63
+ > status: 400,
64
+ > message: "Invalid input",
65
+ > headers: { "x-request-id": requestId },
66
+ > });
67
+ > ```
68
+
69
+ > When an error is thrown, any [prepared headers](/guide/basics/response#preparing-response) set via `event.res.headers` are **not** included in the error response. As a last resort for headers that need to be set implicitly before the error is known (e.g., CORS headers), you can use `event.res.errHeaders`. Built-in utilities like `handleCors` automatically set both.
70
+
71
+ > [!IMPORTANT]
72
+ > Error `statusText` should be short (max 512 to 1024 characters) and only include tab, spaces or visible ASCII characters and extended characters (byte value 128–255). Prefer `message` in JSON body for extended message.
73
+
74
+ ## Unhandled Errors
75
+
76
+ Any error that occurs during calling [request lifecycle](/guide/basics/lifecycle) without using `HTTPError` will be processed as an <u>unhandled</u> error.
77
+
78
+ ```js
79
+ app.get("/error", (event) => {
80
+ // This will cause an unhandled error.
81
+ throw new Error("Something went wrong");
82
+ });
83
+ ```
84
+
85
+ > [!TIP]
86
+ > For enhanced security, H3 hides certain fields of unhandled errors (`data`, `body`, `stack` and `message`) in JSON response.
87
+
88
+ ## Catching Errors
89
+
90
+ Using global [`onError`](/guide/api/h3#global-hooks) hook:
91
+
92
+ ```js
93
+ import { H3, onError } from "h3";
94
+
95
+ // Globally handling errors
96
+ const app = new H3({
97
+ onError: (error) => {
98
+ console.error(error);
99
+ },
100
+ });
101
+ ```
102
+
103
+ Using [`onError` middleware](/guide/basics/middleware) to catch errors.
104
+
105
+ ```js
106
+ import { onError } from "h3";
107
+
108
+ // Handling errors using middleware
109
+ app.use(
110
+ onError((error, event) => {
111
+ console.error(error);
112
+ }),
113
+ );
114
+ ```
115
+
116
+ > [!TIP]
117
+ > When using nested apps, global hooks of sub-apps will not be called. Therefore it is better to use `onError` middleware.
@@ -0,0 +1,57 @@
1
+ # Nested Apps
2
+
3
+ > H3 has a native `mount` method for adding nested sub-apps to the main instance.
4
+
5
+ Typically, H3 projects consist of several [Event Handlers](/guide/basics/handler) defined in one or multiple files (or even [lazy loaded](/guide/basics/handler#lazy-handlers) for faster startup times).
6
+
7
+ It is sometimes more convenient to combine multiple `H3` instances or even use another HTTP framework used by a different team and mount it to the main app instance. H3 provides a native [`.mount`](/guide/api/h3#h3mount) method to facilitate this.
8
+
9
+ ## Nested H3 Apps
10
+
11
+ H3 natively allows mounting sub-apps. When mounted, sub-app routes and middleware are **merged** with the base url prefix into the main app instance.
12
+
13
+ ```js
14
+ import { H3, serve } from "h3";
15
+
16
+ const nestedApp = new H3()
17
+ .use((event) => {
18
+ event.res.headers.set("x-api", "1");
19
+ })
20
+ .get("/**:slug", (event) => ({
21
+ pathname: event.url.pathname,
22
+ slug: event.context.params?.slug,
23
+ }));
24
+
25
+ const app = new H3().mount("/api", nestedApp);
26
+ ```
27
+
28
+ In the example above, when fetching the `/api/test` URL, `pathname` will be `/api/test` (the real path), and `slug` will be `/test` (wildcard param).
29
+
30
+ > [!NOTE]
31
+ > Global config and hooks won't be inherited from the nested app. Consider always setting them from the main app.
32
+
33
+ ## Nested Web Standard Apps
34
+
35
+ Mount a `.fetch` compatible server instance like [Hono](https://hono.dev/) or [Elysia](https://elysiajs.com/) under the base URL.
36
+
37
+ > [!NOTE]
38
+ > Base prefix will be removed from `request.url` passed to the mounted app.
39
+
40
+ ```js
41
+ import { H3 } from "h3";
42
+ import { Hono } from "hono";
43
+ import { Elysia } from "elysia";
44
+
45
+ const app = new H3()
46
+ .mount(
47
+ "/elysia",
48
+ new Elysia().get("/test", () => "Hello Elysia!"),
49
+ )
50
+ .mount(
51
+ "/hono",
52
+ new Hono().get("/test", (c) => c.text("Hello Hono!")),
53
+ );
54
+ ```
55
+
56
+ > [!TIP]
57
+ > Similarly, you can mount an H3 app in [Hono](https://hono.dev/docs/api/hono#mount) or [Elysia](https://elysiajs.com/patterns/mount#mount-1).
@@ -0,0 +1,145 @@
1
+ # H3
2
+
3
+ > H3 class is the core of server.
4
+
5
+ You can create a new H3 app instance using `new H3()`:
6
+
7
+ ```js
8
+ import { H3 } from "h3";
9
+
10
+ const app = new H3({
11
+ /* optional config */
12
+ });
13
+ ```
14
+
15
+ ## `H3` Methods
16
+
17
+ ### `H3.request`
18
+
19
+ A [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)-compatible function allowing to fetch app routes.
20
+
21
+ - Input can be a relative path, [URL](https://developer.mozilla.org/en-US/docs/Web/API/URL), or [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request).
22
+ - Returned value is a [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) promise.
23
+
24
+ ```ts
25
+ const response = await app.request("/");
26
+ console.log(response, await response.text());
27
+ ```
28
+
29
+ ### `H3.fetch`
30
+ Similar to `H3.request` but only accepts one `(req: Request)` argument for cross runtime compatibility.
31
+
32
+ ### `H3.on`
33
+
34
+ Register route handler for specific HTTP method.
35
+
36
+ ```js
37
+ const app = new H3().on("GET", "/", () => "OK");
38
+ ```
39
+
40
+ <read-more></read-more>
41
+
42
+ ### `H3.[method]`
43
+
44
+ Register route handler for specific HTTP method (shortcut for `app.on(method, ...)`).
45
+
46
+ ```js
47
+ const app = new H3().get("/", () => "OK");
48
+ ```
49
+
50
+ ### `H3.all`
51
+
52
+ Register route handler for all HTTP methods.
53
+
54
+ ```js
55
+ const app = new H3().all("/", () => "OK");
56
+ ```
57
+
58
+ ### `H3.use`
59
+
60
+ Register a global [middleware](/guide/basics/middleware).
61
+
62
+ ```js
63
+ const app = new H3()
64
+ .use((event) => {
65
+ console.log(`request: ${event.req.url}`);
66
+ })
67
+ .all("/", () => "OK");
68
+ ```
69
+
70
+ <read-more></read-more>
71
+
72
+ ### `H3.register`
73
+
74
+ Register a H3 plugin to extend app.
75
+
76
+ <read-more></read-more>
77
+
78
+ ### `H3.handler`
79
+
80
+ An H3 [event handler](/guide/basics/handler) useful to compose multiple H3 app instances.
81
+
82
+ **Example:** Nested apps.
83
+
84
+ ```js
85
+ import { H3, serve, redirect, withBase } from "h3";
86
+
87
+ const nestedApp = new H3().get("/test", () => "/test (sub app)");
88
+
89
+ const app = new H3()
90
+ .get("/", (event) => redirect(event, "/api/test"))
91
+ .all("/api/**", withBase("/api", nestedApp.handler));
92
+
93
+ serve(app);
94
+ ```
95
+
96
+ ### `H3.mount`
97
+
98
+ Using `.mount` method, you can register a sub-app with prefix.
99
+
100
+ <read-more></read-more>
101
+
102
+ ## `H3` Options
103
+
104
+ You can pass global app configuration when initializing an app.
105
+
106
+ Supported options:
107
+
108
+ - `debug`: Displays debugging stack traces in HTTP responses (potentially dangerous for production!).
109
+ - `silent`: When enabled, console errors for unhandled exceptions will not be displayed.
110
+ - `plugins`: (see [plugins](/guide/advanced/plugins) for more information)
111
+
112
+ > [!IMPORTANT]
113
+ Enabling `debug` option, sends important stuff like stack traces in error responses. Only enable during development.
114
+
115
+ ### Global Hooks
116
+
117
+ When initializing an H3 app, you can register global hooks:
118
+
119
+ - `onError`
120
+ - `onRequest`
121
+ - `onResponse`
122
+ These hooks are called for every request and can be used to add global logic to your app such as logging, error handling, etc.
123
+
124
+ ```js
125
+ const app = new H3({
126
+ onRequest: (event) => {
127
+ console.log("Request:", event.req.url);
128
+ },
129
+ onResponse: (response, event) => {
130
+ console.log("Response:", event.url.pathname, response.status);
131
+ },
132
+ onError: (error, event) => {
133
+ console.error(error);
134
+ },
135
+ });
136
+ ```
137
+
138
+ > [!IMPORTANT]
139
+ > Global hooks only run from main H3 app and **not** sub-apps. Use [middleware](/guide/basics/middleware) for more flexibility.
140
+
141
+ ## `H3` Properties
142
+
143
+ ### `H3.config`
144
+
145
+ Global H3 instance config.