# vite-plugin-best-mock

> A mock Plugin for vite

Latest version **0.0.6** (published 2022-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install vite-plugin-best-mock
pnpm add vite-plugin-best-mock
yarn add vite-plugin-best-mock
bun add vite-plugin-best-mock
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.6 |
| Published | 2022-11-04 |
| First published | 2022-10-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 6 |
| Unpacked size | 6.2 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 2 |
| Author | qingyang |
| Maintainers | minjs1cn |
| Keywords | vite, plugin, typescript, mock, middleware, connect, multiparty |

## Links

- npm: https://www.npmjs.com/package/vite-plugin-best-mock
- Repository: https://github.com/minjs1cn/vite-plugin-best-mock
- Homepage: https://github.com/minjs1cn/vite-plugin-best-mock/tree/main/#readme
- Issues: https://github.com/minjs1cn/vite-plugin-best-mock/issues
- npm.io page: https://npm.io/package/vite-plugin-best-mock

## Dependencies (6)

- [qs](https://npm.io/package/qs.md) ^6.11.0
- [http](https://npm.io/package/http.md) ^0.0.1-security
- [chokidar](https://npm.io/package/chokidar.md) 3.5.2
- [multiparty](https://npm.io/package/multiparty.md) ^4.2.3
- [playground](https://npm.io/package/playground.md) ^0.7.0
- [connect-multiparty](https://npm.io/package/connect-multiparty.md) ^2.2.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.0.6 (latest) — 2022-11-04
- 0.0.5 — 2022-10-30
- 0.0.4 — 2022-10-30
- 0.0.3 — 2022-10-20
- 0.0.1-alpha-2 — 2022-10-19
- 0.0.1-alpha-1 — 2022-10-17

## README

# vite-plugin-best-mock

**English** | [中文](./README.zh_CN.md)

[![npm][npm-img]][npm-url] [![node][node-img]][node-url]

Provide local and prod mocks for vite.

A mock plugin for vite, and support the local environment and production environment at the same time. Connect service middleware is used locally, runtime not yet implemented.

### Install (yarn or npm)

**node version:** >=12.0.0

**vite version:** >=2.0.0

```bash
yarn add vite-plugin-best-mock -D
# or
npm i vite-plugin-best-mock -D
```

### Example

**Run Example**

```bash
# ts example
cd playground

yarn install

yarn dev
```

## Usage

**Development environment**

The development environment is implemented using Connect middleware。

Different from the production environment, you can view the network request record in the Google Chrome console

- Config plugin in vite.config.ts

```ts
import { UserConfigExport, ConfigEnv } from 'vite'

import { mockPlugin } from 'vite-plugin-mock'

export default ({ command }: ConfigEnv): UserConfigExport => {
  return {
    plugins: [
      vue(),
      mockPlugin(),
    ],
  }
}
```

- Options

```ts
interface MockPluginConfig {
  dir?: string;
  prefix?: string;
  timeout?: [number, number];
  multiparty: MultipartyConfig;
}
```

## Options

### dir

**type:** `string`

**default:** `'mock'`

Set the folder where the mock .ts file is stored

### prefix

**type:** `string`

**default:** `'/api/'`

the url start with prefix will be mocked

### localEnabled

**type:** `boolean`

**default:** `command === 'serve'`

Set whether to enable the local mock .ts file, do not open it in the production environment

### prodEnabled

**type:** `boolean`

**default:** `command !=='serve'`

Set whether to enable mock function for packaging

### multiparty

**type:** `MultipartyConfig`

**default:** `{}`

If you pass multiparty to mockPlugin(), they are passed directly into [multiparty](https://www.npmjs.com/package/multiparty).

## Mock file example

`/path/mock`

```ts
// user.ts

import { MockMethod } from 'vite-plugin-best-mock'

/**
 * url: /user
 * method: get
 */
export const get: MockMethod = (req) => {
	const { id } = req.query || {};
	return {
		id,
		name: 'qingyang',
	};
};

/**
 * url: /user
 * method: post
 */
export const post: MockMethod = (req) => {
	const { id } = req.body || {};
	return {
		id,
		name: 'qingyang',
	};
};
```

### MockMethod

```ts
export interface MockMethod {
  (req: Req, res: Res): any;
  timeout?: Pick<MockConfig, "timeout">["timeout"];
}
```

you can refer to [Next.js](https://nextjs.org/docs/api-routes/dynamic-api-routes) Dynamic api routes.


## License

MIT

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