Intercepting requests
Learn how REST request interception works in Mock Config.
To inspect and handle requests, define REST routes with the rest namespace. Here is a minimal example:
import { rest } from 'mock-config-server';
const route = rest.get('/resource', ({ request }) => ({
method: request.method,
path: request.path
}));This route intercepts GET /resource requests and gives you access to the incoming request data. This page introduces the structure of REST routes and the supported ways of intercepting requests.
Anatomy Of A REST Route
Every REST route consists of a path selector and a response config. The config can be a static value, a resolver function, a generator function, rest.file(), or rest.polling(). Pass matchers and response settings in the optional third argument.
// path selector settings
// v v
rest.get('/resource', { ok: true }, { status: 200 });
// ^ response config
//Route Path
You can provide a string path as the route predicate:
rest.get('/users/:id', () => {});This matches requests by method and pathname. Query parameters are not part of the route path matching.
Query Parameters
Do not include query parameters in the route path, even if you are certain that such a route is exactly what you want to cover in your scenario.
rest.get('/users?id=1', () => {});Query parameters describe additional request data, not the route path itself. Use request.queries in the resolver or queries in a matcher instead.
Special Tokens
When using a string path, you can use special tokens to represent dynamic matching behavior:
:idfor a named path parameter*for a wildcard segment
rest.get('/users/:id', () => {});
rest.get('/users/*', () => {});Regular Expression
You can also provide a regular expression as the path predicate:
rest.get(/\/settings\/(profile|billing)/, () => {});Regular expressions are useful for advanced matching cases, but string paths are usually easier to read and maintain.
Advanced Matching
For more nuanced use cases, use a Matcher to add extra conditions for params, queries, cookies, headers, or body.
For example, exists() is useful when a request should match as long as a value is present:
import { exists, rest } from 'mock-config-server';
const route = rest.get('/products', [{ id: 1, name: 'John' }], {
match: {
queries: {
search: exists()
}
}
});Use fn() when you need a custom comparator function:
import { fn, rest } from 'mock-config-server';
const route = rest.get('/products', [{ id: 1, name: 'John' }], {
match: {
queries: {
search: fn((actual) => typeof actual === 'string' && actual.startsWith('jo'))
}
}
});This keeps the route path simple while still letting you match requests by custom logic. More ready-made functions can be found in the Matcher article.
Resolver
The resolver decides what to return when a request matches the route.
Static Response
Pass a static value when the result is fixed:
import { rest } from 'mock-config-server';
const route = rest.get('/users', [{ id: 1, name: 'John' }]);Dynamic Response
Use a function when the response depends on request data:
import { rest } from 'mock-config-server';
const route = rest.get<{ params: { id: string } }>('/users/:id', ({ request }) => {
return {
id: Number(request.params.id),
name: 'John'
};
});Inside the resolver, you can read request.params, request.queries, request.cookies, request.body, headers, and other request metadata.
Next Steps
Now that you know how REST request interception works, continue with the nested guides in this section: