itty-router 2.5.1 → 2.5.2
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 +33 -31
- package/dist/itty-router.min.js +1 -1
- package/package.json +1 -1
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
|
@@ -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'
|
|
@@ -507,36 +507,38 @@ router.puppy('/', request => {}) // Strongly typed
|
|
|
507
507
|
|
|
508
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
509
|
```js
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
q.
|
|
533
|
-
|
|
534
|
-
|
|
510
|
+
function Router({ base = '', routes = [] } = {}) {
|
|
511
|
+
return {
|
|
512
|
+
__proto__: new Proxy({}, {
|
|
513
|
+
get: (t, k, c) => (p, ...H) =>
|
|
514
|
+
routes.push([
|
|
515
|
+
k.toUpperCase(),
|
|
516
|
+
RegExp(`^${(base + p)
|
|
517
|
+
.replace(/(\/?)\*/g, '($1.*)?')
|
|
518
|
+
.replace(/\/$/, '')
|
|
519
|
+
.replace(/:(\w+)(\?)?(\.)?/g, '$2(?<$1>[^/]+)$2$3')
|
|
520
|
+
.replace(/\.(?=[\w(])/, '\\.')
|
|
521
|
+
.replace(/\)\.\?\(([^\[]+)\[\^/g, '?)\\.?($1(?<=\\.)[^\\.') // RIP all the bytes lost :'(
|
|
522
|
+
}/*$`),
|
|
523
|
+
H,
|
|
524
|
+
]) && c
|
|
525
|
+
}),
|
|
526
|
+
routes,
|
|
527
|
+
async handle (q, ...a) {
|
|
528
|
+
let s, m,
|
|
529
|
+
u = new URL(q.url)
|
|
530
|
+
q.query = Object.fromEntries(u.searchParams)
|
|
531
|
+
for (let [M, p, H] of routes) {
|
|
532
|
+
if ((M === q.method || M === 'ALL') && (m = u.pathname.match(p))) {
|
|
533
|
+
q.params = m.groups
|
|
534
|
+
for (let h of H) {
|
|
535
|
+
if ((s = await h(q.proxy || q, ...a)) !== undefined) return s
|
|
536
|
+
}
|
|
535
537
|
}
|
|
536
538
|
}
|
|
537
539
|
}
|
|
538
540
|
}
|
|
539
|
-
}
|
|
541
|
+
}
|
|
540
542
|
```
|
|
541
543
|
|
|
542
544
|
## Special Thanks
|
|
@@ -575,8 +577,8 @@ These folks are the real heroes, making open source the powerhouse that it is!
|
|
|
575
577
|
- [@technoyes](https://github.com/technoyes) - three kind-of-a-big-deal errors fixed. Imagine the look on my face... thanks man!! :)
|
|
576
578
|
- [@roojay520](https://github.com/roojay520) - TS interface fixes
|
|
577
579
|
#### Documentation
|
|
578
|
-
- [@arunsathiya](https://github.com/arunsathiya),
|
|
579
|
-
[@poacher2k](https://github.com/poacher2k),
|
|
580
|
-
[@ddarkr](https://github.com/ddarkr),
|
|
580
|
+
- [@arunsathiya](https://github.com/arunsathiya),
|
|
581
|
+
[@poacher2k](https://github.com/poacher2k),
|
|
582
|
+
[@ddarkr](https://github.com/ddarkr),
|
|
581
583
|
[@kclauson](https://github.com/kclauson),
|
|
582
584
|
[@jcapogna](https://github.com/jcapogna)
|
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}}}}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "itty-router",
|
|
3
|
-
"version": "2.5.
|
|
3
|
+
"version": "2.5.2",
|
|
4
4
|
"description": "Tiny, zero-dependency router with route param and query parsing - built for Cloudflare Workers, but works everywhere!",
|
|
5
5
|
"main": "./dist/itty-router.min.js",
|
|
6
6
|
"types": "./dist/itty-router.d.ts",
|