Skip to content

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

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

ThrownHTTP statusError code
InvalidValueError406invalid_value
MissingParameterError400missing_parameter
NotFoundError404not_found
Anything else500internal_server_error