API Reference

@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 (CreateTaserAppOptions)

Prop

Type


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((ctx, next) => {
  return next({ traceId: "123" });
});

// 2. Short Function Signature (Layout-Scoped)
const adminAuth = middleware("admin", (ctx, 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 (ctx, next) => {
    return next({ uploadedFile: ctx.body.file });
  });

// 4. Fluent Builder (Layout-Scoped with Inherited State)
const scopedMw = middleware("admin")
  .query(z.object({ filter: z.string() }))
  .handler(async (ctx, 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, 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, next) => ...): Implements middleware logic and returns a strongly-typed MiddlewareUnit.

For simple unvalidated middlewares, short function signatures middleware((ctx, next) => ...) and middleware<TState, TRequires>((ctx, 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 Reference

Prop

Type


ctx.cookies (TaserCookieJar) Reference

Prop

Type


TypeScript Utility Types

import type {
  InferAppContext,
  InferAppManifest,
  InferRouteContext,
  InferRouteInput,
  InferRouteOutput,
  RouteDefinition,
  RouteExport,
  TaserApp,
  TaserCookieJar,
  TaserCookieOptions,
  RouteManifestShape,
  ReturnsMap,
} from "@taserjs/router";

Context Type Extraction Best Practice

Extract route context from the builder variable before .handler() (e.g. typeof GET.$Infer.Context or InferRouteContext<typeof GET>) when typing extracted helper functions.