itty-router
Advanced tools
Comparing version 4.0.0-next.50 to 4.0.0-next.52
@@ -8,5 +8,6 @@ export declare type GenericTraps = { | ||
} & GenericTraps; | ||
export declare type IRequest = { | ||
export declare type IRequestStrict = { | ||
method: string; | ||
url: string; | ||
route: string; | ||
params: { | ||
@@ -19,26 +20,32 @@ [key: string]: string; | ||
proxy?: any; | ||
} & GenericTraps; | ||
export interface RouterOptions { | ||
} & Request; | ||
export declare type IRequest = IRequestStrict & GenericTraps; | ||
export declare type RouterOptions = { | ||
base?: string; | ||
routes?: RouteEntry[]; | ||
} | ||
export interface RouteHandler { | ||
(request: IRequest, ...args: any): any; | ||
} | ||
}; | ||
export declare type RouteHandler<I = IRequest, A extends any[] = any[]> = { | ||
(request: I, ...args: A): any; | ||
}; | ||
export declare type RouteEntry = [string, RegExp, RouteHandler[], string]; | ||
export declare type Route = <T extends RouterType>(path: string, ...handlers: RouteHandler[]) => T; | ||
export declare type RouterHints = { | ||
all: Route; | ||
delete: Route; | ||
get: Route; | ||
options: Route; | ||
patch: Route; | ||
post: Route; | ||
put: Route; | ||
export declare type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RT; | ||
export declare type UniversalRoute<RequestType = IRequest, Args extends any[] = any[]> = (path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RouterType<UniversalRoute<RequestType, Args>, Args>; | ||
declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false; | ||
export declare type CustomRoutes<R = Route> = { | ||
[key: string]: R; | ||
}; | ||
export declare type RouterType = { | ||
__proto__: RouterType; | ||
export declare type RouterType<R = Route, Args extends any[] = any[]> = { | ||
__proto__: RouterType<R>; | ||
routes: RouteEntry[]; | ||
handle: (request: RequestLike, ...extra: any) => Promise<any>; | ||
} & RouterHints; | ||
export declare const Router: ({ base, routes }?: RouterOptions) => RouterType; | ||
handle: <A extends any[] = Args>(request: RequestLike, ...extra: Equal<R, Args> extends true ? A : Args) => Promise<any>; | ||
all: R; | ||
delete: R; | ||
get: R; | ||
head: R; | ||
options: R; | ||
patch: R; | ||
post: R; | ||
put: R; | ||
} & CustomRoutes<R>; | ||
export declare const Router: <RequestType = IRequest, Args extends any[] = any[], RouteType = Equal<RequestType, IRequest> extends true ? Route : UniversalRoute<RequestType, Args>>({ base, routes }?: RouterOptions) => RouterType<RouteType, Args>; | ||
export {}; |
{ | ||
"name": "itty-router", | ||
"version": "4.0.0-next.50", | ||
"version": "4.0.0-next.52", | ||
"description": "Tiny, zero-dependency API router - built for Cloudflare Workers, but works everywhere!", | ||
@@ -19,5 +19,2 @@ "type": "module", | ||
"scripts": { | ||
"docs:dev": "cp README.md ./docs/pages/README.md && vite", | ||
"docs:build": "cp README.md ./docs/pages/README.md && vite build", | ||
"docs:serve": "vite preview", | ||
"lint": "npx eslint src", | ||
@@ -35,3 +32,5 @@ "test": "vitest --coverage --reporter verbose", | ||
"build": "rollup -c", | ||
"release": "release --tag --push --type=next --src=dist" | ||
"release": "release --tag --push --type=next --src=dist", | ||
"runtime:bun": "bun example/bun.ts", | ||
"runtime:node": "node example/node.js" | ||
}, | ||
@@ -54,4 +53,6 @@ "repository": { | ||
"@skypack/package-check": "^0.2.2", | ||
"@types/node": "^20.2.1", | ||
"@vitejs/plugin-vue": "^2.2.4", | ||
"@vitest/coverage-c8": "^0.24.3", | ||
"@whatwg-node/server": "^0.7.6", | ||
"coveralls": "^3.1.1", | ||
@@ -64,3 +65,5 @@ "eslint": "^8.11.0", | ||
"gzip-size": "^6.0.0", | ||
"http": "^0.0.1-security", | ||
"isomorphic-fetch": "^3.0.0", | ||
"itty-router": "^4.0.0-next.50", | ||
"jsdom": "^20.0.1", | ||
@@ -72,6 +75,6 @@ "npm-run-all": "^4.1.5", | ||
"rollup-plugin-multi-input": "^1.3.3", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.8.4", | ||
"vite": "^2.8.6", | ||
"vitest": "^0.24.3", | ||
"vue": "^3.2.31", | ||
"yarn": "^1.22.18", | ||
@@ -78,0 +81,0 @@ "yarn-release": "^1.10.3" |
@@ -1,17 +0,57 @@ | ||
# [![Itty Router](https://user-images.githubusercontent.com/865416/146679767-16be95b4-5dd7-4bcf-aed7-b8aa8c828f48.png)](https://itty-router.dev) | ||
[![Version](https://img.shields.io/npm/v/itty-router.svg?style=flat-square)](https://npmjs.com/package/itty-router) | ||
[![Bundle Size](https://img.shields.io/bundlephobia/minzip/itty-router?style=flat-square)](https://bundlephobia.com/result?p=itty-router) | ||
[![Build Status](https://img.shields.io/github/actions/workflow/status/kwhitley/itty-router/verify.yml?branch=v4.x&style=flat-square)](https://github.com/kwhitley/itty-router/actions/workflows/verify.yml) | ||
[![Coverage Status](https://img.shields.io/coveralls/github/kwhitley/itty-router/v4.x?style=flat-square)](https://coveralls.io/github/kwhitley/itty-router?branch=v4.x) | ||
[![NPM Weekly Downloads](https://img.shields.io/npm/dw/itty-router?style=flat-square)](https://npmjs.com/package/itty-router) | ||
[![Open Issues](https://img.shields.io/github/issues/kwhitley/itty-router?style=flat-square)](https://github.com/kwhitley/itty-router/issues) | ||
[![Discord](https://img.shields.io/discord/832353585802903572?style=flat-square)](https://discord.com/channels/832353585802903572) | ||
[![GitHub Repo stars](https://img.shields.io/github/stars/kwhitley/itty-router?style=social)](https://github.com/kwhitley/itty-router) | ||
[![Twitter](https://img.shields.io/twitter/follow/kevinrwhitley.svg?style=social&label=Follow)](https://www.twitter.com/kevinrwhitley) | ||
<p align="center"> | ||
<a href="https://itty.dev/itty-router"> | ||
<img src="https://user-images.githubusercontent.com/865416/146679767-16be95b4-5dd7-4bcf-aed7-b8aa8c828f48.png" alt="Itty Router" /> | ||
</a> | ||
<p> | ||
Itty aims to be the world's smallest (430 bytes), feature-rich JavaScript router, enabling low-calorie code with a low-calorie bundlesize. Designed originally for Cloudflare Workers, itty can be used in browsers, Service Workers, edge functions, or standalone runtimes like Node! | ||
--- | ||
<h3 align="center"><a href="https://itty.dev/itty-router">4.x Documentation @ itty.dev</a></h3> | ||
<p align="center"> | ||
<a href="https://npmjs.com/package/itty-router" target="_blank"> | ||
<img src="https://img.shields.io/npm/v/itty-router.svg?style=flat-square" alt="npm version" /> | ||
</a> | ||
<a href="https://bundlephobia.com/result?p=itty-router" target="_blank"> | ||
<img src="https://img.shields.io/bundlephobia/minzip/itty-router?style=flat-square" alt="bundle size" /> | ||
</a> | ||
<a href="https://github.com/kwhitley/itty-router/actions/workflows/verify.yml" target="_blank"> | ||
<img src="https://img.shields.io/github/actions/workflow/status/kwhitley/itty-router/verify.yml?branch=v4.x&style=flat-square" alt="build status" /> | ||
</a> | ||
<a href="https://coveralls.io/github/kwhitley/itty-router?branch=v4.x" target="_blank"> | ||
<img src="https://img.shields.io/coveralls/github/kwhitley/itty-router/v4.x?style=flat-square" alt="code coverage" /> | ||
</a> | ||
<a href="https://npmjs.com/package/itty-router" target="_blank"> | ||
<img src="https://img.shields.io/npm/dw/itty-router?style=flat-square" alt="weekly downloads" /> | ||
</a> | ||
<a href="https://github.com/kwhitley/itty-router/issues" target="_blank"> | ||
<img src="https://img.shields.io/github/issues/kwhitley/itty-router?style=flat-square" alt="open issues" /> | ||
</a> | ||
<a href="" target="_blank"> | ||
<img src="" alt="" /> | ||
</a> | ||
</p> | ||
<p align="center"> | ||
<a href="https://discord.com/channels/832353585802903572" target="_blank"> | ||
<img src="https://img.shields.io/discord/832353585802903572?style=flat-square" alt="join us on discord" /> | ||
</a> | ||
<a href="https://github.com/kwhitley/itty-router" target="_blank"> | ||
<img src="https://img.shields.io/github/stars/kwhitley/itty-router?style=social" alt="repo stars" /> | ||
</a> | ||
<a href="https://www.twitter.com/kevinrwhitley" target="_blank"> | ||
<img src="https://img.shields.io/twitter/follow/kevinrwhitley.svg?style=social&label=Follow" alt="follow the author" /> | ||
</a> | ||
<a href="" target="_blank"> | ||
<img src="" alt="" /> | ||
</a> | ||
</p> | ||
--- | ||
Itty aims to be the world's smallest (~440 bytes), feature-rich JavaScript router, enabling beautiful API code with a near-zero bundlesize. Designed originally for [Cloudflare Workers](https://itty.dev/itty-router/runtimes#Cloudflare%20Workers), itty can be used in browsers, Service Workers, edge functions, or standalone runtimes like [Node](https://itty.dev/itty-router/runtimes#Node), [Bun](https://itty.dev/itty-router/runtimes#Bun), etc.! | ||
## Features: | ||
- [fully typed/TypeScript support](https://itty.dev/itty-router#typescript) | ||
- [route params](https://itty.dev/itty-router/route-patterns#params) (including [optional params](https://itty.dev/itty-router/route-patterns#optional)) | ||
@@ -22,9 +62,7 @@ - [wildcards](https://itty.dev/itty-router/route-patterns#wildcards) and [greedy params](https://itty.dev/itty-router/route-patterns#greedy) | ||
- [router nesting](https://itty.dev/itty-router/nesting) | ||
- any real/fake HTTP method | ||
- lightweight, easy-to-understand route code | ||
- works in *any* runtime/environment (including the browser) | ||
- fully typed/TS | ||
- works in virtually [*any* runtime/environment](https://itty.dev/itty-router/runtimes) (including the browser) | ||
## [Full Documentation](https://itty.dev/itty-router) | ||
Complete docs/API available on [itty.dev](https://itty.dev/itty-router), or join our [Discord](https://discord.com/channels/832353585802903572) channel to chat. | ||
Complete docs/API are available at [itty.dev](https://itty.dev/itty-router), or join our [Discord](https://discord.com/channels/832353585802903572) channel to chat! | ||
@@ -39,8 +77,6 @@ ## Installation | ||
import { | ||
error, // create an error response | ||
respondWithError, // downstream handler for thrown errors | ||
respondWithJSON, // downstream handler to send data as JSON | ||
Router, // the ~470 byte router itself | ||
withParams, // middleware to auto-embed route params | ||
notFound, // create a 404 | ||
error, // creates error responses | ||
json, // creates json responses | ||
Router, // the ~440 byte router itself | ||
withParams, // middleware: puts params directly on the Request | ||
} from 'itty-router' | ||
@@ -52,4 +88,3 @@ | ||
router | ||
// add some global middleware | ||
// withParams auto-parses route params into the request | ||
// middleware: withParams auto-parses route params into the request | ||
.all('*', withParams) | ||
@@ -63,14 +98,14 @@ | ||
({ id }) => todos.find(todo => todo.id === id) | ||
|| notFound('That todo was not found') | ||
|| error(404, 'That todo was not found') | ||
) | ||
// 404 for everything else | ||
.all('*', notFound) | ||
.all('*', () => error(404)) | ||
// Example: Cloudflare ESM Worker syntax | ||
// Example: Cloudflare Worker module syntax | ||
export default { | ||
fetch: (request, env, context) => router | ||
.handle(request, env, context) | ||
.then(respondWithJSON) // automatically send as JSON | ||
.catch(respondWithError) // and send error Responses for thrown errors | ||
fetch: (request, ...args) => router | ||
.handle(request, ...args) | ||
.then(json) // automatically send as JSON | ||
.catch(error) // and send error Responses for thrown errors | ||
} | ||
@@ -77,0 +112,0 @@ ``` |
@@ -8,5 +8,6 @@ export declare type GenericTraps = { | ||
} & GenericTraps; | ||
export declare type IRequest = { | ||
export declare type IRequestStrict = { | ||
method: string; | ||
url: string; | ||
route: string; | ||
params: { | ||
@@ -19,26 +20,32 @@ [key: string]: string; | ||
proxy?: any; | ||
} & GenericTraps; | ||
export interface RouterOptions { | ||
} & Request; | ||
export declare type IRequest = IRequestStrict & GenericTraps; | ||
export declare type RouterOptions = { | ||
base?: string; | ||
routes?: RouteEntry[]; | ||
} | ||
export interface RouteHandler { | ||
(request: IRequest, ...args: any): any; | ||
} | ||
}; | ||
export declare type RouteHandler<I = IRequest, A extends any[] = any[]> = { | ||
(request: I, ...args: A): any; | ||
}; | ||
export declare type RouteEntry = [string, RegExp, RouteHandler[], string]; | ||
export declare type Route = <T extends RouterType>(path: string, ...handlers: RouteHandler[]) => T; | ||
export declare type RouterHints = { | ||
all: Route; | ||
delete: Route; | ||
get: Route; | ||
options: Route; | ||
patch: Route; | ||
post: Route; | ||
put: Route; | ||
export declare type Route = <RequestType = IRequest, Args extends any[] = any[], RT = RouterType>(path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RT; | ||
export declare type UniversalRoute<RequestType = IRequest, Args extends any[] = any[]> = (path: string, ...handlers: RouteHandler<RequestType, Args>[]) => RouterType<UniversalRoute<RequestType, Args>, Args>; | ||
declare type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends (<T>() => T extends Y ? 1 : 2) ? true : false; | ||
export declare type CustomRoutes<R = Route> = { | ||
[key: string]: R; | ||
}; | ||
export declare type RouterType = { | ||
__proto__: RouterType; | ||
export declare type RouterType<R = Route, Args extends any[] = any[]> = { | ||
__proto__: RouterType<R>; | ||
routes: RouteEntry[]; | ||
handle: (request: RequestLike, ...extra: any) => Promise<any>; | ||
} & RouterHints; | ||
export declare const Router: ({ base, routes }?: RouterOptions) => RouterType; | ||
handle: <A extends any[] = Args>(request: RequestLike, ...extra: Equal<R, Args> extends true ? A : Args) => Promise<any>; | ||
all: R; | ||
delete: R; | ||
get: R; | ||
head: R; | ||
options: R; | ||
patch: R; | ||
post: R; | ||
put: R; | ||
} & CustomRoutes<R>; | ||
export declare const Router: <RequestType = IRequest, Args extends any[] = any[], RouteType = Equal<RequestType, IRequest> extends true ? Route : UniversalRoute<RequestType, Args>>({ base, routes }?: RouterOptions) => RouterType<RouteType, Args>; | ||
export {}; |
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
34058
310
153
31