Mock Config logoMock config

rest

Complete API reference for the rest namespace in Mock Config.

The rest namespace provides methods for registering REST route handlers. Every method follows the same call signature:

rest.method(path, config, settings?)

rest.get

Intercepts GET requests matching the given path.

rest.get<Options>(path, config, settings?)

Prop

Type

rest.post

Intercepts POST requests. Identical signature to rest.get. The body entity is available in the config and resolver params.

rest.post<Options>(path, config, settings?)

rest.put

Intercepts PUT requests. body entity is available.

rest.put<Options>(path, config, settings?)

rest.patch

Intercepts PATCH requests. body entity is available.

rest.patch<Options>(path, config, settings?)

rest.delete

Intercepts DELETE requests. No body entity.

rest.delete<Options>(path, config, settings?)

rest.options

Intercepts OPTIONS requests. No body entity.

rest.options<Options>(path, config, settings?)

rest.sse

Intercepts GET requests and opens a persistent Server-Sent Events connection. Sets the required Content-Type: text/event-stream headers automatically.

rest.sse<Options>(path, config, settings?)

The resolver receives a client object instead of returning a response value:

Prop

Type

rest.sse('/notifications', ({ client }) => {
  client.send('Hello', { event: 'message' });
  client.close();
});

rest.stream

Intercepts POST requests and opens a persistent SSE connection. Same behavior as rest.sse but for POST — use this when the client sends a body (e.g. an AI prompt) alongside the stream request.

rest.stream<Options>(path, config, settings?)

RestConfig

The config argument accepts one of the following forms.

Inline value

Pass any value directly as the response body:

rest.get('/users', [{ id: 1, name: 'John' }]);
rest.get('/count', 42);

Matching A Response

Pass match in the route settings argument when a response should only apply to specific request conditions:

rest.get('/users', [{ id: 1, name: 'John' }], {
  match: { queries: { role: 'admin' } }
});

Prop

Type

Handler function

Pass a function to compute the response dynamically. The function receives resolver params.

rest.get('/users/:id', ({ request }) => ({
  id: Number(request.params.id),
  name: 'John'
}));

Matching A Handler

Use the same settings argument to match dynamic handlers:

rest.get('/users', ({ entities }) => [{ id: 1, role: entities.queries?.role }], {
  match: { queries: { role: 'admin' } }
});

Prop

Type

Generator function

Pass a generator function when each request should receive the next generated value. When the generator finishes, Mock Config starts it again.

rest.get('/status', function* () {
  yield { status: 'pending' };
  yield { status: 'processing' };
  return { status: 'done' };
});

File helper

Respond with a file from disk:

import { rest } from 'mock-config-server';

rest.get('/report', rest.file('./files/report.pdf'), {
  match: { headers: { accept: 'application/pdf' } }
});

Prop

Type

Polling helper

Cycle through a sequence of responses over time. See Polling for the full details.

import { rest } from 'mock-config-server';

rest.get(
  '/status',
  rest.polling([{ response: { status: 'pending' }, time: 3000 }, { response: { status: 'done' } }])
);

Prop

Type


RestSettings

The optional third argument applied to the route.

Prop

Type


Generic Options

All rest.* methods accept an optional generic Options to type the request and response:

rest.get<{
  params: { id: string };
  query: { locale: string };
  body: { name: string };    // POST/PUT/PATCH only
  response: User;
}>('/users/:id', ({ request, entities }) => { ... });

Prop

Type

On this page