Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
itty-router
Advanced tools
It's an itty bitty router. Like... super tiny, with zero dependencies. For reals.
Did we mention it supports route/query params like in Express.js?
yarn add itty-router
or if you've been transported back to 2017...
npm install itty-router
/api/:collection/:id
)/todos/:id?
){ params: { foo: 'bar' }}
){ query: { page: '3' }}
from /foo?page=3
)import { Router } from 'itty-router'
// create a Router
const router = Router()
// basic GET routs
router.get('/todos/:id', console.log)
// first match always wins, so be careful with order of registering routes
router
.get('/todos/oops', () => console.log('you will never see this'))
.get('/chainable', () => console.log('because why not?')) // this may be dropped to save characters...
.get('/optional/:support?', () => console.log('optionals!')) // will match /optional and /optional/14 both
// works with POST, DELETE, PATCH, etc
router.post('/todos', () => console.log('posted a todo'))
// ...or any other method we haven't yet thought of (thanks to @mvasigh implementation of Proxy <3)
router.future('/todos', () => console.log(`this caught using the FUTURE method!`))
// then handle a request!
router.handle({ method: 'GET', url: 'https://foo.com/todos/13?foo=bar' })
// {
// method: 'GET',
// url: 'http://foo.com/todos/13?foo=bar',
// path: '/todos/13',
// index: 0,
// params: { id: '13' },
// query: { foo: 'bar' }
// }
addEventListener('fetch', ({ request }) => router.handle(request))
yarn test
const Router = () => new Proxy({}, {
get: (obj, prop) => prop === 'handle'
? (req) => {
let { pathname: path, searchParams } = new URL(req.url)
for (let [route, handler] of obj[req.method.toLowerCase()] || []) {
if (hit = path.match(route)) {
return handler({
...req,
params: hit.groups,
path,
query: Object.fromEntries(searchParams.entries())
})
}
}
}
: (path, handler) =>
(obj[prop] = obj[prop] || []).push([path.replace(/(\/:([^\/\?]+)(\?)?)/gi, '/$3(?<$2>[^\/]+)$3'), handler]) && obj
})
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.
FAQs
A tiny, zero-dependency router, designed to make beautiful APIs in any environment.
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
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.