@philiprehberger/typed-router
v0.1.7
Published
Framework-agnostic typed URL routing with path parameter extraction
Readme
@philiprehberger/typed-router
Framework-agnostic typed URL routing with path parameter extraction
Installation
npm install @philiprehberger/typed-routerUsage
import { createRouter } from '@philiprehberger/typed-router';
const router = createRouter();
router.add('GET', '/users', () => listUsers());
router.add('GET', '/users/:id', (params) => getUser(params.id));
router.add('POST', '/users', () => createUser());
router.add('PUT', '/users/:id/roles/:role', (params) => setRole(params.id, params.role));
const match = router.match('GET', '/users/123');
if (match) {
match.params; // { id: '123' }
match.handler(match.params);
}
router.routes(); // list all registered routesAPI
| Export | Description |
|--------|-------------|
| createRouter() | Create a new router instance |
Router
| Method | Description |
|--------|-------------|
| add(method, path, handler) | Register a route with :param placeholders |
| match(method, url) | Find matching route, returns { handler, params } or null |
| routes() | List all registered routes |
Development
npm install
npm run build
npm testSupport
If you find this project useful:
