Mock config server
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 log
  • rewrite (optional): custom output formatter

By default, timestamp and method tokens are prettified. Timestamp transforms from UNIX-timestamp number to DD.MM.YYYY, HH:mm:ss,sss string. Method transforms from lower case to upper case. If rewrite function 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 only true fields are logged.
  • If all fields are false, blacklist mode applies and everything except false fields is logged.
  • Whitelist has priority if both true and false are 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
                    }
                  }
                });
              }
            }
          }
        ]
      }
    ]
  }
];

On this page