Worker
A tiny, opinionated routing primitive for Cloudflare Workers. The package gives you a typed createWorker(routes) factory plus a handful of helpers for parsing query parameters, building JSON responses and signalling validation errors.
There are no plugins, no middleware system and no router DSL. Routes are a flat object keyed by pathname, and the framework's job is to dispatch the request, catch domain exceptions, and translate them into the correct HTTP status.
Categories
- Installation
createWorker— the entry-point factory- Request helpers —
queryDate,queryInteger,queryPosition - Response helpers —
json,error - Errors —
InvalidValueError,MissingParameterError,NotFoundError - Types —
Routes<TBindings>,Worker<TBindings>,Coords
Quick example
ts
import {
createWorker,
json,
queryDate
} from '@basmilius/worker';
interface Bindings {
DB: D1Database;
}
export default createWorker<Bindings>({
'/api/echo': async (req, bindings) => {
const date = queryDate(req, 'date');
return json({
ok: true,
date: date.toISO()
});
}
});Error mapping at a glance
| Thrown | HTTP status | Error code |
|---|---|---|
InvalidValueError | 406 | invalid_value |
MissingParameterError | 400 | missing_parameter |
NotFoundError | 404 | not_found |
| Anything else | 500 | internal_server_error |