h3 0.7.6 → 0.7.9
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/dist/index.cjs +4 -1
- package/dist/index.d.ts +10 -16
- package/dist/index.mjs +4 -1
- package/package.json +8 -8
package/dist/index.cjs
CHANGED
|
@@ -187,10 +187,13 @@ class H3Error extends Error {
|
|
|
187
187
|
}
|
|
188
188
|
}
|
|
189
189
|
function createError(input) {
|
|
190
|
+
if (typeof input === "string") {
|
|
191
|
+
return new H3Error(input);
|
|
192
|
+
}
|
|
190
193
|
if (input instanceof H3Error) {
|
|
191
194
|
return input;
|
|
192
195
|
}
|
|
193
|
-
const err = new H3Error(input.message ?? input.statusMessage);
|
|
196
|
+
const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : void 0);
|
|
194
197
|
if (input.statusCode) {
|
|
195
198
|
err.statusCode = input.statusCode;
|
|
196
199
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -2,28 +2,22 @@ import http from 'http';
|
|
|
2
2
|
import { CookieSerializeOptions } from 'cookie-es';
|
|
3
3
|
import * as ufo from 'ufo';
|
|
4
4
|
|
|
5
|
+
interface H3EventContext extends Record<string, any> {
|
|
6
|
+
}
|
|
5
7
|
interface H3Event {
|
|
6
8
|
'__is_event__': true;
|
|
7
9
|
event: H3Event;
|
|
8
10
|
req: IncomingMessage;
|
|
9
11
|
res: ServerResponse;
|
|
10
|
-
context:
|
|
12
|
+
context: H3EventContext;
|
|
11
13
|
}
|
|
12
14
|
declare type CompatibilityEvent = H3Event | IncomingMessage;
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
interface JSONArray extends Array<JSONValue> {
|
|
17
|
-
}
|
|
18
|
-
declare type JSONValue = undefined | null | string | number | boolean | JSONObject | JSONArray;
|
|
19
|
-
declare type NonNullable<T> = T extends null | undefined ? never : T;
|
|
20
|
-
declare type _H3Response = void | NonNullable<JSONValue> | Buffer;
|
|
21
|
-
declare type H3Response = _H3Response | Promise<_H3Response>;
|
|
22
|
-
interface EventHandler<T extends H3Response = H3Response> {
|
|
15
|
+
declare type H3Response<T = any> = T | Promise<T>;
|
|
16
|
+
interface EventHandler<T = any> {
|
|
23
17
|
'__is_handler__'?: true;
|
|
24
|
-
(event: CompatibilityEvent): T
|
|
18
|
+
(event: CompatibilityEvent): H3Response<T>;
|
|
25
19
|
}
|
|
26
|
-
declare function defineEventHandler<T
|
|
20
|
+
declare function defineEventHandler<T = any>(handler: EventHandler<T>): EventHandler<T>;
|
|
27
21
|
declare const eventHandler: typeof defineEventHandler;
|
|
28
22
|
declare type LazyEventHandler = () => EventHandler | Promise<EventHandler>;
|
|
29
23
|
declare function defineLazyEventHandler(factory: LazyEventHandler): EventHandler;
|
|
@@ -96,7 +90,7 @@ interface AppOptions {
|
|
|
96
90
|
}
|
|
97
91
|
declare function createApp(options?: AppOptions): App;
|
|
98
92
|
declare function use(app: App, arg1: string | Handler | InputLayer | InputLayer[], arg2?: Handler | Partial<InputLayer> | Handler[] | Middleware | Middleware[], arg3?: Partial<InputLayer>): App;
|
|
99
|
-
declare function createAppEventHandler(stack: Stack, options: AppOptions): EventHandler<
|
|
93
|
+
declare function createAppEventHandler(stack: Stack, options: AppOptions): EventHandler<void>;
|
|
100
94
|
|
|
101
95
|
/**
|
|
102
96
|
* H3 Runtime Error
|
|
@@ -119,7 +113,7 @@ declare class H3Error extends Error {
|
|
|
119
113
|
* @param input {Partial<H3Error>}
|
|
120
114
|
* @return {H3Error} An instance of the H3Error
|
|
121
115
|
*/
|
|
122
|
-
declare function createError(input: Partial<H3Error>): H3Error;
|
|
116
|
+
declare function createError(input: string | Partial<H3Error>): H3Error;
|
|
123
117
|
/**
|
|
124
118
|
* Receive an error and return the corresponding response.<br>
|
|
125
119
|
* H3 internally uses this function to handle unhandled errors.<br>
|
|
@@ -248,4 +242,4 @@ interface Router extends AddRouteShortcuts {
|
|
|
248
242
|
}
|
|
249
243
|
declare function createRouter(): Router;
|
|
250
244
|
|
|
251
|
-
export { AddRouteShortcuts, App, AppOptions, AppUse, CacheConditions, CompatibilityEvent, CompatibilityEventHandler, DynamicEventHandler, Encoding, EventHandler, H3Error, H3Event, H3Response, HTTPMethod, Handler, IncomingMessage, InputLayer, InputStack,
|
|
245
|
+
export { AddRouteShortcuts, App, AppOptions, AppUse, CacheConditions, CompatibilityEvent, CompatibilityEventHandler, DynamicEventHandler, Encoding, EventHandler, H3Error, H3Event, H3EventContext, H3Response, HTTPMethod, Handler, IncomingMessage, InputLayer, InputStack, Layer, LazyEventHandler, LazyHandler, MIMES, Matcher, Middleware, NodeHandler, PromisifiedHandler, Router, RouterMethod, RouterUse, ServerResponse, Stack, appendHeader, assertMethod, callHandler, createApp, createAppEventHandler, createError, createEvent, createRouter, defaultContentType, defineEventHandler, defineHandle, defineHandler, defineLazyEventHandler, defineLazyHandler, defineMiddleware, deleteCookie, dynamicEventHandler, eventHandler, handleCacheHeaders, isError, isEvent, isEventHandler, isMethod, isStream, lazyEventHandler, lazyHandle, promisifyHandle, promisifyHandler, send, sendError, sendRedirect, sendStream, setCookie, toEventHandler, use, useBase, useBody, useCookie, useCookies, useMethod, useQuery, useRawBody };
|
package/dist/index.mjs
CHANGED
|
@@ -179,10 +179,13 @@ class H3Error extends Error {
|
|
|
179
179
|
}
|
|
180
180
|
}
|
|
181
181
|
function createError(input) {
|
|
182
|
+
if (typeof input === "string") {
|
|
183
|
+
return new H3Error(input);
|
|
184
|
+
}
|
|
182
185
|
if (input instanceof H3Error) {
|
|
183
186
|
return input;
|
|
184
187
|
}
|
|
185
|
-
const err = new H3Error(input.message ?? input.statusMessage);
|
|
188
|
+
const err = new H3Error(input.message ?? input.statusMessage, input.cause ? { cause: input.cause } : void 0);
|
|
186
189
|
if (input.statusCode) {
|
|
187
190
|
err.statusCode = input.statusCode;
|
|
188
191
|
}
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "h3",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.9",
|
|
4
4
|
"description": "Tiny JavaScript Server",
|
|
5
5
|
"repository": "unjs/h3",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
10
11
|
"import": "./dist/index.mjs",
|
|
11
12
|
"require": "./dist/index.cjs"
|
|
12
13
|
}
|
|
@@ -20,8 +21,8 @@
|
|
|
20
21
|
"dependencies": {
|
|
21
22
|
"cookie-es": "^0.5.0",
|
|
22
23
|
"destr": "^1.1.1",
|
|
23
|
-
"radix3": "^0.1.
|
|
24
|
-
"ufo": "^0.8.
|
|
24
|
+
"radix3": "^0.1.2",
|
|
25
|
+
"ufo": "^0.8.4"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"0x": "latest",
|
|
@@ -44,15 +45,14 @@
|
|
|
44
45
|
"unbuild": "latest",
|
|
45
46
|
"vitest": "latest"
|
|
46
47
|
},
|
|
47
|
-
"packageManager": "pnpm@
|
|
48
|
+
"packageManager": "pnpm@7.1.9",
|
|
48
49
|
"scripts": {
|
|
49
50
|
"build": "unbuild",
|
|
50
51
|
"dev": "vitest",
|
|
51
52
|
"lint": "eslint --ext ts,mjs,cjs .",
|
|
52
53
|
"play": "jiti ./playground/index.ts",
|
|
53
54
|
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs",
|
|
54
|
-
"release": "pnpm
|
|
55
|
-
"test": "vitest run"
|
|
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.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"
|
|
55
|
+
"release": "pnpm test && pnpm test && pnpm build && standard-version && pnpm publish && git push --follow-tags",
|
|
56
|
+
"test": "pnpm lint && vitest run --coverage"
|
|
57
|
+
}
|
|
58
58
|
}
|