http/route_spec.ts

Introspectable route spec system for Hono apps.

Routes are defined as data (method, path, auth, input/output schemas, handler), then applied to Hono. The attack surface is generated from the specs — always accurate, always complete.

Input/output schemas align with SAES ActionSpec conventions: - input: Zod schema for the request body (z.null() for no body) - output: Zod schema for the success response body - z.strictObject() for inputs (reject unknown keys)

Declarations
#

12 declarations

view source

apply_middleware_specs
#

http/route_spec.ts view source

(app: Hono<BlankEnv, BlankSchema, "/">, specs: MiddlewareSpec[]): void

Apply named middleware specs to a Hono app.

app

the Hono app

type Hono<BlankEnv, BlankSchema, "/">

specs

middleware specs to apply

type MiddlewareSpec[]

returns

void

apply_route_specs
#

http/route_spec.ts view source

(app: Hono<BlankEnv, BlankSchema, "/">, specs: RouteSpec[], resolve_auth_guards: AuthGuardResolver, log: Logger, db: Db): void

Apply route specs to a Hono app.

For each spec: resolves auth to guards via the provided resolver, adds input validation middleware (for routes with non-null input schemas), wraps handler with DEV-only output and error validation, and registers the route.

Each handler receives a RouteContext with: - db: transaction-scoped (for non-GET) or pool-level (for GET) - background_db: always pool-level - pending_effects: fire-and-forget effect queue

app

the Hono app

type Hono<BlankEnv, BlankSchema, "/">

specs

route specs to apply

type RouteSpec[]

resolve_auth_guards

log

the logger instance

type Logger

db

database instance for transaction wrapping and RouteContext

type Db

returns

void

AuthGuardResolver
#

get_route_input
#

http/route_spec.ts view source

<T>(c: Context<any, any, {}>): T

Get validated input from the Hono context.

Call this in route handlers after the input validation middleware has run. The type parameter should match the route's input schema.

c

type Context<any, any, {}>

returns

T

the validated request body

get_route_params
#

http/route_spec.ts view source

<T>(c: Context<any, any, {}>): T

Get validated URL path params from the Hono context.

Call this in route handlers after the params validation middleware has run. The type parameter should match the route's params schema.

TODO

c

type Context<any, any, {}>

returns

T

the validated path parameters

get_route_query
#

http/route_spec.ts view source

<T>(c: Context<any, any, {}>): T

Get validated URL query params from the Hono context.

Call this in route handlers after the query validation middleware has run. The type parameter should match the route's query schema.

c

type Context<any, any, {}>

returns

T

the validated query parameters

prefix_route_specs
#

http/route_spec.ts view source

(prefix: string, specs: RouteSpec[]): RouteSpec[]

Prepend a prefix to all route spec paths.

prefix

the path prefix (e.g. /api/account)

type string

specs

route specs to prefix

type RouteSpec[]

returns

RouteSpec[]

new array of specs with prefixed paths

RouteAuth
#

http/route_spec.ts view source

RouteAuth

Auth requirement for a route — none, authenticated, a specific role, or keeper.

{type: 'none'} means the route is open to all clients — including non-browser callers (CLI, API tokens, scripts). No session or auth middleware guards are applied.

RouteContext
#

http/route_spec.ts view source

RouteContext

Per-request deps provided by the framework to route handlers.

db is transaction-scoped for mutation routes and pool-level for reads. background_db is always pool-level — use it for fire-and-forget effects that must outlive the transaction.

db

Transaction-scoped for mutations, pool-level for reads.

type Db

background_db

Always pool-level — for fire-and-forget effects that outlive the transaction.

type Db

pending_effects

Fire-and-forget side effects — push here for post-response flushing.

type Array<Promise<void>>

RouteHandler
#

http/route_spec.ts view source

RouteHandler

Route handler function — receives the Hono context and a RouteContext with per-request deps (db, background_db, pending_effects).

TypeScript allows fewer params, so handlers that don't need route can use (c) => ... without changes.

RouteMethod
#

RouteSpec
#

http/route_spec.ts view source

RouteSpec

A single route definition — the unit of the surface map.

input and output schemas align with SAES ActionSpec naming. Use z.null() for routes with no request body (GET, DELETE without body).

method

path

type string

auth

Auth requirement for this route.

{type: 'none'} means the route is open to all clients including non-browser callers (CLI, scripts) — no auth guards are applied.

handler

description

type string

params

URL path parameter schema. Use z.strictObject() with string fields matching :param segments.

TODO

type z.ZodObject

query

URL query parameter schema. Use z.strictObject() with string fields.

type z.ZodObject

input

Request body schema. Use z.null() for routes with no body.

type z.ZodType

output

Success response body schema.

type z.ZodType

rate_limit

Rate limit key type — declares what this route's rate limiter is keyed on.

When set, 429 (RateLimitError) is auto-derived in derive_error_schemas. The actual RateLimiter instance is still wired imperatively in the handler — this field is metadata for surface introspection and policy invariants.

errors

Handler-specific error response schemas keyed by HTTP status code.

Middleware errors (auth 401/403, validation 400, rate limit 429) are auto-derived from auth, input, and rate_limit. Declare handler-specific errors here (e.g., 404 for not-found, 409 for conflicts).

Explicit entries override auto-derived ones for the same status code.

transaction

Whether to wrap the handler in a database transaction.

When omitted, defaults are derived from the HTTP method: - GETfalse (read-only, no transaction) - All others (POST, PUT, DELETE, PATCH) → true

Set explicitly to override the default (e.g., false for a POST that manages its own transaction like signup).

type boolean

Depends on
#

Imported by
#