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/LICENSE +674 -21
- package/README.md +120 -96
- package/README.zh-CN.md +405 -0
- package/dist/index.cjs +16 -16
- package/dist/index.d.ts +6 -1
- package/dist/index.js +16 -16
- package/package.json +4 -1
package/README.md
CHANGED
|
@@ -1,28 +1,43 @@
|
|
|
1
1
|
# vite-plugin-mock-dev-server
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
|
|
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
|
-
- 🏷
|
|
14
|
-
- 📦
|
|
15
|
-
- 🎨
|
|
16
|
-
- 📥
|
|
17
|
-
- ⚙️
|
|
18
|
-
- 🔥
|
|
19
|
-
- ⚖️
|
|
20
|
-
- 🍕
|
|
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
|
-
|
|
72
|
+
The plugin reads the configuration for `server.proxy` and enables mock matching only for urls where the proxy is set.
|
|
59
73
|
|
|
60
|
-
|
|
74
|
+
The plugin also reads the `define` configuration and supports direct use in mock files.
|
|
61
75
|
|
|
62
|
-
>
|
|
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
|
-
###
|
|
78
|
+
### Edit Mock File
|
|
65
79
|
|
|
66
|
-
|
|
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
|
-
|
|
118
|
+
Configure to read mock files, which can be a directory, glob, or array
|
|
105
119
|
|
|
106
|
-
|
|
120
|
+
Default: `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (relative for `process.cwd()`)
|
|
107
121
|
|
|
108
122
|
- `options.exclude`
|
|
109
123
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
-
*
|
|
170
|
+
* enable/disable the current mock request
|
|
146
171
|
*
|
|
147
|
-
*
|
|
148
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
172
|
-
*
|
|
173
|
-
*
|
|
174
|
-
*
|
|
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
|
-
*
|
|
179
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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
|
-
*
|
|
221
|
-
*
|
|
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
|
-
>
|
|
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
|
-
|
|
267
|
+
See more examples: [example](/example/)
|
|
244
268
|
|
|
245
|
-
####
|
|
246
|
-
|
|
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
|
-
####
|
|
254
|
-
|
|
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
|
-
####
|
|
265
|
-
|
|
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
|
-
####
|
|
274
|
-
|
|
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
|
-
####
|
|
295
|
-
|
|
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
|
-
//
|
|
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
|
-
//
|
|
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
|
-
####
|
|
326
|
-
|
|
349
|
+
#### Example 6:
|
|
350
|
+
Response Delay
|
|
327
351
|
```ts
|
|
328
352
|
export default defineMock({
|
|
329
353
|
url: '/api/test',
|
|
330
|
-
delay: 6000, //
|
|
354
|
+
delay: 6000, // delay 6 seconds
|
|
331
355
|
})
|
|
332
356
|
```
|
|
333
357
|
|
|
334
|
-
####
|
|
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
|
-
####
|
|
345
|
-
|
|
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
|
-
|
|
381
|
+
You need installed `mockjs`
|
|
358
382
|
|
|
359
|
-
###
|
|
360
|
-
|
|
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
|
-
###
|
|
380
|
-
|
|
403
|
+
### Example 10:
|
|
404
|
+
Use json / json5
|
|
381
405
|
```json
|
|
382
406
|
{
|
|
383
|
-
//
|
|
407
|
+
// Support comment
|
|
384
408
|
"url": "/api/test",
|
|
385
409
|
"body": {
|
|
386
410
|
"a": 1
|