Skip to content

Middleware

Middleware > withParams - itty-router

withParams simply allows you to request route params directly from the Request itself, rather than through request.params. It does this by adding a fallback - if request['your param name'] isn't found, it tries again from request.params['your param name'].

That's all it does.

Example: without withParams

ts
router.get('/items/:id', ({ params }) => `Your id is ${params.id}.`)

Example: with withParams

ts
router.get('/items/:id', withParams, ({ id }) => `Your id is ${id}.`)

Including Globally

If using withParams, it's suggested to leverage this upstream (globally), rather than for each individual route.

Here's how to do it using each available Router:

ts
import { AutoRouter } from 'itty-router'

// withParams is included by default
export default AutoRouter()
  .get('/items/:id', ({ id }) => `Your id is ${id}.`)
ts
import { Router, withParams } from 'itty-router'

// add withParams to the before stage
export default Router({ before: [withParams] })
  .get('/items/:id', ({ id }) => `Your id is ${id}.`)
ts
import { IttyRouter, withParams } from 'itty-router'

export default IttyRouter()
  .all('*', withParams) // as global upstream middleware
  .get('/items/:id', ({ id }) => `Your id is ${id}.`)

Notes

  • AutoRouter includes withParams by default as a convenience.
  • For the fastest possible performance, use Router instead of AutoRouter and do not use withParams. The additional proxy layer over the Request object will fractionally slow down all requests of it.

Released under the MIT License.