itty-router
Advanced tools
Comparing version 1.4.3 to 1.5.0
## Changelog | ||
Until this library makes it to a production release of v1.x, **minor versions may contain breaking changes to the API**. After v1.x, semantic versioning will be honored, and breaking changes will only occur under the umbrella of a major version bump. | ||
- **v1.5.0** - added '.all(route, handler)' handling for passthrough middleware | ||
- **v1.4.3** - fixed nested routers using simple "/" routes | ||
@@ -5,0 +6,0 @@ - **v1.4.1** - fixed typings for extra args (thanks [@rodrigoaddor](https://github.com/rodrigoaddor)) |
@@ -5,3 +5,3 @@ const Router = (o = {}) => | ||
? async (q, ...args) => { | ||
for ([p, hs] of t[(q.method || 'GET').toLowerCase()] || []) { | ||
for ([p, hs] of [ t.all || [], t[(q.method || 'GET').toLowerCase()] || [] ].flat()) { | ||
if (m = (u = new URL(q.url)).pathname.match(p)) { | ||
@@ -8,0 +8,0 @@ q.params = m.groups |
@@ -1,1 +0,1 @@ | ||
const e=(e={})=>new Proxy(e,{get:(r,a,o)=>"handle"===a?async(e,...a)=>{for([p,hs]of r[(e.method||"GET").toLowerCase()]||[])if(m=(u=new URL(e.url)).pathname.match(p))for(h of(e.params=m.groups,e.query=Object.fromEntries(u.searchParams.entries()),hs))if(void 0!==(s=await h(e,...a)))return s}:(s,...t)=>(r[a]=r[a]||[]).push([`^${(e.base||"")+s.replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:([^\/\?\.]+)(\?)?/g,"$2(?<$1>[^/.]+)$2")}/*$`,t])&&o});module.exports={Router:e}; | ||
const e=(e={})=>new Proxy(e,{get:(r,a,o)=>"handle"===a?async(e,...a)=>{for([p,hs]of[r.all||[],r[(e.method||"GET").toLowerCase()]||[]].flat())if(m=(u=new URL(e.url)).pathname.match(p))for(h of(e.params=m.groups,e.query=Object.fromEntries(u.searchParams.entries()),hs))if(void 0!==(s=await h(e,...a)))return s}:(s,...t)=>(r[a]=r[a]||[]).push([`^${(e.base||"")+s.replace(/(\/?)\*/g,"($1.*)?").replace(/\/$/,"").replace(/:([^\/\?\.]+)(\?)?/g,"$2(?<$1>[^/.]+)$2")}/*$`,t])&&o});module.exports={Router:e}; |
{ | ||
"name": "itty-router", | ||
"version": "1.4.3", | ||
"version": "1.5.0", | ||
"description": "Tiny, zero-dependency router with route param and query parsing.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/itty-router.min.js", |
@@ -44,3 +44,3 @@ ![Logo][logo-image] | ||
## Features | ||
- [x] tiny (~430 bytes) with zero dependencies | ||
- [x] tiny (~450 bytes) with zero dependencies | ||
- [x] route params, with optionals (e.g. `/api/:foo/:id?.:format?`) | ||
@@ -68,2 +68,5 @@ - [x] bonus query parsing (e.g. `?page=3&foo-bar`) | ||
The "instantiated" router translates any attribute (e.g. `.get`, `.post`, `.patch`, `.whatever`) as a function that binds a "route" (string) to route handlers (functions) on that method type (e.g. `router.get --> GET`, `router.post --> POST`). When the url fed to `.handle({ url })` matches the route and method, the handlers are fired sequentially. Each is given the original request/context, with any parsed route/query params injected as well. The first handler that returns (anything) will end the chain, allowing early exists from errors, inauthenticated requests, etc. This mechanism allows ANY method to be handled, including completely custom methods (we're very curious how creative individuals will abuse this flexibility!). The only "method" currently off-limits is `handle`, as that's used for route handling (see below). | ||
**Special Exception:** To allow nested routers (or middleware) to catch on all METHODS, any routes on the "all" channel will be processed first". If all routes are on "get", feel free to just use `.get`, but `.all` will allow routes/middleware/routers to match on ANY methods (useful when embedding nested routers that have full CRUD methods). For example: `router.all('/crud/*', crudRouter.handle)` | ||
```js | ||
@@ -137,3 +140,3 @@ // register a route on the "GET" method | ||
parentRouter.get('/todos/*', todosRouter.handle) // all /todos/* routes will route through the todosRouter | ||
parentRouter.all('/todos/*', todosRouter.handle) // all /todos/* routes will route through the todosRouter | ||
``` | ||
@@ -167,3 +170,3 @@ | ||
? async (q, ...args) => { | ||
for ([p, hs] of t[(q.method || 'GET').toLowerCase()] || []) { | ||
for ([p, hs] of [ t.all || [], t[(q.method || 'GET').toLowerCase()] || [] ].flat()) { | ||
if (m = (u = new URL(q.url)).pathname.match(p)) { | ||
@@ -170,0 +173,0 @@ q.params = m.groups |
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
15883
221