HTTP Router
Lightweight TypeScript decorators and a type-safe router for itty-router. Includes a build-time OpenAPI 3.1 generator. Ported from itty-decorators.
Features
Type-Safe Routing:
TypedRouterprovides full TypeScript support for request bodies, response types, and context.Auto JSON Enforcement: Automatically validates
Content-Type: application/jsonfor mutation methods (POST, PUT, PATCH).Multipart Support: Opt into
multipart/form-datahandling withMultipart<T>and{ multipart: true }.Declarative Metadata: Use
@Controllerand@Endpointdecorators to document your API logic directly in code.DI Integration:
@Controllercomposes the core DI@Containerdecorator, so controllers are auto-registered and can use@Componentinjection anduseContainer().resolve(...).OpenAPI 3.1 Support: Generate a complete OpenAPI specification from your code at build time.
Minimal Footprint: Built on top of the ultra-light
itty-router.
Installation
Quick Start
1. Create a Controller (DI-aware)
Annotate your API logic using decorators and the TypedRouter. Controllers are automatically registered with the DI container, so you can inject services and resolve the controller instance.
2. Multipart File Uploads
Use Multipart<T> and { multipart: true } to accept multipart/form-data instead of JSON. The handler receives req.content typed as FormData.
3. Path and Query Parameters
Use PathParams<T> and QueryParams<T> combined with RequestSpec<...> to provide strong typing for URL path parameters (e.g., /user/:id) and query string parameters (e.g., ?search=term). The handler will receive them in req.params and req.query.
OpenAPI Generation
@di-framework/http provides a built-in CLI and a registry to generate OpenAPI specs from your controllers.
Using the CLI
The easiest way to generate a spec is using the provided CLI tool.
Options:
--controllers <path>: (Required) Path to the file that imports all your decorated controllers.--output <path>: (Optional) Path to save the generated JSON (default:openapi.json).
Manual Generation
You can also generate the spec programmatically using the generateOpenAPI function and the default registry:
If you need full control, you can iterate the registry manually:
API Reference
TypedRouter<Args[]>()
A proxy for itty-router that enables type-safe method definitions.
Args: An array of types representing additional arguments passed tofetch(e.g.,[Env, ExecutionContext]).
json<T>(data: T, init?: ResponseInit)
A typed wrapper around itty-router's json helper.
Json<T> / Multipart<T>
Body spec markers used with RequestSpec<> to declare the expected content type. Json<T> types req.content as T; Multipart<T> types it as FormData. Multipart routes require passing { multipart: true } as the third argument to the route method.
PathParams<T> / QueryParams<T>
Spec markers used with RequestSpec<> to declare the expected type of path and query parameters. PathParams<T> types req.params as T; QueryParams<T> types req.query as T.
@Controller(options?)
Composed decorator that:
Marks a class for inclusion in the OpenAPI registry; and
Registers the class with the core DI container (same instance as
@di-framework/core).
Options: { singleton?: boolean; container?: DIContainer }
@Endpoint(metadata)
Method or property decorator that attaches OpenAPI metadata.
summary: Short summary of the operation.description: Verbose explanation.requestBody: OpenAPI Request Body object.responses: OpenAPI Responses object.
HttpRouter.builder() & @HttpRouter(options?)
An extensible, fluent builder above TypedRouter() and its corresponding decorator:
prefix(pathPrefix): Sets a global base path prefix for registered routes.catch(handler): Registers a custom global error handler.use(...middleware): Registers global middleware executed on all routes.withAuth(options): Extension point for auth integrations without introducing runtime dependencies in@di-framework/http.extend(fn): Register custom builder extensions.