Response helpers
Built-in helpers for JSON, HTML, streaming, and more.
Handlers can return a plain object and Zen Router will serialize it as JSON with
status 200 automatically. For other response types, use these helpers.
json
json(value, status?, headers?)
Returns a JSON response with the correct content type.
importfrom "@liveblocks/zenrouter";
zen.route
"POST /api/posts",
schema,
asyncauth, body=>
const post = awaitcreatePost
return json201
html
html(content, status?, headers?)
Returns an HTML response.
importfrom "@liveblocks/zenrouter";
zen.route
"GET /health",
=>
return html"<h1>OK</h1>"
abort
abort(status, headers?)
Throws an error response. See error handling for details.
importfrom "@liveblocks/zenrouter";
abort404// never returnstextStream
textStream(iterable, headers?, options?)
Returns a streaming text response from a string generator. Chunks are buffered to reduce per-chunk overhead (default buffer size: 64 KB).
importfrom "@liveblocks/zenrouter";
zen.route
"GET /api/stream",
=>
function* generate() {
yield "Hello ";
yield "world!";
return textStreamgenerate
ndjsonStream
ndjsonStream(iterable, headers?)
Returns a streaming NDJSON (Newline Delimited JSON) response. Each value is serialized as a single line.
importfrom "@liveblocks/zenrouter";
zen.route
"GET /api/events",
=>
function* events() {
yield"start"
yield"data", value: 42
yield"end"
return ndjsonStreamevents