# @xdoer/mock

> A simple state share tool for React App

Latest version **0.0.1** (published 2022-03-26) · MIT license · 0 weekly downloads

## Install

```sh
npm install @xdoer/mock
pnpm add @xdoer/mock
yarn add @xdoer/mock
bun add @xdoer/mock
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2022-03-26 |
| First published | 2022-03-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 4 |
| Unpacked size | 19.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | xdoer |
| Maintainers | thomas_han |

## Links

- npm: https://www.npmjs.com/package/@xdoer/mock
- npm.io page: https://npm.io/package/@xdoer/mock

## Dependencies (4)

- [express](https://npm.io/package/express.md) ^4.17.2
- [fs-extra](https://npm.io/package/fs-extra.md) ^10.0.0
- [@types/faker](https://npm.io/package/@types/faker.md) ^6.6.9
- [@faker-js/faker](https://npm.io/package/@faker-js/faker.md) ^6.0.0-alpha.3

## Recent versions

- 0.0.1 (latest) — 2022-03-26

## README

# Mock

Mock 工具

## 使用

### 配置

编写 mock.js 文件，进行配置

```ts
const mock = require('@xdoer/mock');

mock({
  port: 10086,
  method: 'post',
  mockDir: './mock',
});
```

### Mock 数据

新建 mock 文件夹，在文件夹下新建文件 user.js

```ts
module.export = faker => {
  const users = new Array(10).fill(null).map(() => ({
    id: faker.datatype.uuid(),
    name: faker.name.findName(),
    email: faker.internet.email(),
  }));

  return [
    {
      path: '/user/:id',
      data: [
        {
          // 场景值，http://localhost:10086/user/1?scene=success
          scene: 'success',
          response: req => {
            return {
              success: true,
              result: users.find(user => user.id === req.params.id),
            };
          },
        },
        {
          // 场景值，http://localhost:10086/user/1?scene=fail
          scene: 'fail',
          response: req => {
            return {
              success: false,
              result: {},
            };
          },
        },
      ],
    },
    {
      path: '/users',
      data: [
        {
          scene: 'success',
          response: {
            success: true,
            result: users,
          },
        },
      ],
    },
  ];
};
```

### 自动重启

mock 路由编辑完之后，需要重新启动 mock 服务器。需要引入 [ScriptRunner](https://github.com/xdoer/ScriptRunner) 管理和运行

```ts
import { Config } from '@xdoer/script-runner/lib/types';
import { MockArgs } from '@xdoer/mock';
import { ChokidarArgs } from '@xdoer/chokidar';
import runScript from '@xdoer/script-runner/lib/runScript';
const debounce = require('lodash/debounce');

const debounceExec = debounce(script => {
  runScript(config.scripts.find(v => v.module === script)!);
}, 1000);

const basePath = process.cwd();

const config: Config = {
  script: [
    {
      module: '@xdoer/chokidar',
      args: <ChokidarArgs>[
        {
          options: { persistent: true, ignoreInitial: true },
          list: [
            {
              target: basePath + '/mock/**.ts',
              watch: {
                change: () => {
                  debounceExec('@xdoer/chokidar');
                },
              },
            },
          ],
        },
      ],
    },
    {
      module: '@xdoer/mock',
      args: <MockArgs>[
        {
          port: 3000,
          method: 'get',
          mockDir: './mock',
        },
      ],
      // 开启子进程运行模式
      subProcess: true,
    },
  ],
};

export default config;
```

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