@taserjs/router
Complete API reference for @taserjs/router: createTaserApp, createContext, fluent RouteBuilder chaining, reply helpers, and core types.
The @taserjs/router package is the core routing and context engine of the Taser.js ecosystem.
Core Functions
createTaserApp(options?)
Creates a new TaserRouter builder instance.
function createTaserApp(options?: CreateTaserAppOptions): TaserRouter;Options (TaserAppOptions on defineTaser)
Prop
Type
Cookies are middleware, not defineTaser options
Mount cookie({ secret, ... }) from @taserjs/router/middleware/cookie on a layout. See
Cookie Management.
createContext(definition)
Defines application boot singletons and per-request metadata.
function createContext<TBoot, TReq>(definition: {
boot?: () => Promise<TBoot> | TBoot;
request?: (req: Request) => Promise<TReq> | TReq;
}): ContextDefinition<TBoot, TReq>;Prop
Type
middleware()
Constructs a standalone, reusable middleware unit with support for fluent schema chaining (query, params, body, returns), body mode tagging ("json" | "form" | "urlencoded"), layout scoping, multi-layout branch unions, and state preconditions:
// 1. Short Function Signature (Unscoped, Inferred State)
const log = middleware((_args, next) => {
return next({ traceId: "123" });
});
// 2. Short Function Signature (Layout-Scoped)
const adminAuth = middleware("admin", (_args, next) => {
return next({ role: "admin" });
});
// 3. Fluent Builder (Unscoped, with Tagged Body Mode & Schemas)
const uploadMw = middleware()
.query(z.object({ tag: z.string() }))
.params(z.object({ id: z.coerce.number() }))
.body("form", z.object({ file: z.instanceof(File) })) // tagged bodyMode!
.returns({ 400: z.object({ error: z.string() }) })
.handler(async ({ req }, next) => {
return next({ uploadedFile: req.body.file });
});
// 4. Fluent Builder (Layout-Scoped with Inherited State)
const scopedMw = middleware("admin")
.query(z.object({ filter: z.string() }))
.handler(async (_args, next) => {
return next({ permission: "write" });
});honoMw()
Adapts a Web Standard / Hono-compatible middleware function (c, next) => ... into a Taser.js-compatible middleware handler function ({ ctx, req }, next) => Promise<Response>.
import { honoMw, middleware } from "@taserjs/router";
import { cors } from "hono/cors";
// Used directly in route/layout chains
t.get("/hello").use(honoMw(cors()));
// Or wrapped in middleware
const corsMiddleware = middleware(honoMw(cors()));Builder Methods & Generics
.query(schema): Attaches query parameter validation schema..params(schema): Attaches path parameter validation schema..body(schema)/.body(mode, schema): Attaches JSON or tagged ("form" | "urlencoded") body validation schema..requires<{ state?, params?, query?, body? }>(): Declares required upstream preconditions across request facets (state,params,query,body). Checked at compile time by.use(...)..handler(({ ctx, req, state }, next) => ...): Implements middleware logic and returns a strongly-typedMiddlewareUnit.
For simple unvalidated middlewares, short function signatures middleware((args, next) => ...) and layout-scoped middleware("/admin", (args, next) => ...) are also supported.
TaserRouter Class Methods
Prop
Type
t Route Builders (t.<verb>)
Imported as import { t } from "@taserjs/router". t provides builder factory functions for defining file-based route endpoints and layouts.
Prop
Type
RouteBuilder Fluent Methods
Prop
Type
Tree-Shakeable Reply Helpers (@taserjs/router/reply)
Prop
Type
stream Helpers (@taserjs/router/stream)
Edge-compatible Web Standard helpers (no filesystem file() helper):
Prop
Type
cookie() Middleware & TaserCookieJar
Import cookie from @taserjs/router/middleware/cookie and mount it on a layout (t.layout("/*").use(cookie({ secret }))). Handlers under that layout receive { cookies: TaserCookieJar }.
Prop
Type
TypeScript Utility Types
import type {
BodyMode,
ContextDefinition,
HeaderKey,
HttpMethod,
LayoutDefinition,
MiddlewareArgs,
MiddlewareDefinition,
MiddlewareHandler,
MiddlewareInput,
MiddlewarePreconditions,
NextFunction,
NotFoundHandler,
OnErrorHandler,
RequestHeader,
ResponseOptions,
RouteBodySchema,
RouteBuilder,
RouteDefinition,
RouteHandler,
RouteHandlerArgs,
RouteSchemas,
RouterRegister,
StandardSchemaV1,
StatusCode,
TaserAppOptions,
TaserDefinition,
TaserHeaders,
TaserRequest,
ValidationFacet,
} from "@taserjs/router";
import type {
Cookie,
CookieJarOptions,
CookieOptions,
TaserCookieJar,
} from "@taserjs/router/middleware";Context Type Extraction Best Practice
Extract handler argument types from the builder before .handler() (e.g. Parameters<typeof GET.handler>[0]) when typing extracted helper functions.