itty-router 2.5.1 → 2.6.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/CHANGELOG.md +1 -0
- package/README.md +11 -45
- package/dist/itty-router.d.ts +1 -1
- package/dist/itty-router.min.js +1 -1
- package/dist/itty-router.min.mjs +1 -0
- package/package.json +26 -14
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,7 @@ Until this library makes it to a production release of v1.x, **minor versions ma
|
|
|
3
3
|
|
|
4
4
|
[@SupremeTechnopriest)(https://github.com/SupremeTechnopriest) - improved TypeScript support and documentation! :D\
|
|
5
5
|
|
|
6
|
+
- **v2.5.2** - fixes issue with arrow functions in CommonJS named exports (rare issue)
|
|
6
7
|
- **v2.5.1** - added context to Cloudflare ES module syntax example (credit [@jcapogna](https://github.com/jcapogna))
|
|
7
8
|
- **v2.5.0** - improved TypeScript docs/types (thanks [@SupremeTechnopriest](https://github.com/SupremeTechnopriest)!)
|
|
8
9
|
- **v2.4.9** - fixed the cursed "optional" file format capturing bug - RIP all the bytes lost
|
package/README.md
CHANGED
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
<img alt="Join the discussion on Github" src="https://img.shields.io/badge/Github%20Discussions%20%26%20Support-Chat%20now!-blue" />
|
|
20
20
|
</a>-->
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
Tiny, zero-dependency router with route param and query parsing - built for [Cloudflare Workers](https://developers.cloudflare.com/workers/), but works everywhere!
|
|
23
23
|
|
|
24
24
|
### Addons & Related Libraries
|
|
25
25
|
1. [itty-router-extras](https://www.npmjs.com/package/itty-router-extras) - adds quality-of-life improvements and utility functions for simplifying request/response code (e.g. middleware, cookies, body parsing, json handling, errors, and an itty version with automatic exception handling)!
|
|
@@ -27,9 +27,9 @@ It's an itty bitty router, designed for express-like routing within [Cloudflare
|
|
|
27
27
|
|
|
28
28
|
## Features
|
|
29
29
|
- [x] Tiny ([~500 bytes](https://bundlephobia.com/package/itty-router) compressed), with zero dependencies.
|
|
30
|
-
- [x] [TypeScript support](#typescript)
|
|
31
|
-
- [x]
|
|
32
|
-
- [x]
|
|
30
|
+
- [x] [Fully typed/TypeScript support](#typescript)
|
|
31
|
+
- [x] Supports sync/async handlers/middleware.
|
|
32
|
+
- [x] Parses route params, with wildcards and optionals (e.g. `/api/:collection/:id?`)
|
|
33
33
|
- [x] Query parsing (e.g. `?page=3&foo=bar`)
|
|
34
34
|
- [x] [Middleware support](#middleware). Any number of sync/async handlers may be passed to a route.
|
|
35
35
|
- [x] [Nestable](#nested-routers-with-404-handling). Supports nesting routers for API branching.
|
|
@@ -105,9 +105,9 @@ router.get('/todos/:user/:item?', (req) => {
|
|
|
105
105
|
```
|
|
106
106
|
|
|
107
107
|
### 3. Handle Incoming Request(s)
|
|
108
|
-
##### `async router.handle(request.proxy: Proxy || request: Request, ...anything else)
|
|
108
|
+
##### `async router.handle(request.proxy: Proxy || request: Request, ...anything else) => Promise<any>`
|
|
109
109
|
Requests (doesn't have to be a real Request class) should have both a **method** and full **url**.
|
|
110
|
-
The `handle` method will then return the first matching route handler that returns something (or nothing at all if no match).
|
|
110
|
+
The `handle` method will then return a Promise, resolving with the first matching route handler that returns something (or nothing at all if no match).
|
|
111
111
|
|
|
112
112
|
```js
|
|
113
113
|
router.handle({
|
|
@@ -301,7 +301,7 @@ export default {
|
|
|
301
301
|
export default {
|
|
302
302
|
fetch: (...args) => router
|
|
303
303
|
.handle(...args)
|
|
304
|
-
.then(response =>
|
|
304
|
+
.then(response => {
|
|
305
305
|
// can modify response here before final return, e.g. CORS headers
|
|
306
306
|
|
|
307
307
|
return response
|
|
@@ -452,7 +452,7 @@ const router = Router<Request, IMethods>() // Valid
|
|
|
452
452
|
const router = Router<void, IMethods>() // Valid
|
|
453
453
|
```
|
|
454
454
|
|
|
455
|
-
The router will also accept any string as a method, not just those provided on the `TMethods` type.
|
|
455
|
+
The router will also accept any string as a method, not just those provided on the `TMethods` type.
|
|
456
456
|
|
|
457
457
|
```ts
|
|
458
458
|
import { Router, Route } from 'itty-router'
|
|
@@ -505,40 +505,6 @@ router.puppy('/', request => {}) // Strongly typed
|
|
|
505
505
|
1. Submit PR with a detailed description of what you're doing
|
|
506
506
|
1. I'll add you to the credits! :)
|
|
507
507
|
|
|
508
|
-
### The Entire Code (for more legibility, [see src on GitHub](https://github.com/kwhitley/itty-router/blob/v2.x/src/itty-router.js))
|
|
509
|
-
```js
|
|
510
|
-
const Router = ({ base = '', routes = [] } = {}) => ({
|
|
511
|
-
__proto__: new Proxy({}, {
|
|
512
|
-
get: (t, k, c) => (p, ...H) =>
|
|
513
|
-
routes.push([
|
|
514
|
-
k.toUpperCase(),
|
|
515
|
-
RegExp(`^${(base + p)
|
|
516
|
-
.replace(/(\/?)\*/g, '($1.*)?')
|
|
517
|
-
.replace(/\/$/, '')
|
|
518
|
-
.replace(/:(\w+)(\?)?(\.)?/g, '$2(?<$1>[^/]+)$2$3')
|
|
519
|
-
.replace(/\.(?=[\w(])/, '\\.')
|
|
520
|
-
.replace(/\)\.\?\(([^\[]+)\[\^/g, '?)\\.?($1(?<=\\.)[^\\.')
|
|
521
|
-
}/*$`),
|
|
522
|
-
H,
|
|
523
|
-
]) && c
|
|
524
|
-
}),
|
|
525
|
-
routes,
|
|
526
|
-
async handle (q, ...a) {
|
|
527
|
-
let s, m,
|
|
528
|
-
u = new URL(q.url)
|
|
529
|
-
q.query = Object.fromEntries(u.searchParams)
|
|
530
|
-
for (let [M, p, H] of routes) {
|
|
531
|
-
if ((M === q.method || M === 'ALL') && (m = u.pathname.match(p))) {
|
|
532
|
-
q.params = m.groups
|
|
533
|
-
for (let h of H) {
|
|
534
|
-
if ((s = await h(q.proxy || q, ...a)) !== undefined) return s
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
})
|
|
540
|
-
```
|
|
541
|
-
|
|
542
508
|
## Special Thanks
|
|
543
509
|
This repo goes out to my past and present colleagues at Arundo - who have brought me such inspiration, fun,
|
|
544
510
|
and drive over the last couple years. In particular, the absurd brevity of this code is thanks to a
|
|
@@ -575,8 +541,8 @@ These folks are the real heroes, making open source the powerhouse that it is!
|
|
|
575
541
|
- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
|
|
576
542
|
- [@roojay520](https://github.com/roojay520) - TS interface fixes
|
|
577
543
|
#### Documentation
|
|
578
|
-
- [@arunsathiya](https://github.com/arunsathiya),
|
|
579
|
-
[@poacher2k](https://github.com/poacher2k),
|
|
580
|
-
[@ddarkr](https://github.com/ddarkr),
|
|
544
|
+
- [@arunsathiya](https://github.com/arunsathiya),
|
|
545
|
+
[@poacher2k](https://github.com/poacher2k),
|
|
546
|
+
[@ddarkr](https://github.com/ddarkr),
|
|
581
547
|
[@kclauson](https://github.com/kclauson),
|
|
582
548
|
[@jcapogna](https://github.com/jcapogna)
|
package/dist/itty-router.d.ts
CHANGED
|
@@ -42,7 +42,7 @@ export interface IHTTPMethods {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
export type Router<TRequest = Request, TMethods = {}> = {
|
|
45
|
-
handle: (request: TRequest, ...extra: any) => any
|
|
45
|
+
handle: (request: TRequest, ...extra: any) => Promise<any>
|
|
46
46
|
routes: RouteEntry<TRequest>[]
|
|
47
47
|
all: Route
|
|
48
48
|
} & TMethods & {
|
package/dist/itty-router.min.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
module.exports={Router:({base:
|
|
1
|
+
module.exports={Router:function({base:t="",routes:n=[]}={}){return{__proto__:new Proxy({},{get:(e,a,o)=>(e,...r)=>n.push([a.toUpperCase(),RegExp(`^${(t+e).replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:(\w+)(\?)?(\.)?/g,"$2(?<$1>[^/]+)$2$3").replace(/\.(?=[\w(])/,"\\.").replace(/\)\.\?\(([^\[]+)\[\^/g,"?)\\.?($1(?<=\\.)[^\\.")}/*$`),r])&&o}),routes:n,async handle(e,...r){let a,o,t=new URL(e.url);e.query=Object.fromEntries(t.searchParams);for(var[p,s,u]of n)if((p===e.method||"ALL"===p)&&(o=t.pathname.match(s))){e.params=o.groups;for(var c of u)if(void 0!==(a=await c(e.proxy||e,...r)))return a}}}}};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default{Router:function({base:t="",routes:n=[]}={}){return{__proto__:new Proxy({},{get:(e,a,o)=>(e,...r)=>n.push([a.toUpperCase(),RegExp(`^${(t+e).replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:(\w+)(\?)?(\.)?/g,"$2(?<$1>[^/]+)$2$3").replace(/\.(?=[\w(])/,"\\.").replace(/\)\.\?\(([^\[]+)\[\^/g,"?)\\.?($1(?<=\\.)[^\\.")}/*$`),r])&&o}),routes:n,async handle(e,...r){let a,o,t=new URL(e.url);e.query=Object.fromEntries(t.searchParams);for(var[p,s,u]of n)if((p===e.method||"ALL"===p)&&(o=t.pathname.match(s))){e.params=o.groups;for(var c of u)if(void 0!==(a=await c(e.proxy||e,...r)))return a}}}}};
|
package/package.json
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-router",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Tiny, zero-dependency router with route param and query parsing - built for Cloudflare Workers, but works everywhere!",
|
|
5
|
-
"
|
|
6
|
-
"types": "./dist/itty-router.d.ts",
|
|
5
|
+
"sourceType": "module",
|
|
7
6
|
"files": [
|
|
8
7
|
"dist/itty-router.min.js",
|
|
8
|
+
"dist/itty-router.min.mjs",
|
|
9
9
|
"dist/itty-router.d.ts"
|
|
10
10
|
],
|
|
11
|
+
"main": "./dist/itty-router.min.js",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"require": "./dist/itty-router.min.js",
|
|
15
|
+
"import": "./dist/itty-router.min.mjs"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"types": "./dist/itty-router.d.ts",
|
|
11
19
|
"keywords": [
|
|
12
20
|
"router",
|
|
13
21
|
"cloudflare",
|
|
@@ -47,7 +55,11 @@
|
|
|
47
55
|
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls",
|
|
48
56
|
"prerelease": "yarn verify",
|
|
49
57
|
"prebuild": "rimraf dist && mkdir dist && node prebuild.js && cp src/itty-router.d.ts dist",
|
|
50
|
-
"build": "
|
|
58
|
+
"build": "yarn uglify",
|
|
59
|
+
"uglify": "yarn uglify:cjs && yarn uglify:esm",
|
|
60
|
+
"uglify:esm": "uglifyjs dist/itty-router.mjs -c -m --toplevel > dist/itty-router.min.mjs",
|
|
61
|
+
"uglify:cjs": "uglifyjs src/itty-router.js -c -m --toplevel > dist/itty-router.min.js",
|
|
62
|
+
"build:esm": "esbuild src/itty-router.js --format=esm --outfile=./dist/itty-router.mjs",
|
|
51
63
|
"postbuild": "node check-size.js",
|
|
52
64
|
"release": "release --tag --push"
|
|
53
65
|
},
|
|
@@ -62,20 +74,20 @@
|
|
|
62
74
|
},
|
|
63
75
|
"homepage": "https://itty-router.dev",
|
|
64
76
|
"devDependencies": {
|
|
65
|
-
"@vitejs/plugin-vue": "^
|
|
66
|
-
"@vue/compiler-sfc": "^3.2.2",
|
|
67
|
-
"chalk": "^4.1.2",
|
|
77
|
+
"@vitejs/plugin-vue": "^2.2.4",
|
|
68
78
|
"coveralls": "^3.1.1",
|
|
69
|
-
"
|
|
70
|
-
"eslint
|
|
71
|
-
"
|
|
79
|
+
"esbuild": "^0.14.27",
|
|
80
|
+
"eslint": "^8.11.0",
|
|
81
|
+
"eslint-plugin-jest": "^26.1.2",
|
|
82
|
+
"fs-extra": "^10.0.1",
|
|
72
83
|
"gzip-size": "^6.0.0",
|
|
73
84
|
"isomorphic-fetch": "^3.0.0",
|
|
74
|
-
"jest": "^27.
|
|
85
|
+
"jest": "^27.5.1",
|
|
75
86
|
"rimraf": "^3.0.2",
|
|
76
|
-
"uglify-js": "^3.
|
|
77
|
-
"vite": "^2.
|
|
78
|
-
"
|
|
87
|
+
"uglify-js": "^3.15.3",
|
|
88
|
+
"vite": "^2.8.6",
|
|
89
|
+
"vue": "^3.2.31",
|
|
90
|
+
"yarn": "^1.22.18",
|
|
79
91
|
"yarn-release": "^1.10.3"
|
|
80
92
|
}
|
|
81
93
|
}
|