vite-plugin-mock-dev-server 0.3.0 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,28 +1,43 @@
1
1
  # vite-plugin-mock-dev-server
2
2
 
3
- vite mock开发服务(mock-dev-server)插件。
4
-
5
- vite 开发环境中,注入一个 mock-dev-server
6
-
7
- ## 特性
8
-
9
- - ⚡️ 轻量,灵活,快速
10
- - 🧲 非注入式,对客户端代码无侵入
11
- - 💡 ESModule
3
+ <br>
4
+ <br>
5
+ <p align="center">Vite Plugin for API mock dev server.</p>
6
+
7
+ <br>
8
+ <p align="center">
9
+ <a href="https://www.npmjs.com/package/vite-plugin-mock-dev-server"><img alt="npm" src="https://img.shields.io/npm/v/vite-plugin-mock-dev-server?style=flat-square"></a>
10
+ <img alt="node-current" src="https://img.shields.io/node/v/vite-plugin-mock-dev-server?style=flat-square">
11
+ <img alt="GitHub package.json dependency version (prod)" src="https://img.shields.io/github/package-json/dependency-version/pengzhanbo/vite-plugin-mock-dev-server/vite?style=flat-square">
12
+ <img alt="NPM" src="https://img.shields.io/npm/l/vite-plugin-mock-dev-server?style=flat-square">
13
+ </p>
14
+ <br>
15
+ <p align="center">
16
+ <span>English</span> | <a href="/README.zh-CN.md">简体中文</a>
17
+ </p>
18
+ <br>
19
+ <br>
20
+
21
+
22
+ ## Feature
23
+
24
+ - ⚡️ light weight,flexible,fast
25
+ - 🧲 Non - injection, no intrusion to client code
26
+ - 💡 ESModule/commonjs
12
27
  - 🦾 Typescript
13
- - 🏷 支持 json / json5 编写 mock 数据
14
- - 📦 自动加载 mock 文件
15
- - 🎨 可选择你喜欢的任意用于生成mock数据库,如 `mockjs`,或者不是用其他库
16
- - 📥 路径规则匹配,请求参数匹配
17
- - ⚙️ 随意开启或关闭对某个接口的 mock配置
18
- - 🔥 热更新
19
- - ⚖️ 使用 `server.proxy` 配置
20
- - 🍕 支持在 mock文件中使用 `viteConfig.define`配置字段
28
+ - 🏷 Support json / json5
29
+ - 📦 Auto import mock file
30
+ - 🎨 Support any lib,like `mockjs`,or not use it.
31
+ - 📥 Path rules match and request parameters match
32
+ - ⚙️ Support Enabled/Disabled any one of api mock
33
+ - 🔥 HMR
34
+ - ⚖️ Use `server.proxy`
35
+ - 🍕 Support `viteConfig.define` in mock file
21
36
 
22
37
 
23
- ## 使用
38
+ ## Usage
24
39
 
25
- ### 安装
40
+ ### Install
26
41
 
27
42
  ```sh
28
43
  # npm
@@ -33,7 +48,7 @@ yarn add vite-plugin-mock-dev-server
33
48
  pnpm add -D vite-plugin-mock-dev-server
34
49
  ```
35
50
 
36
- ### 配置
51
+ ### Configuration
37
52
 
38
53
  `vite.config.ts`
39
54
  ```ts
@@ -44,7 +59,6 @@ export default defineConfig({
44
59
  plugins: [
45
60
  mockDevServerPlugin(),
46
61
  ],
47
- // 这里定义的字段,在mock中也能使用
48
62
  define: {},
49
63
  server: {
50
64
  proxy: {
@@ -55,15 +69,15 @@ export default defineConfig({
55
69
  }
56
70
  })
57
71
  ```
58
- 插件会读取 `server.proxy` 的配置, 仅对设置了代理的 url 匹配,启用mock 匹配。
72
+ The plugin reads the configuration for `server.proxy` and enables mock matching only for urls where the proxy is set.
59
73
 
60
- 插件也会读取 `define` 配置, 支持在 mock 文件中直接使用。
74
+ The plugin also reads the `define` configuration and supports direct use in mock files.
61
75
 
62
- > 因为一般场景下,我们只需要对有代理的url进行mock,这样才能通过 vite 提供的 http 服务进行 代理和 mock
76
+ > In a general case, we only need to mock the url with the proxy so that we can proxy and mock through the http service provided by vite
63
77
 
64
- ### 编写mock文件
78
+ ### Edit Mock File
65
79
 
66
- 默认配置,在你的项目根目录的 `mock` 目录中编写mock数据:
80
+ By default, write mock data in the `mock` directory of your project root:
67
81
 
68
82
  `mock/api.mock.ts` :
69
83
  ```ts
@@ -78,7 +92,7 @@ export default defineMock({
78
92
  })
79
93
  ```
80
94
 
81
- ## 方法
95
+ ## Methods
82
96
 
83
97
  ### mockDevServerPlugin(options)
84
98
 
@@ -101,20 +115,31 @@ export default defineConfig({
101
115
 
102
116
  - `option.include`
103
117
 
104
- 配置读取 mock文件,可以是一个 目录,glob,或者一个数组
118
+ Configure to read mock files, which can be a directory, glob, or array
105
119
 
106
- 默认值: `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (相对于根目录)
120
+ Default: `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (relative for `process.cwd()`)
107
121
 
108
122
  - `options.exclude`
109
123
 
110
- 配置读取 mock文件时,需要排除的文件, 可以是一个 目录、glob、或者一个数组
111
-
112
- 默认值:`['**/node_modules/**','**/test/**','**/cypress/**','src/**','**/.vscode/**','**/.git/**','**/dist/**',]`
124
+ When you configure the mock files to be read, the files you want to exclude can be a directory, a glob, or an array
125
+
126
+ Default:
127
+ ```ts
128
+ [
129
+ '**/node_modules/**',
130
+ '**/test/**',
131
+ '**/cypress/**',
132
+ 'src/**',
133
+ '**/.vscode/**',
134
+ '**/.git/**',
135
+ '**/dist/**'
136
+ ]
137
+ ```
113
138
 
114
139
 
115
140
  ### defineMock(config)
116
141
 
117
- mock 配置帮助函数,提供类型检查帮助
142
+ Mock Type Helper
118
143
 
119
144
  ```ts
120
145
  import { defineMock } from 'vite-plugin-mock-dev-server'
@@ -125,16 +150,16 @@ export default defineMock({
125
150
  })
126
151
  ```
127
152
 
128
- ## Mock 配置
153
+ ## Mock Configuration
129
154
 
130
155
  ```ts
131
156
  export default defineMock({
132
157
  /**
133
- * 请求地址,支持 `/api/user/:id` 格式
158
+ * Address of request,support `/api/user/:id`
134
159
  */
135
160
  url: '/api/test',
136
161
  /**
137
- * 接口支持的请求方法
162
+ * The request method supported by the API
138
163
  *
139
164
  * @type string | string[]
140
165
  * @default ['POST','GET']
@@ -142,45 +167,52 @@ export default defineMock({
142
167
  */
143
168
  method: ['GET', 'POST'],
144
169
  /**
145
- * 是否启用当前 mock请求
170
+ * enable/disable the current mock request
146
171
  *
147
- * 在实际场景中,我们一般只需要某几个mock接口生效,
148
- * 而不是所以mock接口都启用。
149
- * 对当前不需要mock的接口,可设置为 false
172
+ * we typically only need a few mock interfaces to work.
173
+ * set `false` to disable current mock
150
174
  *
151
175
  * @default true
152
176
  */
153
177
  enable: true,
154
178
  /**
155
- * 设置接口响应延迟, 单位:ms
179
+ * response delay, unit:ms
156
180
  *
157
181
  * @default 0
158
182
  */
159
183
  delay: 1000,
160
184
  /**
161
- * 响应状态码
185
+ * response status
162
186
  *
163
187
  * @default 200
164
188
  */
165
189
  status: 200,
166
190
  /**
167
- * 响应状态文本
191
+ * response status text
168
192
  */
169
193
  statusText: 'OK',
170
194
  /**
171
- * 请求验证器,通过验证器则返回 mock数据,否则不是用当前mock。
172
- * 这对于一些场景中,某个接口需要通过不同的入参来返回不同的数据,
173
- * 验证器可以很好的解决这一类问题,将同个 url 分为多个 mock配置,
174
- * 根据 验证器来判断哪个mock配置生效。
195
+ * Request a validator, through which the mock data
196
+ * is returned, otherwise not the current mock.
197
+ * In some scenarios where an interface needs to
198
+ * return different data through different inputs,
199
+ * the validator can solve this kind of problem well.
200
+ * It divides the same url into multiple mock
201
+ * configurations and determines which mock configuration
202
+ * is valid according to the validator.
175
203
  *
176
204
  * @type { header?: object; body?: object; query?: object; params?: object }
177
205
  *
178
- * 如果 validator 传入的是一个对象,那么验证方式是严格比较 请求的接口
179
- * 中,headers/body/query/params 的各个`key`的`value`是否全等,
180
- * 全等则校验通过
206
+ * If the validator incoming is an object,
207
+ * then the validation method is the comparison of the
208
+ * strict request of interface, headers/body/query/params
209
+ * each `key-value` congruent, congruent check through
181
210
  *
182
211
  * @type ({ header: object; body: object; query: object; params: object }) => boolean
183
- * 如果 validator 传入的是一个函数,那么会讲 请求的接口相关数据作为入参,提供给使用者进行自定义校验,并返回一个 boolean
212
+ * If the validator is passed a function,
213
+ * it takes the requested interface-related data as an input,
214
+ * gives it to the consumer for custom validation,
215
+ * and returns a boolean
184
216
  *
185
217
  */
186
218
  validator: {
@@ -191,41 +223,31 @@ export default defineMock({
191
223
  },
192
224
  /**
193
225
  *
194
- * 响应状态 headers
226
+ * response headers
195
227
  *
196
228
  * @type Record<string, any>
197
229
  *
198
230
  * @type (({ query, body, params, headers }) => Record<string, any>)
199
- * 入参部分为 请求相关信息
200
231
  */
201
232
  headers: {
202
233
  'Content-Type': 'application/json'
203
234
  },
204
235
 
205
236
  /**
206
- * 响应体数据
207
- * 定义返回的响应体数据内容。
208
- * 在这里,你可以直接返回JavaScript支持的数据类型如 `string/number/array/object`
209
- * 同时,你也可以使用如 `mockjs` 等库来生成数据内容
237
+ * Response Body
238
+ * Support `string/number/array/object`
239
+ * You can also use libraries such as' mockjs' to generate data content
210
240
  *
211
241
  * @type string | number | array | object
212
- * 直接返回定义的数据
213
242
  *
214
243
  * @type (request: { header, query, body, params }) => any | Promise<any>
215
- * 如果传入一个函数,那么可以更加灵活的定义返回响应体数据
216
244
  */
217
245
  body: {},
218
246
 
219
247
  /**
220
- * 如果通过 body 配置不能解决mock需求,
221
- * 那么可以通过 配置 response,暴露http server 的接口,
222
- * 实现完全可控的自定义配置
223
- *
224
- * 在 req参数中,已内置了 query、body、params 的解析,
225
- * 你可以直接使用它们
226
- *
227
- * 别忘了,需要通过 `res.end()` 返回响应体数据,
228
- * 或者需要跳过mock,那么别忘了调用 `next()`
248
+ * If the mock requirement cannot be addressed with the body configuration,
249
+ * Then you can expose the http server interface by configuring response,
250
+ * Achieve fully controlled custom configuration.
229
251
  */
230
252
  response(req, res, next) {
231
253
  res.end()
@@ -234,24 +256,26 @@ export default defineMock({
234
256
 
235
257
  ```
236
258
 
237
- > 注意:
259
+ > Tips:
238
260
  >
239
- > 如果使用 json/json5 编写 mock文件,则不支持使用 `response` 方法,以及不支持使用其他字段的函数形式。
261
+ > If you write mock files using json/json5,
262
+ > the 'response' method is not supported,
263
+ > as is the function form that uses other fields.
240
264
 
241
265
  `mock/**/*.mock.{ts,js,mjs,cjs,json,json5}`
242
266
 
243
- 查看更多示例: [example](/example/)
267
+ See more examples: [example](/example/)
244
268
 
245
- #### 示例1:
246
- 命中 `/api/test` 请求,并返回一个 数据为空的响应体内容
269
+ #### Example 1:
270
+ Match `/api/test`,And returns a response body content with empty data
247
271
  ```ts
248
272
  export default defineMock({
249
273
  url: '/api/test',
250
274
  })
251
275
  ```
252
276
 
253
- #### 示例2:
254
- 命中 `/api/test` 请求,并返回一个固定内容数据
277
+ #### Example 2:
278
+ Match `/api/test` ,And returns a fixed content data
255
279
  ```ts
256
280
  export default defineMock({
257
281
  url: '/api/test',
@@ -261,8 +285,8 @@ export default defineMock({
261
285
  })
262
286
  ```
263
287
 
264
- #### 示例3:
265
- 限定只允许 `GET` 请求
288
+ #### Example 3:
289
+ Only Support `GET` Method
266
290
  ```ts
267
291
  export default defineMock({
268
292
  url: '/api/test',
@@ -270,8 +294,8 @@ export default defineMock({
270
294
  })
271
295
  ```
272
296
 
273
- #### 示例4:
274
- 在返回的响应头中,添加自定义header
297
+ #### Example 4:
298
+ In the response header, add a custom header
275
299
  ```ts
276
300
  export default defineMock({
277
301
  url: '/api/test',
@@ -291,11 +315,11 @@ export default defineMock({
291
315
  })
292
316
  ```
293
317
 
294
- #### 示例5:
295
- 定义多个相同url请求mock,并使用验证器匹配生效规则
318
+ #### Example 5:
319
+ Define multiple mock requests for the same url and match valid rules with validators
296
320
  ```ts
297
321
  export default defineMock([
298
- // 命中 /api/test?a=1
322
+ // Match /api/test?a=1
299
323
  {
300
324
  url: '/api/test',
301
325
  validator: {
@@ -307,7 +331,7 @@ export default defineMock([
307
331
  message: 'query.a === 1'
308
332
  }
309
333
  },
310
- // 命中 /api/test?a=2
334
+ // Match /api/test?a=2
311
335
  {
312
336
  url: '/api/test',
313
337
  validator: {
@@ -322,17 +346,17 @@ export default defineMock([
322
346
  ])
323
347
  ```
324
348
 
325
- #### 示例6:
326
- 延迟接口响应:
349
+ #### Example 6:
350
+ Response Delay
327
351
  ```ts
328
352
  export default defineMock({
329
353
  url: '/api/test',
330
- delay: 6000, // 延迟 6
354
+ delay: 6000, // delay 6 seconds
331
355
  })
332
356
  ```
333
357
 
334
- #### 示例7:
335
- 使接口请求失败
358
+ #### Example 7:
359
+ The interface request failed
336
360
  ```ts
337
361
  export default defineMock({
338
362
  url: '/api/test',
@@ -341,8 +365,8 @@ export default defineMock({
341
365
  })
342
366
  ```
343
367
 
344
- #### 示例8:
345
- 使用 `mockjs` 生成响应数据:
368
+ #### Example 8:
369
+ Use `mockjs`:
346
370
  ```ts
347
371
  import Mock from 'mockjs'
348
372
  export default defineMock({
@@ -354,10 +378,10 @@ export default defineMock({
354
378
  })
355
379
  })
356
380
  ```
357
- 请先安装 `mockjs`
381
+ You need installed `mockjs`
358
382
 
359
- ### 示例9:
360
- 使用 `response` 自定义响应
383
+ ### Example 9:
384
+ Use `response` to customize the response
361
385
  ```ts
362
386
  export default defineMock({
363
387
  url: '/api/test',
@@ -376,11 +400,11 @@ export default defineMock({
376
400
  })
377
401
  ```
378
402
 
379
- ### 示例10:
380
- 使用 json / json5
403
+ ### Example 10:
404
+ Use json / json5
381
405
  ```json
382
406
  {
383
- // 支持 comment
407
+ // Support comment
384
408
  "url": "/api/test",
385
409
  "body": {
386
410
  "a": 1