Ir al contenido

Handler Functions

Esta página aún no está disponible en tu idioma.

Handler functions give you full programmatic control over a route response. You export functions from a handlers file; yRest loads it at startup and calls the matching function when the route is hit. Unlike scenarios, handlers can run any logic — counters, state, branching on the request, reading and writing your collections.

A handlers file is an ES module that exports one function per handler. It can be JavaScript (.js, .mjs, .cjs) or TypeScript (.ts, .mts):

yrest.handlers.ts
import { defineHandler } from "@yrest/cli";
export const login = defineHandler((req) => {
const { password } = (req.body ?? {}) as { password?: string };
if (password !== "secret") return { status: 401, body: { error: "Invalid credentials" } };
return { body: { token: "tok-123" } };
});

Pass it to the server with --handlers, or set it once in yrest.config.yml:

Terminal window
yrest serve db.yml --handlers yrest.handlers.ts
yrest.config.yml
handlers: yrest.handlers.ts

Every exported function is registered under its export name. Anything that is not a function is ignored.

Point a _routes entry at a handler with _handler:

_routes:
- _method: POST
_path: /login
_handler: login
- _method: GET
_path: /users/:id/card
_handler: userCard

Path params, query string, body and headers of the request are all available to the handler.

type HandlerRequest = {
params: Record<string, string>;
query: Record<string, string | string[]>;
body: unknown;
headers: Record<string, string | string[]>;
};
type HandlerResponse = {
status?: number; // defaults to 200
body?: unknown;
headers?: Record<string, string>;
};
type HandlerContext = {
collection<T>(name: string): CollectionHandle<T>;
};
type Handler = (
req: HandlerRequest,
ctx: HandlerContext
) => HandlerResponse | Promise<HandlerResponse>;

A handler returns a HandlerResponse — or a promise of one, so async functions work as well. A 204 without a body is sent empty.

defineHandler is an identity wrapper exported by @yrest/cli. It changes nothing at runtime; it gives the function its parameter and return types, so req, ctx and the returned object are checked by TypeScript:

import { defineHandler } from "@yrest/cli";
export const userCard = defineHandler((req) => ({
body: { id: req.params["id"], badge: "gold" },
}));

defineSseHandler does the same for SSE handler streams.

The second argument, ctx, gives the handler typed access to the live data through ctx.collection(name). It is the same handle as server.collection() in the programmatic API:

import { defineHandler } from "@yrest/cli";
import type { User } from "./db.types"; // generated with `yrest types`
export const login = defineHandler((req, { collection }) => {
const { email } = (req.body ?? {}) as { email?: string };
const user = collection<User>("users").findWhere((u) => u.email === email)[0];
return user
? { body: { token: `tok-${user.id}` } }
: { status: 401, body: { error: "Unknown user" } };
});
export const signup = defineHandler((req, { collection }) => {
const user = collection<User>("users").create(req.body as Omit<User, "id">);
return { status: 201, body: user };
});

Writes go through the same layer as the REST routes: the user that signup creates is returned by GET /users straight away, gets its id from the server’s id strategy and is persisted to db.yml like a POST.

MethodReturnsDescription
findAll()T[]Every item, in stored order
findById(id)T | undefinedItem by id — 1 and "1" both match
findWhere(fn)T[]Items for which the predicate returns true
count()numberNumber of items
create(data)TAdds an item; the id is generated when omitted
update(id, patch)T | undefinedMerges patch into the item (the id cannot change)
delete(id)T | undefinedRemoves the item and returns it

Returned items are copies — change data with update(), not by mutating a result. collection() throws for a collection that does not exist.

Use yrest types to generate the User, Post… interfaces from your db.yml.

An SSE route can hand the whole stream to a handler with _handler inside _sse, instead of a scripted _events list:

_routes:
- _method: SSE
_path: /events/chat
_sse:
_handler: chatStream
- _method: POST
_path: /chat/messages
_handler: postMessage
import { EventEmitter } from "node:events";
import { defineHandler, defineSseHandler } from "@yrest/cli";
const bus = new EventEmitter();
export const chatStream = defineSseHandler((req, raw) => {
const send = (msg: unknown) => raw.write(`event: message\ndata: ${JSON.stringify(msg)}\n\n`);
bus.on("message", send);
raw.on("close", () => bus.off("message", send));
});
export const postMessage = defineHandler((req, { collection }) => {
const message = collection("messages").create(req.body as { text: string });
bus.emit("message", message);
return { status: 201, body: message };
});

An SSE handler receives (req, raw, ctx), where raw is the Node.js ServerResponse. yRest sends the SSE headers and a keep-alive comment every 15 s; the handler writes frames in the event: / data: wire format and calls raw.end() when the stream is over. Listen to raw.on("close") to stop work when the client disconnects.

SituationResponse
_handler names a function the file does not export501 with Handler "name" is not defined in the handlers file
The handler throws or rejects500 with Handler "name" threw an error: …
An SSE handler is missing501 before the stream opens
An SSE handler throwsAn event: error frame, then the stream is closed
The handlers file fails to loadThe error is logged and the server starts without handlers

_error on the route takes priority over the handler: the route always answers with the forced status.

yrest handler adds a stub to the handlers file configured in yrest.config.yml (or yrest.handlers.js), creating the file if needed:

Terminal window
yrest handler login
yrest handler login --method POST --path /login
yrest handler login --method POST --path /login --register

For a .ts / .mts handlers file the stub uses defineHandler:

/** Handler for POST /login */
export const login = defineHandler(async (req, ctx) => {
// TODO: implement handler logic — ctx.collection("name") gives typed access to the data
return { status: 200, body: null };
});

--register also writes the _routes entry (_method, _path, _handler) to db.yml.

TypeScript handler files are imported directly, without a build step:

  • Node ≥ 22.18 strips the types itself. Use import type for type-only imports, and only erasable syntax (no enum or parameter properties).
  • Older Node versions — load tsx into Node: NODE_OPTIONS="--import tsx" npx @yrest/cli serve db.yml --handlers yrest.handlers.ts.
  • Or compile the file to .js and point --handlers at the output.

In JavaScript, ctx is available too; JSDoc gives editor types without a compiler:

yrest.handlers.js
/** @type {import('@yrest/cli').Handler} */
export const login = async (req, ctx) => {
const user = ctx.collection("users").findWhere((u) => u.email === req.body?.email)[0];
return user ? { body: { token: `tok-${user.id}` } } : { status: 401 };
};
  • SSE Streams — scripted event streams and handler-driven ones
  • Codegen — generate the types of your collections with yrest types
  • Programmatic API — the same collection access from your tests
  • Scenarios — conditional responses without code