# fe-dev-server-plugin-mock

> Mock plugin for fe-dev-server

Latest version **0.2.2** (published 2026-06-25) · ISC license · 0 weekly downloads

## Install

```sh
npm install fe-dev-server-plugin-mock
pnpm add fe-dev-server-plugin-mock
yarn add fe-dev-server-plugin-mock
bun add fe-dev-server-plugin-mock
```

## Health

**Score 65/100 (B)** — status: active.

Positive: has types; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads; no esm support; pre 1.0.

## Facts

| | |
|---|---|
| Version | 0.2.2 |
| Published | 2026-06-25 |
| First published | 2022-12-11 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 8 |
| Unpacked size | 97.4 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| Author | zlcatwu |
| Maintainers | zlcatwu |
| Keywords | fe-dev-server, plugin, mock |

## Links

- npm: https://www.npmjs.com/package/fe-dev-server-plugin-mock
- npm.io page: https://npm.io/package/fe-dev-server-plugin-mock

## Dependencies (8)

- [ejs](https://npm.io/package/ejs.md) ~3.1.8
- [glob](https://npm.io/package/glob.md) ~8.0.3
- [slash](https://npm.io/package/slash.md) ~3.0.0
- [lodash](https://npm.io/package/lodash.md) ~4.17.21
- [pkg-dir](https://npm.io/package/pkg-dir.md) 5.0.0
- [chokidar](https://npm.io/package/chokidar.md) ~3.5.3
- [fs-extra](https://npm.io/package/fs-extra.md) ~10.1.0
- [fe-dev-server-core](https://npm.io/package/fe-dev-server-core.md) 0.2.0

## Alternatives

- [pagerjs](https://npm.io/package/pagerjs.md) — 60 weekly downloads
- [whistle.savefor-mock](https://npm.io/package/whistle.savefor-mock.md) — 4 weekly downloads
- [tinyspy](https://npm.io/package/tinyspy.md) — 0 weekly downloads
- [@wingtics/connector-mock](https://npm.io/package/@wingtics/connector-mock.md) — 0 weekly downloads
- [@site-spy/mcp-server](https://npm.io/package/@site-spy/mcp-server.md) — 0 weekly downloads

## Recent versions

- 0.2.2 (latest) — 2026-06-25
- 0.2.1 — 2026-06-24
- 0.2.0 — 2026-05-14
- 0.1.0 — 2026-05-14
- 0.0.25 — 2026-05-14
- 0.0.24 — 2026-05-14
- 0.0.23 — 2026-05-14
- 0.0.22 — 2025-05-23
- 0.0.21 — 2024-01-23
- 0.0.20 — 2024-01-23
- 0.0.19 — 2024-01-23
- 0.0.18 — 2024-01-19
- 0.0.17 — 2023-04-17
- 0.0.16 — 2023-04-17
- 0.0.15 — 2023-04-17
- … 14 more at https://npm.io/package/fe-dev-server-plugin-mock/versions

## README

# fe-dev-server-plugin-mock

> A plugin for JSON API mock

## Config

### dir

string - the location of mock files

### defaultMockFileCfg

object - default config of mock file return

defaultMockFileCfg.delay: number - response delay time in milliseconds

defaultMockFileCfg.delayOffset: number - the maximum delay time offset in milliseconds

defaultMockFileCfg.headers: object - the extra response headers

### mockFileKeyFormatter

function - change the mockFileKey for the request in order to use other mock file for response

```javascript
({
  // original: POST /api/users?_method=GET => /api/users/post.js
  // formatted: POST /api/users?_method=GET => /api/users/get.js
  mockFileKeyFormatter: ({ headers, pathname, method, search, originalMockKey }) => {
    if (search._method) {
      return {
        method: search._method,
        pathname,
      };
    }
    return originalMockKey;
  },
});
```

### forceEnable

boolean - when true, ignore the `enable` field in individual mock files and force all mocks to take effect. Useful for debugging or when you want to temporarily enable all mocks without modifying each file.

@default false

### How to use

Create mock file in the configed directory (filepath format like `${pathanme}/${method}.js`), mock plugin will reading these files and make response, for examples:

`api/login/post.js`

```javascript
// should be active
exports.enable = true;

exports.mock = ({ request, params, query, body, method, headers }) => ({
  // response data
  data: {},
  // the response will be delayed in 90~110 ms
  delay: 100,
  delayOffset: 10,
  // extra headers
  headers: {
    'custom-header': 'value',
  },
  // response status code
  statusCode: 200,
});
```

Also, dynamic params in route is supported, for examples, `api/users/{id}.js` will make response to `/api/users/1` or `/api/users/2`. And you will get params `{ id: 1 / 2 }` in mockFile's mock function.

Any request can not find specify mock file or mock file is inactive, it will be skipped to next plugin.

---
_Source: https://npm.io/package/fe-dev-server-plugin-mock · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
