Programmatic API
Questi contenuti non sono ancora disponibili nella tua lingua.
The programmatic API lets you start and stop a yRest server from inside your test suite or any Node.js script — no CLI, no separate process. Import createYrestServer, pass a config object, and use the running server URL in your tests.
import { createYrestServer } from "@yrest/cli";
const server = createYrestServer({ file: "./tests/db.yml", port: 3070 });await server.start();// run tests against http://localhost:3070await server.stop();Installation
Section titled “Installation”createYrestServer(options)
Section titled “createYrestServer(options)”Options reference
Section titled “Options reference”| Option | Type | Default | Description |
|---|---|---|---|
file | string | — | Path to the YAML database file |
port | number | 3070 | Port to listen on |
host | string | "localhost" | Host to bind |
base | string | "" | URL prefix for all routes |
readonly | boolean | false | Reject all write operations |
delay | number | 0 | Simulated latency in ms |
snapshot | boolean | false | Enable snapshot/reset endpoints |
Return value — YrestServer
Section titled “Return value — YrestServer”Lifecycle methods
Section titled “Lifecycle methods”server.start()
Section titled “server.start()”server.stop()
Section titled “server.stop()”Typed collection access
Section titled “Typed collection access”server.collection<T>(name) returns a typed CRUD handle for the named collection:
import type { User } from "./db.types"; // from yrest types codegen
const users = server.collection<User>("users");users.findAll(); // User[]users.findById(1); // User | undefinedusers.create({ name: "Ana" }); // Userusers.update(1, { name: "Ana M." });users.delete(1);Vitest example
Section titled “Vitest example”Playwright example
Section titled “Playwright example”Cypress example
Section titled “Cypress example”Next steps
Section titled “Next steps”- Configuration — same options available in
yrest.config.yml - Quick Start — run yRest from the CLI
- CLI Reference —
yrest serveflag reference