h3 1.7.1 → 1.8.0-rc.0
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.
- package/README.md +107 -22
- package/dist/index.cjs +420 -148
- package/dist/index.d.ts +135 -73
- package/dist/index.mjs +415 -150
- package/package.json +23 -21
package/README.md
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
|
|
10
10
|
H3 is a minimal h(ttp) framework built for high performance and portability.
|
|
11
11
|
|
|
12
|
+
👉 [Online Playground](https://stackblitz.com/github/unjs/h3/tree/main/playground?startScript=dev)
|
|
13
|
+
|
|
12
14
|
## Features
|
|
13
15
|
|
|
14
16
|
✔️ **Portable:** Works perfectly in Serverless, Workers, and Node.js
|
|
@@ -36,6 +38,45 @@ yarn add h3
|
|
|
36
38
|
pnpm add h3
|
|
37
39
|
```
|
|
38
40
|
|
|
41
|
+
<details>
|
|
42
|
+
<summary>Using Nightly Releases</summary>
|
|
43
|
+
|
|
44
|
+
If you are directly using `h3` as a dependency:
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{
|
|
48
|
+
"dependencies": {
|
|
49
|
+
"h3": "npm:h3-nightly@latest"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
If you are using a framework ([Nuxt](https://nuxt.com/) or [Nitro](https://nitro.unjs.io/)) that is using `h3`:
|
|
55
|
+
|
|
56
|
+
pnpm and yarn:
|
|
57
|
+
|
|
58
|
+
```json
|
|
59
|
+
{
|
|
60
|
+
"resolutions": {
|
|
61
|
+
"h3": "npm:h3-nightly@latest"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
npm:
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{
|
|
70
|
+
"overrides": {
|
|
71
|
+
"h3": "npm:h3-nightly@latest"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
**Note:** Make sure to recreate lockfile and `node_modules` after reinstall to avoid hoisting issues.
|
|
77
|
+
|
|
78
|
+
</details>
|
|
79
|
+
|
|
39
80
|
## Usage
|
|
40
81
|
|
|
41
82
|
```ts
|
|
@@ -94,6 +135,8 @@ app.use(router);
|
|
|
94
135
|
|
|
95
136
|
Routes are internally stored in a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree) and matched using [unjs/radix3](https://github.com/unjs/radix3).
|
|
96
137
|
|
|
138
|
+
For using nested routers, see [this example](https://stackblitz.com/edit/github-2bmusk?file=app.ts&startScript=dev)
|
|
139
|
+
|
|
97
140
|
## More app usage examples
|
|
98
141
|
|
|
99
142
|
```js
|
|
@@ -158,56 +201,98 @@ app.use("/big", () => import("./big-handler"), { lazy: true });
|
|
|
158
201
|
|
|
159
202
|
H3 has a concept of composable utilities that accept `event` (from `eventHandler((event) => {})`) as their first argument. This has several performance benefits over injecting them to `event` or `app` instances in global middleware commonly used in Node.js frameworks, such as Express. This concept means only required code is evaluated and bundled, and the rest of the utilities can be tree-shaken when not used.
|
|
160
203
|
|
|
161
|
-
|
|
204
|
+
👉 You can check list of exported built-in utils from [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).
|
|
205
|
+
|
|
206
|
+
#### Body
|
|
162
207
|
|
|
163
208
|
- `readRawBody(event, encoding?)`
|
|
164
209
|
- `readBody(event)`
|
|
165
|
-
- `
|
|
166
|
-
- `
|
|
167
|
-
|
|
168
|
-
|
|
210
|
+
- `readValidatedBody(event, validate)`
|
|
211
|
+
- `readMultipartFormData(event)`
|
|
212
|
+
|
|
213
|
+
#### Request
|
|
214
|
+
|
|
169
215
|
- `getQuery(event)`
|
|
216
|
+
- `getValidatedBody(event, validate)`
|
|
170
217
|
- `getRouterParams(event)`
|
|
171
|
-
- `
|
|
172
|
-
- `
|
|
218
|
+
- `getMethod(event, default?)`
|
|
219
|
+
- `isMethod(event, expected, allowHead?)`
|
|
220
|
+
- `assertMethod(event, expected, allowHead?)`
|
|
173
221
|
- `getRequestHeaders(event, headers)` (alias: `getHeaders`)
|
|
174
222
|
- `getRequestHeader(event, name)` (alias: `getHeader`)
|
|
223
|
+
- `getRequestURL(event)`
|
|
224
|
+
- `getRequestHost(event)`
|
|
225
|
+
- `getRequestProtocol(event)`
|
|
226
|
+
- `getRequestPath(event)`
|
|
227
|
+
|
|
228
|
+
#### Response
|
|
229
|
+
|
|
230
|
+
- `send(event, data, type?)`
|
|
231
|
+
- `sendNoContent(event, code = 204)`
|
|
232
|
+
- `setResponseStatus(event, status)`
|
|
233
|
+
- `getResponseStatus(event)`
|
|
234
|
+
- `getResponseStatusText(event)`
|
|
235
|
+
- `getResponseHeaders(event)`
|
|
236
|
+
- `getResponseHeader(event, name)`
|
|
175
237
|
- `setResponseHeaders(event, headers)` (alias: `setHeaders`)
|
|
176
238
|
- `setResponseHeader(event, name, value)` (alias: `setHeader`)
|
|
177
239
|
- `appendResponseHeaders(event, headers)` (alias: `appendHeaders`)
|
|
178
240
|
- `appendResponseHeader(event, name, value)` (alias: `appendHeader`)
|
|
179
|
-
- `
|
|
241
|
+
- `defaultContentType(event, type)`
|
|
242
|
+
- `sendRedirect(event, location, code=302)`
|
|
243
|
+
- `isStream(data)`
|
|
180
244
|
- `sendStream(event, data)`
|
|
245
|
+
- `writeEarlyHints(event, links, callback)`
|
|
246
|
+
|
|
247
|
+
#### Sanitize
|
|
248
|
+
|
|
249
|
+
- `sanitizeStatusMessage(statusMessage)`
|
|
250
|
+
- `sanitizeStatusCode(statusCode, default = 200)`
|
|
251
|
+
|
|
252
|
+
#### Error
|
|
253
|
+
|
|
181
254
|
- `sendError(event, error, debug?)`
|
|
182
|
-
- `getMethod(event, default?)`
|
|
183
|
-
- `isMethod(event, expected, allowHead?)`
|
|
184
|
-
- `assertMethod(event, expected, allowHead?)`
|
|
185
255
|
- `createError({ statusCode, statusMessage, data? })`
|
|
256
|
+
|
|
257
|
+
#### Route
|
|
258
|
+
|
|
259
|
+
- `useBase(base, handler)`
|
|
260
|
+
|
|
261
|
+
#### Proxy
|
|
262
|
+
|
|
186
263
|
- `sendProxy(event, { target, ...options })`
|
|
187
264
|
- `proxyRequest(event, { target, ...options })`
|
|
188
265
|
- `fetchWithEvent(event, req, init, { fetch? }?)`
|
|
189
266
|
- `getProxyRequestHeaders(event)`
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
- `
|
|
194
|
-
- `
|
|
267
|
+
|
|
268
|
+
#### Cookie
|
|
269
|
+
|
|
270
|
+
- `parseCookies(event)`
|
|
271
|
+
- `getCookie(event, name)`
|
|
272
|
+
- `setCookie(event, name, value, opts?)`
|
|
273
|
+
- `deleteCookie(event, name, opts?)`
|
|
274
|
+
- `splitCookiesString(cookiesString)`
|
|
275
|
+
|
|
276
|
+
#### Session
|
|
277
|
+
|
|
195
278
|
- `useSession(event, config = { password, maxAge?, name?, cookie?, seal?, crypto? })`
|
|
196
279
|
- `getSession(event, config)`
|
|
197
280
|
- `updateSession(event, config, update)`
|
|
198
|
-
- `clearSession(event, config)`
|
|
199
281
|
- `sealSession(event, config)`
|
|
200
282
|
- `unsealSession(event, config, sealed)`
|
|
283
|
+
- `clearSession(event, config)`
|
|
284
|
+
|
|
285
|
+
#### Cache
|
|
286
|
+
|
|
287
|
+
- `handleCacheHeaders(event, opts)`
|
|
288
|
+
|
|
289
|
+
#### Cors
|
|
290
|
+
|
|
201
291
|
- `handleCors(options)` (see [h3-cors](https://github.com/NozomuIkuta/h3-cors) for more detail about options)
|
|
202
292
|
- `isPreflightRequest(event)`
|
|
203
293
|
- `isCorsOriginAllowed(event)`
|
|
204
294
|
- `appendCorsHeaders(event, options)` (see [h3-cors](https://github.com/NozomuIkuta/h3-cors) for more detail about options)
|
|
205
295
|
- `appendCorsPreflightHeaders(event, options)` (see [h3-cors](https://github.com/NozomuIkuta/h3-cors) for more detail about options)
|
|
206
|
-
- `getRequestHost(event)`
|
|
207
|
-
- `getRequestProtocol(event)`
|
|
208
|
-
- `getRequestURL(event)`
|
|
209
|
-
|
|
210
|
-
👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).
|
|
211
296
|
|
|
212
297
|
## Community Packages
|
|
213
298
|
|