Skip to content

Config reference

MockrConfig<E> — what mockr<E>(config) accepts.

ts
interface MockrConfig<E = Record<string, unknown>> {
  port?:        number;            // default: random free port
  endpoints:    EndpointDef<E>[];
  proxy?:       ProxyConfig;
  middleware?:  Middleware[];
  scenarios?:   Record<string, ScenarioSetup<E>>;
  recorder?:    RecorderOptions;
  tui?:         boolean;
  cors?:        boolean | CorsOptions; // permissive cors helper
}
FieldDescription
portListen port. Defaults to a random free one — read server.port after await.
endpointsEndpoint definitions — see Endpoints reference.
proxyPass-through target for unmatched routes. See Proxy.
middlewarePre-routing pipeline. See Middleware.
scenariosNamed server states. See Scenarios.
recorderEnables the recorder + Chrome extension hand-off. See Recorder.
tuiAuto-launch the terminal UI on start.
corsConvenience flag — set true for permissive CORS, or pass an options object.

Async return

mockr<E>(config) returns a Promise<MockrServer>. Always await — the port is bound before the promise resolves.

ts
const server = await mockr<Endpoints>({ /* ... */ });
console.log('listening on', server.port);

endpoints<E>([...]) helper

Per-group runtime no-op that type-checks endpoint definitions in a separate file:

ts
// src/mocks/cart.ts
import { endpoints } from '@yoyo-org/mockr';
import type { Endpoints } from '../types.js';

export const cartMocks = endpoints<Endpoints>([
  { url: '/api/cart', data: [] },
]);

Top-level mockr<E>(...) keeps the explicit generic — groups compose into it.

file<T>(path)

Brands a JSON path with a type so dataFile flows the type through:

ts
import { file } from '@yoyo-org/mockr';
import type { Todo } from './types.js';

{ url: '/api/todos', dataFile: file<Todo[]>('./todos.json') }
//        ListHandle<Todo> on the cross-endpoint side

Runtime value is the path string. The watcher reloads on fs.watch events (debounced 100ms).

MIT License