Comparing version 0.3.9 to 0.4.0
@@ -6,7 +6,7 @@ import { IncomingMessage, ServerResponse } from 'http'; | ||
declare type Handle<T = any> = (req: IncomingMessage, res: ServerResponse) => T; | ||
declare type Handle<T = any, ReqT = {}> = (req: IncomingMessage & ReqT, res: ServerResponse) => T; | ||
declare type PHandle = Handle<Promise<any>>; | ||
declare type Middleware = (req: IncomingMessage, res: ServerResponse, next: (err?: Error) => any) => any; | ||
declare type LazyHandle = () => Handle | Promise<Handle>; | ||
declare const defineHandle: <T>(handler: Handle<T>) => Handle<T>; | ||
declare const defineHandle: <T>(handler: Handle<T, {}>) => Handle<T, {}>; | ||
declare const defineMiddleware: (middleware: Middleware) => Middleware; | ||
@@ -237,4 +237,5 @@ declare function promisifyHandle(handle: Handle | Middleware): PHandle; | ||
declare type HTTPMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE'; | ||
declare function useQuery(req: IncomingMessage): ufo.QueryObject; | ||
declare type HTTPMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE'; | ||
declare function useMethod(req: IncomingMessage, defaultMethod?: HTTPMethod): HTTPMethod; | ||
@@ -249,2 +250,14 @@ declare function isMethod(req: IncomingMessage, expected: HTTPMethod | HTTPMethod[], allowHead?: boolean): boolean; | ||
export { App, AppOptions, AppUse, H3Error, HTTPMethod, Handle, InputLayer, InputStack, Layer, LazyHandle, MIMES, Matcher, Middleware, PHandle, Stack, appendHeader, assertMethod, callHandle, createApp, createError, createHandle, defaultContentType, defineHandle, defineMiddleware, isMethod, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useMethod, useQuery, useRawBody }; | ||
declare type RouterMethod = Lowercase<HTTPMethod>; | ||
declare type HandleWithParams = Handle<any, { | ||
params: Record<string, string>; | ||
}>; | ||
declare type AddWithMethod = (path: string, handle: HandleWithParams) => Router; | ||
declare type AddRouteShortcuts = Record<Lowercase<HTTPMethod>, AddWithMethod>; | ||
interface Router extends AddRouteShortcuts { | ||
add: (path: string, handle: HandleWithParams, method?: RouterMethod | 'all') => Router; | ||
handle: Handle; | ||
} | ||
declare function createRouter(): Router; | ||
export { AddRouteShortcuts, AddWithMethod, App, AppOptions, AppUse, H3Error, Handle, HandleWithParams, InputLayer, InputStack, Layer, LazyHandle, MIMES, Matcher, Middleware, PHandle, Router, RouterMethod, Stack, appendHeader, assertMethod, callHandle, createApp, createError, createHandle, createRouter, defaultContentType, defineHandle, defineMiddleware, isMethod, lazyHandle, promisifyHandle, send, sendError, sendRedirect, setCookie, use, useBase, useBody, useCookie, useCookies, useMethod, useQuery, useRawBody }; |
{ | ||
"name": "h3", | ||
"version": "0.3.9", | ||
"version": "0.4.0", | ||
"description": "Tiny JavaScript Server", | ||
@@ -21,9 +21,15 @@ "repository": "unjs/h3", | ||
"scripts": { | ||
"build": "siroc build", | ||
"dev": "jiti test/playground", | ||
"lint": "eslint --ext ts .", | ||
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./hello.js", | ||
"release": "yarn test && yarn build && standard-version && npm publish && git push --follow-tags", | ||
"test": "yarn lint && jest" | ||
"build": "unbuild", | ||
"dev": "jiti ./playground/index.ts", | ||
"lint": "eslint --ext ts,mjs,cjs .", | ||
"profile": "0x -o -D .profile -P 'autocannon -c 100 -p 10 -d 40 http://localhost:$PORT' ./playground/server.cjs", | ||
"release": "yarn lint && yarn test && yarn build && standard-version && npm publish && git push --follow-tags", | ||
"test": "vitest run" | ||
}, | ||
"dependencies": { | ||
"cookie": "^0.4.2", | ||
"destr": "^1.1.0", | ||
"radix3": "^0.1.1", | ||
"ufo": "^0.7.11" | ||
}, | ||
"devDependencies": { | ||
@@ -34,22 +40,19 @@ "0x": "latest", | ||
"@types/express": "latest", | ||
"@types/jest": "latest", | ||
"@types/node": "latest", | ||
"@types/supertest": "latest", | ||
"autocannon": "latest", | ||
"c8": "latest", | ||
"connect": "latest", | ||
"cookie-es": "latest", | ||
"destr": "latest", | ||
"eslint": "latest", | ||
"express": "latest", | ||
"get-port": "^5.0.0", | ||
"jest": "latest", | ||
"jiti": "latest", | ||
"listhen": "latest", | ||
"siroc": "latest", | ||
"standard-version": "latest", | ||
"supertest": "latest", | ||
"ts-jest": "latest", | ||
"typescript": "latest", | ||
"ufo": "latest" | ||
"unbuild": "latest", | ||
"vitest": "latest" | ||
} | ||
} |
@@ -24,2 +24,4 @@ [![npm downloads](https://img.shields.io/npm/dm/h3.svg?style=flat-square)](https://npmjs.com/package/h3) | ||
✔️ **Router:** Super fast route matching using [unjs/radix3](https://github.com/unjs/radix3) | ||
## Install | ||
@@ -61,4 +63,26 @@ | ||
## Examples | ||
## Router | ||
The `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. | ||
To opt-in using a more advanced and convenient routing system, we can create a router instance and register it to app instance. | ||
```ts | ||
import { createApp, createRouter } from 'h3' | ||
const app = createApp() | ||
const router = createRouter() | ||
.get('/', () => 'Hello World!') | ||
.get('/hello/:name', req => `Hello ${req.params.name}!`) | ||
app.use(router) | ||
``` | ||
**Tip:** We can register same route more than once with different methods. | ||
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). | ||
## More usage examples | ||
```js | ||
@@ -65,0 +89,0 @@ // Handle can directly return object or Promise<object> for JSON response |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20
149
41026
4
931
+ Addedcookie@^0.4.2
+ Addeddestr@^1.1.0
+ Addedradix3@^0.1.1
+ Addedufo@^0.7.11
+ Addedcookie@0.4.2(transitive)
+ Addeddestr@1.2.2(transitive)
+ Addedradix3@0.1.2(transitive)
+ Addedufo@0.7.11(transitive)