http/route_result.ts

What a route handler returns: a success, a failure, a raw answer described as data, or an SSE stream. A handler never builds a Response: apply_route_specs prepares the response from the result inside the route's transaction and builds it only after the commit, through the Hono context (prepare_route_response, emit_route_response in http/route_spec.ts). So every response a route spec answers carries what the middleware put on the context — the session middleware's clear of a stale cookie among it — and a Set-Cookie the handler returns rides only a success whose writes committed.

  • route_ok(value, {set_cookie?}) — value as JSON at 200, the Set-Cookie lines after any the middleware put on the context
  • route_fail(def, fields?) — the definition's body at its status, with the headers it derives (Retry-After); never a cookie
  • route_raw({status, headers?, body?}) — a status, headers, and a body written as given: an empty body, bytes, a stream, a JSON-RPC envelope
  • route_sse(stream) — the stream the route opened for this request (RouteContext.open_sse)

A result is plain data, so domain code and shared helpers fail without the Hono context (return route_fail(REST_ROW_NOT_FOUND)), and a handler — which receives a RouteContext, never the Hono context — forwards a failure it was handed (if (!result.ok) return result).

Hono-free: nothing here imports hono.

view source

Declarations
#

18 declarations

ReadonlyJson
#

http/route_result.ts view source

ReadonlyJson<T>

type unknown extends T ? T : T extends string | number | bigint | boolean | symbol | null | undefined ? T : T extends readonly (infer TItem)[] ? readonly ReadonlyJson<TItem>[] : { readonly [K in keyof T as string extends K ? unknown extends T[K] ? never : K : K]: ReadonlyJson<T[K]>; }

import type {ReadonlyJson} from '@fuzdev/fuz_app/http/route_result.js';

A JSON value's type made readonly at every depth: what a handler's route_ok value is checked against. route_ok infers its value as written ({ready: true}, not {ready: boolean}), arrays as readonly tuples, which a readonly array of the output's element type accepts. A tuple reads as an array of its elements. An index signature of unknown (a loose_object's other keys, a record(unknown)) is left out, so a value typed by an interface, which has no index signature, is accepted.

generics

ReadonlyJson<T>
T

RestErrorBodyInput
#

http/route_result.ts view source

RestErrorBodyInput<TDef>

type TDef extends RestErrorDef<number, infer TCode extends string, infer TFields extends SchemaEntries> ? { error: TCode; } & SchemaObjectInput<TFields> : never

import type {RestErrorBodyInput} from '@fuzdev/fuz_app/http/route_result.js';

The input side of a definition's body: what route_fail builds from it.

generics

RestErrorBodyInput<TDef extends RestErrorDef>
TDef
constraint RestErrorDef

route_fail
#

http/route_result.ts view source

<TDef extends RestErrorDef>(def: TDef, ...fields: RouteFailFields<TDef>): RouteFailOf<TDef> import {route_fail} from '@fuzdev/fuz_app/http/route_result.js';

A failure: the route answers {error: def.code, ...fields} at def.status, with the headers def derives from that body.

In a handler it must be one the route allows: a definition in its errors, one its auth or rate_limit derives, or a catch layer's answer (ALWAYS_ACCEPTED_ERRORS) — or any definition whose body one of those accepts at the same status and code.

def

the definition, a rest_error

type TDef

fields

the body's fields beside error: absent for a bare code, optional when every field is

type RouteFailFields<TDef>

returns

RouteFailOf<TDef>

generics

route_fail<TDef extends RestErrorDef>
TDef
constraint RestErrorDef

route_ok
#

http/route_result.ts view source

<const T extends {} | null>(value: T, options?: RouteOkOptions | undefined): RouteOk<T> import {route_ok} from '@fuzdev/fuz_app/http/route_result.js';

A success: the route answers value as JSON at 200, with the Set-Cookie lines of options.set_cookie after any the middleware wrote.

Typed in a handler by the input side of the route's output schema (ReadonlyJson). The value is inferred as written, its literals kept.

value

the response body, never undefined

type T

options?

the Set-Cookie lines the success carries

optional

returns

RouteOk<T>

generics

route_ok<T extends {} | null>
T
constraint {} | null

route_raw
#

http/route_result.ts view source

(init: RouteRawInit): RouteRaw import {route_raw} from '@fuzdev/fuz_app/http/route_result.js';

A raw answer at any status, described as data: an empty body, bytes, a stream, a JSON-RPC envelope. A JSON answer of the route's own goes through route_ok or route_fail.

The framework checks it when it prepares the response, inside the route's transaction, so a raw answer it refuses throws and rolls back:

  • a status outside 200 to 599
  • a header of ROUTE_RAW_REFUSED_HEADERS, a name or value a Headers refuses, an empty value or an empty list (a runtime writes one its own way), one name twice in any case
  • a non-null body without a Content-Type (a runtime would derive one, each its own way), and a body on a null-body status (204, 205, 304)
  • a Content-Length that is not a decimal integer without a leading zero (0 or [1-9][0-9]*), a list included, one past the safe integer range, one on a 204 or 205 (no content to measure, RFC 9110 §8.6 and §15.3.6; a 304 may name the size of the representation it stands for, §15.4.5), and one a text or bytes body doesn't have — the UTF-8 bytes of a string, the byteLength of a Uint8Array. @hono/node-server sends any of these as written while Bun rewrites them to the body it sends, so the two runtimes would answer differently. With a null or stream body the size is the route's to declare: an X-Accel-Redirect answer sends one with no body
  • a body that is none of RouteRawBody (a Response included), a stream that is locked, bytes whose buffer is detached, and bytes whose resizable buffer shrank below the view — read as empty, they would answer as zero bytes on every runtime

A stream that was read and released is unlocked yet can't be answered with, and nothing shows it before a Response is built from it: that fails after the commit, answering the error catch's 500 with an error naming the route (or, on @hono/node-server, whose own Response reads the stream only as it writes, an empty body). A view whose resizable buffer shrinks after the result is prepared is past what the prepare can see.

A stream body's cancel must release its source — close the file, end the child process: it is the one hook the spine calls for a stream it does not send. A stream body the route does not end up answering with — refused here, or rolled back by a failed commit — is cancelled, and so is one answering a HEAD, which gets the status and headers alone. A source opened before the status is known (a file opened to choose 200 or 404) is released the same way whichever answer it ends up behind.

init

the status, headers, and body

returns

RouteRaw

ROUTE_RAW_REFUSED_HEADERS
#

http/route_result.ts view source

readonly string[] import {ROUTE_RAW_REFUSED_HEADERS} from '@fuzdev/fuz_app/http/route_result.js';

The header names a raw answer may not carry, lowercase (compared case-insensitively): Set-Cookie, which only a success carries (RouteOkOptions.set_cookie), and the hop-by-hop names, which are the server's to write — framing, the connection, an upgrade.

route_sse
#

http/route_result.ts view source

(stream: SseStream<never>): RouteSse import {route_sse} from '@fuzdev/fuz_app/http/route_result.js';

An SSE answer: the stream RouteContext.open_sse opened for this request. The framework answers with that stream's response, and refuses (throws) a stream this request did not open.

stream

the stream, as open_sse returned it

type SseStream<never>

returns

RouteSse

RouteFail
#

http/route_result.ts view source

RouteFail<TStatus, TCode, TBody> import type {RouteFail} from '@fuzdev/fuz_app/http/route_result.js';

A failure, typed by what reaches the wire — its status, its code, and its body — so a handler may answer with any definition whose body one of the route's allowed definitions at that status and code accepts. def carries the definition's headers.

generics

RouteFail<TStatus extends number, TCode extends string, TBody>
TStatus
constraint number
TCode
constraint string
TBody

ok

type false

readonly

status

type TStatus

readonly

code

type TCode

readonly

body

type TBody

readonly

def

The definition the failure was made from, at the same status and code.

type RestErrorDef<TStatus, TCode, SchemaEntries>

readonly

RouteFailFields
#

http/route_result.ts view source

RouteFailFields<TDef>

type TDef extends RestErrorDef<number, string, infer TFields extends SchemaEntries> ? keyof TFields extends never ? [] : Record<never, never> extends SchemaObjectInput<TFields> ? [fields?: SchemaObjectInput<TFields> | undefined] : [fields: SchemaObjectInput<TFields>] : never

import type {RouteFailFields} from '@fuzdev/fuz_app/http/route_result.js';

The arguments route_fail takes after a definition: none for a bare code, optional fields when every field is optional, required fields when any is.

generics

RouteFailFields<TDef extends RestErrorDef>
TDef
constraint RestErrorDef

RouteFailOf
#

http/route_result.ts view source

RouteFailOf<TDef>

type TDef extends RestErrorDef<number, string, SchemaEntries> ? RouteFail<TDef["status"], TDef["code"], RestErrorBodyInput<TDef>> : never

import type {RouteFailOf} from '@fuzdev/fuz_app/http/route_result.js';

A definition's failure result. Distributes over a union of definitions.

generics

RouteFailOf<TDef extends RestErrorDef>
TDef
constraint RestErrorDef

RouteOk
#

http/route_result.ts view source

RouteOk<T> import type {RouteOk} from '@fuzdev/fuz_app/http/route_result.js';

A success: value is the response body, answered as JSON at 200, with set_cookie's lines ([] for none).

generics

RouteOk<T>
T

ok

type true

readonly

value

type T

readonly

set_cookie

type readonly string[]

readonly

RouteOkOptions
#

http/route_result.ts view source

RouteOkOptions import type {RouteOkOptions} from '@fuzdev/fuz_app/http/route_result.js';

Options for route_ok.

set_cookie?

Set-Cookie header values, written in order after every Set-Cookie line the middleware put on the context — the session middleware's clear of a stale cookie first. Formatted by the caller (sign_session_cookie, format_session_clear_cookie); the framework treats them as opaque lines.

type readonly string[]

readonly

RouteRaw
#

http/route_result.ts view source

RouteRaw import type {RouteRaw} from '@fuzdev/fuz_app/http/route_result.js';

A raw answer (route_raw): a status, headers, and a body, written as given.

ok

type "raw"

readonly

status

type number

readonly

headers

type Readonly<Record<string, string | readonly string[]>>

readonly

body

type RouteRawBody

readonly

RouteRawBody
#

http/route_result.ts view source

RouteRawBody

type string | Uint8Array<ArrayBufferLike> | ReadableStream<Uint8Array<ArrayBufferLike>> | null

import type {RouteRawBody} from '@fuzdev/fuz_app/http/route_result.js';

A raw answer's body, written as given: none, text, bytes, or a byte stream.

RouteRawHeaders
#

http/route_result.ts view source

Readonly<Record<string, string | readonly string[]>>

type Readonly<Record<string, string | readonly string[]>>

import type {RouteRawHeaders} from '@fuzdev/fuz_app/http/route_result.js';

A raw answer's headers by name; a name a response repeats takes a list.

[key: string]

type string | readonly string[]

readonly

RouteRawInit
#

http/route_result.ts view source

RouteRawInit import type {RouteRawInit} from '@fuzdev/fuz_app/http/route_result.js';

What route_raw takes.

status

The status, 200 to 599.

type number

readonly

headers?

The headers, none of ROUTE_RAW_REFUSED_HEADERS; a body needs its Content-Type.

type Readonly<Record<string, string | readonly string[]>>

readonly

body?

The body, null (the default) for none.

type RouteRawBody

readonly

RouteResult
#

RouteSse
#

http/route_result.ts view source

RouteSse import type {RouteSse} from '@fuzdev/fuz_app/http/route_result.js';

An SSE answer (route_sse): the stream RouteContext.open_sse opened for this request, whose response the framework answers with.

ok

type "sse"

readonly

stream

Typed to admit a stream of any event type — SseStream is contravariant in it.

type SseStream<never>

readonly

Depends on
#

Imported by
#