Mocking requests
Logger
Request and response logging
In mock-config-server you can log requests and responses using the log function inside interceptors. This helps you see what the client sends and what the server returns.
Quick Example
import type { MockServerConfig } from 'mock-config-server';
export const mockServerConfig: MockServerConfig = [
{
configs: [
{
path: '/posts',
method: 'get',
routes: [
{
interceptors: {
request: ({ log }) => {
log({
options: {
id: true,
type: true,
timestamp: true,
method: true,
url: true
}
});
},
response: (data, { log }) => {
log({
options: {
type: true,
statusCode: true,
method: true,
url: true
},
rewrite: ({ type, statusCode, method, url }) => {
console.info(`${type} ${method}: ${url} => ${statusCode}`);
}
});
return data;
}
}
}
]
}
]
}
];How It Works
log accepts following parameters:
options: which tokens to logrewrite(optional): custom output formatter
By default,
timestampandmethodtokens are prettified. Timestamp transforms from UNIX-timestamp number toDD.MM.YYYY, HH:mm:ss,sssstring. Method transforms from lower case to upper case. Ifrewritefunction is used, those tokens will remain unformatted. You can format them as you need.
Logger Tokens
Prop
Type
Whitelist And Blacklist
For mapped entities (headers, cookies, query, params), you can pass Record<string, boolean> instead of true/false to control which fields are logged.
- If at least one field is
true, whitelist mode applies and onlytruefields are logged. - If all fields are
false, blacklist mode applies and everything exceptfalsefields is logged. - Whitelist has priority if both
trueandfalseare present.
import type { MockServerConfig } from 'mock-config-server';
export const mockServerConfig: MockServerConfig = [
{
configs: [
{
path: '/posts',
method: 'get',
routes: [
{
interceptors: {
request: ({ log }) => {
log({
// whitelist: only query1 and query2 will be logged
options: {
query: {
query1: true,
query2: true
}
}
});
log({
// whitelist: only cookie1 and cookie2 will be logged
options: {
cookies: {
cookie1: true,
cookie2: true,
cookie3: false
}
}
});
log({
// blacklist: all headers will be logged except header1
options: {
headers: {
header1: false
}
}
});
}
}
}
]
}
]
}
];