Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

itty-router

kwhitley506.8kMIT5.0.22TypeScript support: included

A tiny, zero-dependency router, designed to make beautiful APIs in any environment.

api, router, cloudflare, workers, worker, serverless, cors, middleware, rest, serviceworker, nested

readme


Itty Router

Version Bundle Size Build Status Coverage Downloads Issues Discord Github Follow @ittydev

v5 Documentation  |   v4 -> v5 Migration Guide  |   Discord


An ultra-tiny API microrouter, for use when size matters (e.g. Cloudflare Workers).

Features

  • Tiny. Routers from ~450 bytes to a ~970 bytes batteries-included version (~240-500x smaller than Express.js).
  • TypeScript. Powerfully (and flexibly) typed for any environment.
  • Route-parsing & query parsing.
  • Middleware. Use ours or write your own.
  • 100% Test Coverage. Bulletproof for production peace-of-mind.
  • Designed specifically for serverless (but works anywhere).
  • No assumptions. Return anything; pass in anything.
  • Future-proof. HTTP methods not-yet-invented already work with it.

Example

import { AutoRouter } from 'itty-router' // ~1kB

const router = AutoRouter()

router
  .get('/hello/:name', ({ name }) => `Hello, ${name}!`)
  .get('/json', () => [1,2,3])
  .get('/promises', () => Promise.resolve('foo'))

export default { ...router } // strips the proxy

// that's it ^-^

Need Help?

Complete API documentation is available on itty.dev, or join our Discord channel to chat with community members for quick help!

Join the Discussion!

Have a question? Suggestion? Idea? Complaint? Want to send a gift basket? Join us on Discord!

A Special Thanks :heart:

As the community and contributor list has grown (and thus an individualized list here is no longer easily maintainable), I'd like to thank each and every one of you for making itty far greater than its humble origins. The robustness you see today, the careful consideration of every byte spent on features, the API choices, the code-golfing itself... are all thanks to the efforts and feedback from the community. I'd especially like to thank the core contributors and PR-authors, as well as the fantastic folks on the itty Discord group, for their tireless work refining this little beast and answering community questions.

changelog

Changelog

v5.0.20

  • docs: export default { ...router } as README example

    v5.0.17

  • fixed: corsify should clone response before appending headers

    v5.0.16

  • maintenance: README

    v5.0.15

  • maintenance: types cleanup and publishing test

    v5.0.14

  • maintenance: types cleanup and publishing test

    v5.0.13

  • fixed: Router/AutoRouter stages were not connected to router-level generics

    v5.0.12

  • fixed: ./types was not being properly exported

    v5.0.10

  • fixed: response formatters in finally stage could still cross pollute headers in Node

    v5.0.9

  • fixed: cors preflight should reflect requested headers as the default (required for credentials)

    v5.0.7

  • fixed: withParams could attempt to bind null (collision with node adapter)

    v5.0.6

  • fixed: corsify as replacing status codes (now mutates original response)

    v5.0.5

  • fixed: corsify now properly ignores WebSocket responses

    v5.0.4

  • fixed: (TypeScript) middleware corrupting downstream request types and args

    v5.0.2

  • fixed: AutoRouter was missing the router-level generics support of the other 2 routers.
  • fixed: All 3 routers had their 3rd generic argument, ResponseType added per the spec.

    v5.0.0

  • BREAKING: router.fetch replaces router.handle (now deprecated)
  • BREAKING: "createCors" has been deprecated in favor of "cors" (new options & requirements)
  • changed: previous Router (smallest) is now IttyRouter
  • added: Router (full backwards compatability with previous Router) has been added, including:
    • "before" stage (array of request handlers)
    • "finally" stage (array of response handlers)
    • "catch" stage (single error handler)
  • added: AutoRouter (batteries-included Router)
  • fixed (TS): Routers types have been modified to allow both rotuer-level generics AND route-level generics in the same instance.
  • docs: see Migration guide at https://itty.dev/itty-router/migrations/v4-v5

For changes prior to v5, see the v4 CHANGELOG