Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
itty-router
Advanced tools
Tiny, zero-dependency API router - built for Cloudflare Workers, but works everywhere!
Tiny (the router alone is >500 bytes), zero-dependency router - built for Cloudflare Workers, but works everywhere!
This router avoids all assumptions about how or where you'll use it, what you'll return with it, etc. It specifically chooses a linear flow-through route-matching pattern to allow the most power and flexibility in middleware chaining (including other routers for nested APIs). By giving you complete control over the routes, middleware chain, and downstream handling, you can even make your own, more opinionated router with itty at the core, for a mere ~500 bytes.
import {
error, // create an error response
respondWithError, // downstream handler for thrown errors
respondWithJSON, // downstream handler to send data as JSON
Router, // the ~470 byte router itself
withParams, // middleware to auto-embed route params
notFound, // create a 404
} from 'itty-router'
const router = Router() // create a new Router
const todos = [] // and some fake todos
router
// add some global middleware
// withParams auto-parses route params into the request
.all('*', withParams)
// GET list of todos
.get('/todos', () => todos)
// GET single todo, by ID
.get('/todos/:id',
({ id }) => todos.find(todo => todo.id === id)
|| notFound('That todo was not found')
)
// 404 for everything else
.all('*', notFound)
// Example: Cloudflare ESM Worker syntax
export default {
fetch: (request, env, context) => router
.handle(request, env, context)
.then(respondWithJSON) // automatically send as JSON
.catch(respondWithError) // and send error Responses for thrown errors
}
For complete API documentation, please visit itty.dev.
Have a question? Suggestion? Complaint? Want to send me a gift basket?
Join us on Discord!
yarn
yarn dev
yarn verify
This repo goes out to my past and present colleagues at Arundo - who have brought me such inspiration, fun,
and drive over the last couple years. In particular, the absurd brevity of this code is thanks to a
clever [abuse] of Proxy
, courtesy of the brilliant @mvasigh.
This trick allows methods (e.g. "get", "post") to by defined dynamically by the router as they are requested,
drastically reducing boilerplate.
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
FAQs
A tiny, zero-dependency router, designed to make beautiful APIs in any environment.
The npm package itty-router receives a total of 0 weekly downloads. As such, itty-router popularity was classified as not popular.
We found that itty-router demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.