itty-router
Advanced tools
Comparing version 2.1.3 to 2.1.4
{ | ||
"name": "itty-router", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"description": "Tiny, zero-dependency router with route param and query parsing.", | ||
@@ -5,0 +5,0 @@ "main": "./dist/itty-router.min.js", |
@@ -24,13 +24,18 @@ ![Logo][logo-image] | ||
// register some routes | ||
router | ||
.get('/todos', () => new Response('Todos Index!')) // GET index | ||
.get('/todos/:id', request => new Response(`Todo #${request.params.id}`)) // GET item | ||
.post('/todos', async request => { // POST new item | ||
const content = await request.json() | ||
// GET collection index | ||
router.get('/todos', () => new Response('Todos Index!')) | ||
return new Response('Creating a new Todo with following payload: ' + JSON.stringify(content)) | ||
}) | ||
.all('*', () => new Response('Not Found.', { status: 404 })) // 404 otherwise | ||
// GET item | ||
router.get('/todos/:id', ({ params }) => new Response(`Todo #${params.id}`)) | ||
// POST to the collection (we'll use async here) | ||
router.post('/todos', async request => { | ||
const content = await request.json() | ||
return new Response('Creating Todo: ' + JSON.stringify(content)) | ||
}) | ||
// 404 for everything else | ||
router.all('*', () => new Response('Not Found.', { status: 404 })) | ||
// attach the router "handle" to the event handler | ||
@@ -70,3 +75,3 @@ addEventListener('fetch', event => | ||
const router = Router() // no "new", as this is not a real ES6 class/constructor! | ||
const router = Router() // no "new", as this is not a real class/constructor! | ||
``` | ||
@@ -97,5 +102,5 @@ | ||
// 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/jane?limit=2&page=1 --> { user: 'jane', query: { limit: '2', page: '2' } } | ||
// 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' } } | ||
``` | ||
@@ -108,3 +113,3 @@ | ||
// lets save a missing handler | ||
const missingHandler = new Response('That resource was not found.', { status: 404 }) | ||
const missingHandler = new Response('Not found.', { status: 404 }) | ||
@@ -111,0 +116,0 @@ // create a parent router |
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
265
17106