Route
The ordinary route: a method, an endpoint, and a handler.
Route is the RouteVariant.dynamic member of the RouteBase family and the one most code uses. The others exist for responses the framework can produce itself — files, directories, socket upgrades — while this one just runs your function.
import { Route } from "@ozanarslan/corpus";
new Route("GET /users/:id", (c) => findUser(c.params.id));A RouteConfig adds schemas for the body, search and params, which both validate the request and type Context, so c.params.id is known to be whatever the schema says it is.Contents
Route
class
class Route<B = unknown, S = unknown, P = unknown, R = unknown, E extends string = string, >extends RouteBase<B, S, P, R, E>A route handled by a function.
Constructing one registers it on the nearest App, which compiles it into the server's route map at App.listen time along with whatever Middleware targets it.
The type parameters are usually inferred from the RouteConfig rather than written out, and flow into the Context the handler receives.
Type parameters
B— Parsed Context.body type.S— Parsed Context.search type.P— Parsed Context.params type.R— Response body type.E— The literal endpoint type, carried so the endpoint stays narrowly typed at the call site.
Route.constructor()
constructor();
constructor(address: RouteAddress<E>, callback: ContextHandler<B, S, P, R>, model?: RouteConfig<B, S, P, R>, );
constructor(address?: RouteAddress<E>, callback?: ContextHandler<B, S, P, R>, model?: RouteConfig<B, S, P, R>, )Creates a route for subclasses, which declare Route.method, Route.endpoint and Route.handler as class fields and call RouteBase.register themselves.
Creates a route and registers it on the nearest App.
Parameters
address— The RouteAddress: a"METHOD /endpoint"string or a method-and-endpoint pair.callback— The ContextHandler that answers the request.model— Optional RouteConfig declaring validation schemas and per-route limits. Its schemas also type theContextthe handler receives.
Route.variant
readonly variant: RouteVariantMarks this route as RouteVariant.dynamic for App route compilation.
Route.method
readonly method!: MethodThe Method this route answers, taken from the RouteAddress.
Route.endpoint
readonly endpoint!: EThe path this route answers. Supports :name parameters and a trailing wildcard, both of which arrive in Context.params.
Route.config
readonly config?: RouteConfig<B, S, P, R>Validation schemas and per-route limits. Absent means nothing is validated — and, since getContextAccess inspects it, absent schemas are one of the signals that a surface need not be parsed at all.
Route.handler
override handler!: ContextHandler<B, S, P, R>The function that answers the request.
Return a value to send it as the body, a Res to control the whole response, or nothing after mutating Context.res directly.