portfolio / work / yrest.mdoc
yRest
A mock API. One YAML file.
A YAML-powered alternative to json-server. Point it at a db.yml file and get a full REST API mock with CRUD, relations, filters and snapshots, so frontend work doesn't wait for the backend.
At a glance
- Problem
- Frontend work blocks on APIs that don't exist yet; I needed relations, scenarios and custom routes in a mock that is easy to edit.
- My part
- Designed and built it alone: product scope, CLI, server, the db.yml format, tests, docs and releases.
- What I delivered
- Published on npm as @yrest/cli, released from version tags with npm provenance.
- A documentation site with an interactive playground.
- Unit, end-to-end and security tests in CI on Node 20 and 22.

Problem
Frontend work often blocks on an API that doesn't exist yet. For my cases I needed more than CRUD over a JSON file: relations, conditional responses, slow or failing endpoints and custom routes, in a format I could edit as easily as a config file. yRest was born to bring those needs together around one YAML file.
The idea
One db.yml file is the whole API:
users:
- id: 1
name: Ana
email: ana@test.com
posts:
- id: 1
title: First post
userId: 1npx @yrest/cli serve db.ymlEvery top-level collection becomes a full CRUD resource (GET, POST, PUT, PATCH, DELETE) on http://localhost:3070.
Solution
The project grew in phases, each one closing a gap I hit while using it in real frontends:
- Querying: field operators (
_gte,_like,_regex…), full-text search, sorting, pagination with an optional envelope response and field projection. - Relations: many-to-one, one-to-one and many-to-many, nested routes and automatic embedding, with a compact syntax for declaring them.
- Custom routes: static responses, template variables, conditional scenarios, per-route delay and error injection for the endpoints that aren't plain CRUD.
- Logic when it's needed: handler functions in JavaScript or TypeScript, plus Server-Sent Events streams.
- Server modes: watch, readonly, delay and snapshot.
- Interoperability: OpenAPI 3.0 export and TypeScript type generation from
db.yml. - Tests as a first-class use case: a programmatic API (
createYrestServer) to start a mock inside Vitest or Playwright on a random port, with typed access to the data.
Technical decisions
Fastify over NestJS
- Problem
- The tool runs through npx: every start has to feel instant.
- Decision
- A lightweight stack on Fastify, Commander, yaml and Zod instead of a framework like NestJS.
- Alternatives
- NestJS, with modules, dependency injection and decorators.
- Trade-off
- Less built-in structure, so module boundaries (cli, storage, server, router, config) are kept by convention.
- Outlook
- NestJS stays an option if yRest grows into a web panel or a plugin system.
Atomic writes
- Problem
- Writes go straight to the user's db.yml; an interrupted process could leave it corrupted.
- Decision
- Atomic writes: write to a temporary file, then rename it over the original.
- Alternatives
- Writing in place, or keeping changes only in memory.
- Trade-off
- One extra file operation per write, which is negligible for a development tool.
- Evidence
- POST, PUT, PATCH and DELETE persist to db.yml without partial files.
Custom routes before the proxy
- Problem
- Some endpoints in a real frontend aren't CRUD, and some should hit a real API.
- Decision
- Ship custom routes first and defer the proxy fallback.
- Alternatives
- A hybrid proxy that forwards unknown routes to a real backend.
- Trade-off
- Mixing mocked and real routes is not possible yet.
Quality
The suite runs with Vitest on Node 20 and 22 in CI: unit, end-to-end and dedicated security tests for XSS, ReDoS and handler functions. Releases are published from a version tag with npm provenance.
What's next
The roadmap includes a visual panel, a Docker image, request validation with JSON Schema, WebSocket mock channels and YAML snippets for the editor. Editor support itself already shipped as a separate project, yRest for VS Code.
Gallery
Stack
| Layer | Technology | Why |
|---|---|---|
| Runtime | TypeScript | Typed core, public API and generated types for consumers (yrest types). |
| API | Fastify | Fast startup and low overhead for a CLI launched with npx; good TypeScript support. |
| Tooling | Commander | The init, serve, openapi and types commands. |
| Data | yaml + Zod | Read and write db.yml; validate config and options before the server starts. |
| Quality | Vitest | Unit, end-to-end and security tests, run in CI on Node 20 and 22. |
| Tooling | tsup | Builds the npm package (ESM + CJS + type declarations). |
| Interface | Astro Starlight | Documentation site with search, translations and the interactive playground. |




