Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

itty-router

Package Overview
Dependencies
Maintainers
2
Versions
265
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

itty-router - npm Package Compare versions

Comparing version 2.1.2 to 2.1.3

2

package.json
{
"name": "itty-router",
"version": "2.1.2",
"version": "2.1.3",
"description": "Tiny, zero-dependency router with route param and query parsing.",

@@ -5,0 +5,0 @@ "main": "./dist/itty-router.min.js",

@@ -28,3 +28,3 @@ ![Logo][logo-image]

.get('/todos/:id', request => new Response(`Todo #${request.params.id}`)) // GET item
.delete('/todos', async request => { // POST new item
.post('/todos', async request => { // POST new item
const content = await request.json()

@@ -46,8 +46,8 @@

- [x] Route params, with optional param support (e.g. `/api/:collection/:id?`)
- [x] [Format support](#file-format-support) (e.g. `/api/items.:format`) to handle things like `.csv`/`.json` within same route
- [x] [Format support](#file-format-support) (e.g. `/api/items.:format`) for **.csv**, **.json**, etc. within same route
- [x] Query parsing (e.g. `?page=3&foo=bar` will add a `request.query` object with keys `page` and `foo`)
- [x] Wildcard support for nesting, global middleware, etc. (e.g. `/api/*`)
- [x] Middleware support. Any number of sync/async [middleware handlers](#middleware) may be passed to a route/wildcard.
- [x] Nestable. Supports [nested routers](#nested-routers-with-404-handling) for API branching.
- [x] Supports ANY method (e.g. `router.puppy('/:name', handler)` will match to method `PUPPY`)
- [x] [Middleware support](#middleware). Any number of sync/async handlers may be passed to a route.
- [x] [Nestable](#nested-routers-with-404-handling). Supports nesting routers for API branching.
- [x] Supports **any** method type (e.g. `router.puppy('/:name', handler)` would work)
- [x] Route match to multiple methods using the ["all" channel](#nested-routers-with-404-handling)

@@ -57,3 +57,3 @@ - [x] Define [base path](#nested-routers-with-404-handling) per router to prefix all routes (useful for nested routers)

- [x] Chainable route declarations (why not?)
- [ ] have pretty code (yeah right...)
- [ ] Readable internal code (yeah right...)

@@ -80,4 +80,4 @@ # Options API

router.get('/todos/:user/:item?', (req) => {
let { params, query, url } = req
let { user, item } = params
const { params, query, url } = req
const { user, item } = params

@@ -124,8 +124,8 @@ console.log({ user, item, query })

parentRouter
.all('/todos/*', todosRouter.handle) // all /todos/* routes will route through the todosRouter
.all('*', missingHandler)
.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 (it will fail to catch inside todosRouter, and be caught by final "all" on parentRouter)
// POST /todos --> missingHandler (caught eventually by parentRouter)
// GET /foo --> missingHandler

@@ -135,6 +135,6 @@ ```

### Middleware
*Any handler that does NOT return* will effectively be considered "middleware", continuing to execute future functions/routes until one returns, closing the response.
Any handler that does not **return** will effectively be considered "middleware", continuing to execute future functions/routes until one returns, closing the response.
```js
// withUser modifies original request, but returns nothing (allowing flow to continue)
// withUser modifies original request, but returns nothing
const withUser = request => {

@@ -146,9 +146,11 @@ request.user = { name: 'Mittens', age: 3 }

const requireUser = request => {
if (!request.user) return new Response('Not Authenticated', { status: 401 })
if (!request.user) {
return new Response('Not Authenticated', { status: 401 })
}
}
// showUser returns a response with the user, as it is assumed to exist at this point
// showUser returns a response with the user (assumed to exist)
const showUser = request => new Response(JSON.stringify(request.user))
// now let's add some routes
router

@@ -169,2 +171,3 @@ .get('/pass/user', withUser, requireUser, showUser)

```js
// middleware that injects a user, but doesn't return
const withUser = request => {

@@ -176,5 +179,6 @@ request.user = { name: 'Mittens', age: 3 }

.get('*', withUser) // embeds user before all other matching routes
.get('/user', request => new Response(JSON.stringify(request.user))) // user embedded already!
.get('/user', request => new Response(`Hello, ${user.name}!`))
router.handle({ url: 'https://example.com/user' }) // --> STATUS 200: { name: 'Mittens', age: 3 }
router.handle({ url: 'https://example.com/user' })
// STATUS 200: Hello, Mittens!
```

@@ -193,9 +197,9 @@

## Testing & Contributing
1. fork repo
2. add code
3. run tests (add your own if needed) `yarn dev`
4. verify tests run once minified `yarn verify`
5. commit files (do not manually modify version numbers)
6. submit PR
7. we'll add you to the credits :)
1. Fork repo
2. Run tests (add your own if needed) `yarn dev`
3. Add your code (tests will re-run in the background)
4. Verify tests run once minified `yarn verify`
5. Commit files (do not manually modify version numbers)
6. Submit PR with a detailed description of what you're doing
7. I'll add you to the credits! :)

@@ -208,3 +212,4 @@ ## The Entire Code (for more legibility, [see src on GitHub](https://github.com/kwhitley/itty-router/blob/v1.x/src/itty-router.js))

? async (r, ...a) => {
for ([p, hs] of t.r.filter(i => i[2] === r.method || i[2] === 'ALL')) {
for (let [p, hs] of t.r.filter(i => i[2] === r.method || i[2] === 'ALL')) {
let m, s, u
if (m = (u = new URL(r.url)).pathname.match(p)) {

@@ -214,3 +219,3 @@ r.params = m.groups

for (h of hs) {
for (let h of hs) {
if ((s = await h(r, ...a)) !== undefined) return s

@@ -226,3 +231,3 @@ }

.replace(/\/$/, '')
.replace(/:(\w+)(\?)?/g, '$2(?<$1>[^/\.]+)$2')
.replace(/:(\w+)(\?)?(\.)?/g, '$2(?<$1>[^/$3]+)$2$3')
}\/*$`,

@@ -258,3 +263,3 @@ hs,

# Contributors
These folks are the real heroes, making open source the powerhouse that it is! Help us out and add your name to this list!
These folks are the real heroes, making open source the powerhouse that it is! Help out and get your name added to this list! <3

@@ -261,0 +266,0 @@ #### Core, Concepts, and Codebase

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc