h3 0.5.6 → 0.7.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 +1 -1
- package/dist/index.cjs +9 -4
- package/dist/index.d.ts +3 -5
- package/dist/index.mjs +9 -4
- package/package.json +2 -2
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -165,7 +165,10 @@ function useCookie(event, name) {
|
|
|
165
165
|
return useCookies(event)[name];
|
|
166
166
|
}
|
|
167
167
|
function setCookie(event, name, value, serializeOptions) {
|
|
168
|
-
const cookieStr = cookieEs.serialize(name, value,
|
|
168
|
+
const cookieStr = cookieEs.serialize(name, value, {
|
|
169
|
+
path: "/",
|
|
170
|
+
...serializeOptions
|
|
171
|
+
});
|
|
169
172
|
appendHeader(event, "Set-Cookie", cookieStr);
|
|
170
173
|
}
|
|
171
174
|
function deleteCookie(event, name, serializeOptions) {
|
|
@@ -343,10 +346,12 @@ function createEvent(req, res) {
|
|
|
343
346
|
const event = {
|
|
344
347
|
__is_event__: true,
|
|
345
348
|
req,
|
|
346
|
-
res
|
|
349
|
+
res,
|
|
350
|
+
context: {}
|
|
347
351
|
};
|
|
348
352
|
event.event = event;
|
|
349
353
|
req.event = event;
|
|
354
|
+
req.context = event.context;
|
|
350
355
|
req.req = req;
|
|
351
356
|
req.res = res;
|
|
352
357
|
res.event = event;
|
|
@@ -498,8 +503,8 @@ function createRouter() {
|
|
|
498
503
|
statusMessage: `Method ${method} is not allowed on this route.`
|
|
499
504
|
});
|
|
500
505
|
}
|
|
501
|
-
event.event.params = matched.params || {};
|
|
502
|
-
event.req.params = event.event.params;
|
|
506
|
+
event.event.context.params = matched.params || {};
|
|
507
|
+
event.req.context.params = event.event.params;
|
|
503
508
|
return handler(event);
|
|
504
509
|
});
|
|
505
510
|
return router;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,10 +7,9 @@ interface H3Event {
|
|
|
7
7
|
event: H3Event;
|
|
8
8
|
req: IncomingMessage;
|
|
9
9
|
res: ServerResponse;
|
|
10
|
-
|
|
11
|
-
params?: Record<string, any>;
|
|
10
|
+
context: Record<string, any>;
|
|
12
11
|
}
|
|
13
|
-
declare type CompatibilityEvent = H3Event | IncomingMessage
|
|
12
|
+
declare type CompatibilityEvent = H3Event | IncomingMessage;
|
|
14
13
|
declare type _JSONValue<T = string | number | boolean> = T | T[] | Record<string, T>;
|
|
15
14
|
declare type JSONValue = _JSONValue<_JSONValue>;
|
|
16
15
|
declare type H3Response = void | JSONValue | Buffer;
|
|
@@ -35,10 +34,9 @@ declare function isEvent(input: any): input is H3Event;
|
|
|
35
34
|
|
|
36
35
|
interface CompatibilityRequestProps {
|
|
37
36
|
event: H3Event;
|
|
37
|
+
context: Record<string, any>;
|
|
38
38
|
/** Only available with connect and press */
|
|
39
39
|
originalUrl?: string;
|
|
40
|
-
/** Request params only filled with h3 Router handlers */
|
|
41
|
-
params?: Record<string, any>;
|
|
42
40
|
}
|
|
43
41
|
interface IncomingMessage extends http.IncomingMessage, CompatibilityRequestProps {
|
|
44
42
|
req: H3Event['req'];
|
package/dist/index.mjs
CHANGED
|
@@ -157,7 +157,10 @@ function useCookie(event, name) {
|
|
|
157
157
|
return useCookies(event)[name];
|
|
158
158
|
}
|
|
159
159
|
function setCookie(event, name, value, serializeOptions) {
|
|
160
|
-
const cookieStr = serialize(name, value,
|
|
160
|
+
const cookieStr = serialize(name, value, {
|
|
161
|
+
path: "/",
|
|
162
|
+
...serializeOptions
|
|
163
|
+
});
|
|
161
164
|
appendHeader(event, "Set-Cookie", cookieStr);
|
|
162
165
|
}
|
|
163
166
|
function deleteCookie(event, name, serializeOptions) {
|
|
@@ -335,10 +338,12 @@ function createEvent(req, res) {
|
|
|
335
338
|
const event = {
|
|
336
339
|
__is_event__: true,
|
|
337
340
|
req,
|
|
338
|
-
res
|
|
341
|
+
res,
|
|
342
|
+
context: {}
|
|
339
343
|
};
|
|
340
344
|
event.event = event;
|
|
341
345
|
req.event = event;
|
|
346
|
+
req.context = event.context;
|
|
342
347
|
req.req = req;
|
|
343
348
|
req.res = res;
|
|
344
349
|
res.event = event;
|
|
@@ -490,8 +495,8 @@ function createRouter() {
|
|
|
490
495
|
statusMessage: `Method ${method} is not allowed on this route.`
|
|
491
496
|
});
|
|
492
497
|
}
|
|
493
|
-
event.event.params = matched.params || {};
|
|
494
|
-
event.req.params = event.event.params;
|
|
498
|
+
event.event.context.params = matched.params || {};
|
|
499
|
+
event.req.context.params = event.event.params;
|
|
495
500
|
return handler(event);
|
|
496
501
|
});
|
|
497
502
|
return router;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "h3",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Tiny JavaScript Server",
|
|
5
5
|
"repository": "unjs/h3",
|
|
6
6
|
"license": "MIT",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"release": "pnpm lint && pnpm test && pnpm build && standard-version && pnpm publish && git push --follow-tags",
|
|
55
55
|
"test": "vitest run"
|
|
56
56
|
},
|
|
57
|
-
"readme": "[](https://npmjs.com/package/h3)\n[](https://npmjs.com/package/h3)\n[](https://bundlephobia.com/result?p=h3)\n[](https://github.com/unjs/h3/actions)\n[](https://codecov.io/gh/unjs/h3)\n[](https://www.jsdocs.io/package/h3)\n\n> H3 is a minimal h(ttp) framework built for high performance and portability\n\n<!--  -->\n\n## Features\n\n✔️ **Portable:** Works perfectly in Serverless, Workers, and Node.js\n\n✔️ **Compatible:** Support connect/express middleware\n\n✔️ **Minimal:** Small, tree-shakable and zero-dependency\n\n✔️ **Modern:** Native promise support\n\n✔️ **Extendable:** Ships with a set of composable utilities but can be extended\n\n✔️ **Router:** Super fast route matching using [unjs/radix3](https://github.com/unjs/radix3)\n\n## Install\n\n```bash\n# Using npm\nnpm install h3\n\n# Using yarn\nyarn add h3\n\n# Using pnpm\npnpm add h3\n```\n\n## Usage\n\n```ts\nimport { createServer } from 'http'\nimport { createApp } from 'h3'\n\nconst app = createApp()\napp.use('/', () => 'Hello world!')\n\ncreateServer(app).listen(process.env.PORT || 3000)\n```\n\n<details>\n <summary>Example using <a href=\"https://github.com/unjs/listhen\">listhen</a> for an elegant listener.</summary>\n\n```ts\nimport { createApp } from 'h3'\nimport { listen } from 'listhen'\n\nconst app = createApp()\napp.use('/', () => 'Hello world!')\n\nlisten(app)\n```\n</details>\n\n## Router\n\nThe `app` instance created by `h3` uses a middleware stack (see [how it works](#how-it-works)) with the ability to match route prefix and apply matched middleware.\n\nTo opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance.\n\n```ts\nimport { createApp, createRouter } from 'h3'\n\nconst app = createApp()\n\nconst router = createRouter()\n .get('/', () => 'Hello World!')\n .get('/hello/:name', req => `Hello ${req.params.name}!`)\n\napp.use(router)\n```\n\n**Tip:** We can register same route more than once with different methods.\n\nRoutes are internally stored in a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree) and matched using [unjs/radix3](https://github.com/unjs/radix3).\n\n## More usage examples\n\n```js\n// Handle can directly return object or Promise<object> for JSON response\napp.use('/api', (req) => ({ url: req.url }))\n\n// We can have better matching other than quick prefix match\napp.use('/odd', () => 'Is odd!', { match: url => url.substr(1) % 2 })\n\n// Handle can directly return string for HTML response\napp.use(() => '<h1>Hello world!</h1>')\n\n// We can chain calls to .use()\napp.use('/1', () => '<h1>Hello world!</h1>')\n .use('/2', () => '<h1>Goodbye!</h1>')\n\n// Legacy middleware with 3rd argument are automatically promisified\napp.use((req, res, next) => { req.setHeader('X-Foo', 'bar'); next() })\n\n// Force promisify a legacy middleware\n// app.use(someMiddleware, { promisify: true })\n\n// Lazy loaded routes using { lazy: true }\n// app.use('/big', () => import('./big'), { lazy: true })\n```\n\n## Utilities\n\nInstead of adding helpers to `req` and `res`, h3 exposes them as composable utilities.\n\n- `useRawBody(req, encoding?)`\n- `useBody(req)`\n- `useCookies(req)`\n- `useCookie(req, name)`\n- `setCookie(res, name, value, opts?)`\n- `deleteCookie(res, name, opts?)`\n- `useQuery(req)`\n- `send(res, data, type?)`\n- `sendRedirect(res, location, code=302)`\n- `appendHeader(res, name, value)`\n- `createError({ statusCode, statusMessage, data? })`\n- `sendError(res, error, debug?)`\n- `defineHandle(handle)`\n- `defineMiddleware(middlware)`\n- `useMethod(req, default?)`\n- `isMethod(req, expected, allowHead?)`\n- `assertMethod(req, expected, allowHead?)`\n\n👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).\n\n## How it works?\n\nUsing `createApp`, it returns a standard `(req, res)` handler function and internally an array called middleware stack. using`use()` method we can add an item to this internal stack.\n\nWhen a request comes, each stack item that matches the route will be called and resolved until [`res.writableEnded`](https://nodejs.org/api/http.html#http_response_writableended) flag is set, which means the response is sent. If `writableEnded` is not set after all middleware, a `404` error will be thrown. And if one of the stack items resolves to a value, it will be serialized and sent as response as a shorthand method to sending responses.\n\nFor maximum compatibility with connect/express middleware (`req, res, next?` signature), h3 converts classic middleware into a promisified version ready to use with stack runner:\n\n- If middleware has 3rd next/callback param, the promise will `resolve/reject` when called\n- If middleware returns a promise, it will be **chained** to the main promise\n- If calling middleware throws an immediate error, the promise will be rejected\n- On `close` and `error` events of res, the promise will `resolve/reject` (to ensure if middleware simply calls `res.end`)\n\n## License\n\nMIT\n"
|
|
57
|
+
"readme": "[](https://npmjs.com/package/h3)\n[](https://npmjs.com/package/h3)\n[](https://bundlephobia.com/result?p=h3)\n[](https://github.com/unjs/h3/actions)\n[](https://codecov.io/gh/unjs/h3)\n[](https://www.jsdocs.io/package/h3)\n\n> H3 is a minimal h(ttp) framework built for high performance and portability\n\n<!--  -->\n\n## Features\n\n✔️ **Portable:** Works perfectly in Serverless, Workers, and Node.js\n\n✔️ **Compatible:** Support connect/express middleware\n\n✔️ **Minimal:** Small, tree-shakable and zero-dependency\n\n✔️ **Modern:** Native promise support\n\n✔️ **Extendable:** Ships with a set of composable utilities but can be extended\n\n✔️ **Router:** Super fast route matching using [unjs/radix3](https://github.com/unjs/radix3)\n\n## Install\n\n```bash\n# Using npm\nnpm install h3\n\n# Using yarn\nyarn add h3\n\n# Using pnpm\npnpm add h3\n```\n\n## Usage\n\n```ts\nimport { createServer } from 'http'\nimport { createApp } from 'h3'\n\nconst app = createApp()\napp.use('/', () => 'Hello world!')\n\ncreateServer(app).listen(process.env.PORT || 3000)\n```\n\n<details>\n <summary>Example using <a href=\"https://github.com/unjs/listhen\">listhen</a> for an elegant listener.</summary>\n\n```ts\nimport { createApp } from 'h3'\nimport { listen } from 'listhen'\n\nconst app = createApp()\napp.use('/', () => 'Hello world!')\n\nlisten(app)\n```\n</details>\n\n## Router\n\nThe `app` instance created by `h3` uses a middleware stack (see [how it works](#how-it-works)) with the ability to match route prefix and apply matched middleware.\n\nTo opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance.\n\n```ts\nimport { createApp, createRouter } from 'h3'\n\nconst app = createApp()\n\nconst router = createRouter()\n .get('/', () => 'Hello World!')\n .get('/hello/:name', req => `Hello ${req.context.params.name}!`)\n\napp.use(router)\n```\n\n**Tip:** We can register same route more than once with different methods.\n\nRoutes are internally stored in a [Radix Tree](https://en.wikipedia.org/wiki/Radix_tree) and matched using [unjs/radix3](https://github.com/unjs/radix3).\n\n## More usage examples\n\n```js\n// Handle can directly return object or Promise<object> for JSON response\napp.use('/api', (req) => ({ url: req.url }))\n\n// We can have better matching other than quick prefix match\napp.use('/odd', () => 'Is odd!', { match: url => url.substr(1) % 2 })\n\n// Handle can directly return string for HTML response\napp.use(() => '<h1>Hello world!</h1>')\n\n// We can chain calls to .use()\napp.use('/1', () => '<h1>Hello world!</h1>')\n .use('/2', () => '<h1>Goodbye!</h1>')\n\n// Legacy middleware with 3rd argument are automatically promisified\napp.use((req, res, next) => { req.setHeader('X-Foo', 'bar'); next() })\n\n// Force promisify a legacy middleware\n// app.use(someMiddleware, { promisify: true })\n\n// Lazy loaded routes using { lazy: true }\n// app.use('/big', () => import('./big'), { lazy: true })\n```\n\n## Utilities\n\nInstead of adding helpers to `req` and `res`, h3 exposes them as composable utilities.\n\n- `useRawBody(req, encoding?)`\n- `useBody(req)`\n- `useCookies(req)`\n- `useCookie(req, name)`\n- `setCookie(res, name, value, opts?)`\n- `deleteCookie(res, name, opts?)`\n- `useQuery(req)`\n- `send(res, data, type?)`\n- `sendRedirect(res, location, code=302)`\n- `appendHeader(res, name, value)`\n- `createError({ statusCode, statusMessage, data? })`\n- `sendError(res, error, debug?)`\n- `defineHandle(handle)`\n- `defineMiddleware(middlware)`\n- `useMethod(req, default?)`\n- `isMethod(req, expected, allowHead?)`\n- `assertMethod(req, expected, allowHead?)`\n\n👉 You can learn more about usage in [JSDocs Documentation](https://www.jsdocs.io/package/h3#package-functions).\n\n## How it works?\n\nUsing `createApp`, it returns a standard `(req, res)` handler function and internally an array called middleware stack. using`use()` method we can add an item to this internal stack.\n\nWhen a request comes, each stack item that matches the route will be called and resolved until [`res.writableEnded`](https://nodejs.org/api/http.html#http_response_writableended) flag is set, which means the response is sent. If `writableEnded` is not set after all middleware, a `404` error will be thrown. And if one of the stack items resolves to a value, it will be serialized and sent as response as a shorthand method to sending responses.\n\nFor maximum compatibility with connect/express middleware (`req, res, next?` signature), h3 converts classic middleware into a promisified version ready to use with stack runner:\n\n- If middleware has 3rd next/callback param, the promise will `resolve/reject` when called\n- If middleware returns a promise, it will be **chained** to the main promise\n- If calling middleware throws an immediate error, the promise will be rejected\n- On `close` and `error` events of res, the promise will `resolve/reject` (to ensure if middleware simply calls `res.end`)\n\n## License\n\nMIT\n"
|
|
58
58
|
}
|