itty-router
Advanced tools
Comparing version 2.1.4 to 2.1.5
{ | ||
"name": "itty-router", | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"description": "Tiny, zero-dependency router with route param and query parsing.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/itty-router.min.js", |
@@ -74,3 +74,3 @@ ![Logo][logo-image] | ||
const router = Router() // no "new", as this is not a real class/constructor! | ||
const router = Router() // no "new", as this is not a real class | ||
``` | ||
@@ -83,6 +83,5 @@ | ||
router.get('/todos/:user/:item?', (req) => { | ||
const { params, query, url } = req | ||
const { user, item } = params | ||
const { params, query } = req | ||
console.log({ user, item, query }) | ||
console.log({ params, query }) | ||
}) | ||
@@ -101,6 +100,33 @@ ``` | ||
// Example outputs (using route handler from step #2 above): | ||
// GET /todos/jane/13 --> { user: 'jane', item: '13', query: {} } | ||
// GET /todos/jane --> { user: 'jane', query: {} } | ||
// GET /todos/i?limit=2&page=1 --> { user: 'i', query: { limit: '2', page: '2' } } | ||
/* | ||
Example outputs (using route handler from step #2 above): | ||
GET /todos/jane/13 | ||
{ | ||
params: { | ||
user: 'jane', | ||
item: '13' | ||
}, | ||
query: {} | ||
} | ||
GET /todos/jane | ||
{ | ||
params: { | ||
user: 'jane' | ||
}, | ||
query: {} | ||
} | ||
GET /todos/jane?limit=2&page=1 | ||
{ | ||
params: { | ||
user: 'jane' | ||
}, | ||
query: { | ||
limit: '2', | ||
page: '2' | ||
} | ||
} | ||
*/ | ||
``` | ||
@@ -112,25 +138,25 @@ | ||
```js | ||
// lets save a missing handler | ||
const missingHandler = new Response('Not found.', { status: 404 }) | ||
// lets save a missing handler | ||
const missingHandler = new Response('Not found.', { status: 404 }) | ||
// create a parent router | ||
const parentRouter = Router() | ||
// create a parent router | ||
const parentRouter = Router() | ||
// and a child router | ||
const todosRouter = Router({ base: '/todos' }) | ||
// and a child router | ||
const todosRouter = Router({ base: '/todos' }) | ||
// with some routes on it... | ||
todosRouter | ||
.get('/', () => new Response('Todos Index')) | ||
.get('/:id', ({ params }) => new Response(`Todo #${params.id}`)) | ||
// with some routes on it... | ||
todosRouter | ||
.get('/', () => new Response('Todos Index')) | ||
.get('/:id', ({ params }) => new Response(`Todo #${params.id}`)) | ||
// then divert ALL requests to /todos/* into the child router | ||
parentRouter | ||
.all('/todos/*', todosRouter.handle) // attach child router | ||
.all('*', missingHandler) // catch any missed routes | ||
// then divert ALL requests to /todos/* into the child router | ||
parentRouter | ||
.all('/todos/*', todosRouter.handle) // attach child router | ||
.all('*', missingHandler) // catch any missed routes | ||
// GET /todos --> Todos Index | ||
// GET /todos/13 --> Todo #13 | ||
// POST /todos --> missingHandler (caught eventually by parentRouter) | ||
// GET /foo --> missingHandler | ||
// GET /todos --> Todos Index | ||
// GET /todos/13 --> Todo #13 | ||
// POST /todos --> missingHandler (caught eventually by parentRouter) | ||
// GET /foo --> missingHandler | ||
``` | ||
@@ -137,0 +163,0 @@ |
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
291
17087