vite-plugin-mock-dev-server 1.1.1 → 1.1.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 +102 -73
- package/README.zh-CN.md +100 -74
- package/dist/index.cjs +63 -44
- package/dist/index.d.ts +190 -29
- package/dist/index.js +63 -44
- package/package.json +7 -5
package/README.md
CHANGED
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
- 🍕 Support `viteConfig.define` in mock file.
|
|
41
41
|
- ⚓️ Support `resolve.alias` in mock file.
|
|
42
42
|
- 📤 Support `multipart` content-type,mock upload file.
|
|
43
|
+
- 🍪 Support cookies
|
|
43
44
|
- 🌈 Support `vite preview` mode
|
|
44
45
|
- 🗂 Support building small independent deployable mock services.
|
|
45
46
|
|
|
@@ -193,6 +194,12 @@ export default defineConfig({
|
|
|
193
194
|
})
|
|
194
195
|
```
|
|
195
196
|
|
|
197
|
+
- `options.cookiesOptions`
|
|
198
|
+
|
|
199
|
+
Configure to `cookies`, see [cookies](https://github.com/pillarjs/cookies#new-cookiesrequest-response--options)
|
|
200
|
+
|
|
201
|
+
**Default:** `{}`
|
|
202
|
+
|
|
196
203
|
- `options.build`
|
|
197
204
|
|
|
198
205
|
Configuration needed to build a small, independently deployable mock service.
|
|
@@ -253,11 +260,12 @@ export default defineApiMock({
|
|
|
253
260
|
export default defineMock({
|
|
254
261
|
/**
|
|
255
262
|
* Request address, supports the `/api/user/:id` format.
|
|
263
|
+
* The plugin matches the path through `path-to-regexp`.
|
|
264
|
+
* @see https://github.com/pillarjs/path-to-regexp
|
|
256
265
|
*/
|
|
257
266
|
url: '/api/test',
|
|
258
267
|
/**
|
|
259
268
|
* Supported request methods of the interface.
|
|
260
|
-
*
|
|
261
269
|
* @type string | string[]
|
|
262
270
|
* @default ['POST','GET']
|
|
263
271
|
*
|
|
@@ -275,13 +283,11 @@ export default defineMock({
|
|
|
275
283
|
enable: true,
|
|
276
284
|
/**
|
|
277
285
|
* Set interface response delay, unit: ms.
|
|
278
|
-
*
|
|
279
286
|
* @default 0
|
|
280
287
|
*/
|
|
281
288
|
delay: 1000,
|
|
282
289
|
/**
|
|
283
290
|
* response status
|
|
284
|
-
*
|
|
285
291
|
* @default 200
|
|
286
292
|
*/
|
|
287
293
|
status: 200,
|
|
@@ -302,8 +308,8 @@ export default defineMock({
|
|
|
302
308
|
* then the validation method is to strictly compare whether the `value`
|
|
303
309
|
* of each `key` in headers/body/query/params in the request interface is exactly equal.
|
|
304
310
|
* If they are all equal, then the validation passes.
|
|
305
|
-
*
|
|
306
311
|
* @type ({ headers: object; body: object; query: object; params: object; refererQuery: object }) => boolean
|
|
312
|
+
*
|
|
307
313
|
* If the validator passed in is a function,
|
|
308
314
|
* then the data related to the requested interface will be provided as input parameters
|
|
309
315
|
* for users to perform custom validation and return a boolean.
|
|
@@ -322,11 +328,8 @@ export default defineMock({
|
|
|
322
328
|
refererQuery: {}
|
|
323
329
|
},
|
|
324
330
|
/**
|
|
325
|
-
*
|
|
326
331
|
* response headers
|
|
327
|
-
*
|
|
328
332
|
* @type Record<string, any>
|
|
329
|
-
*
|
|
330
333
|
* @type (({ query, body, params, headers }) => Record<string, any>)
|
|
331
334
|
*/
|
|
332
335
|
headers: {
|
|
@@ -335,27 +338,35 @@ export default defineMock({
|
|
|
335
338
|
|
|
336
339
|
/**
|
|
337
340
|
* response cookies
|
|
338
|
-
* @type Record<string, string |
|
|
341
|
+
* @type Record<string, string | [value: string, option: CookieOption]>
|
|
339
342
|
* @see https://github.com/pillarjs/cookies#cookiessetname--values--options
|
|
340
343
|
*/
|
|
341
344
|
cookies: {
|
|
342
345
|
'your-cookie': 'your cookie value',
|
|
343
|
-
'cookie&option': {
|
|
344
|
-
value: 'cookie value',
|
|
345
|
-
option: { path: '/', httpOnly: true }
|
|
346
|
-
}
|
|
346
|
+
'cookie&option': ['cookie value', { path: '/', httpOnly: true }]
|
|
347
347
|
},
|
|
348
|
+
/**
|
|
349
|
+
* Response body data type, optional values include `text, json, buffer`.
|
|
350
|
+
* And also support types included in `mime-db`.
|
|
351
|
+
* When the response body returns a file and you are not sure which type to use,
|
|
352
|
+
* you can pass the file name as the value. The plugin will internally search for matching
|
|
353
|
+
* `content-type` based on the file name suffix.
|
|
354
|
+
* However, if it is a TypeScript file such as `a.ts`, it may not be correctly matched
|
|
355
|
+
* as a JavaScript script. You need to modify `a.ts` to `a.js` as the value passed
|
|
356
|
+
* in order to recognize it correctly.
|
|
357
|
+
* @see https://github.com/jshttp/mime-db
|
|
358
|
+
* @default 'json'
|
|
359
|
+
*/
|
|
360
|
+
type: 'json',
|
|
348
361
|
|
|
349
362
|
/**
|
|
350
363
|
* Response Body
|
|
351
|
-
* Support `string/number/array/object`
|
|
364
|
+
* Support `string/number/array/object/buffer/ReadableStream`
|
|
352
365
|
* You can also use libraries such as' mockjs' to generate data content
|
|
353
|
-
*
|
|
354
|
-
* @type string | number | array | object
|
|
355
|
-
*
|
|
366
|
+
* @type string | number | array | object | ReadableStream | buffer
|
|
356
367
|
* @type (request: { headers, query, body, params, refererQuery, getCookie }) => any | Promise<any>
|
|
357
368
|
*/
|
|
358
|
-
body:
|
|
369
|
+
body: '',
|
|
359
370
|
|
|
360
371
|
/**
|
|
361
372
|
* If the mock requirement cannot be solved through `body` configuration,
|
|
@@ -404,37 +415,35 @@ type Response = http.ServerResponse<http.IncomingMessage> & {
|
|
|
404
415
|
```
|
|
405
416
|
|
|
406
417
|
|
|
407
|
-
> Tips
|
|
418
|
+
> **Tips:**
|
|
408
419
|
>
|
|
409
420
|
> If you write mock files using json/json5,
|
|
410
421
|
> the 'response' method is not supported,
|
|
411
422
|
> as is the function form that uses other fields.
|
|
412
423
|
|
|
424
|
+
## Example
|
|
425
|
+
|
|
413
426
|
`mock/**/*.mock.{ts,js,mjs,cjs,json,json5}`
|
|
414
427
|
|
|
415
428
|
See more examples: [example](/example/)
|
|
416
429
|
|
|
417
|
-
|
|
418
|
-
Match `/api/test`,And returns a response body content with empty data
|
|
430
|
+
**exp:** Match `/api/test`,And returns a response body content with empty data
|
|
419
431
|
```ts
|
|
420
432
|
export default defineMock({
|
|
421
433
|
url: '/api/test',
|
|
422
434
|
})
|
|
423
435
|
```
|
|
424
436
|
|
|
425
|
-
|
|
426
|
-
Match `/api/test` ,And returns a static content data
|
|
437
|
+
**exp:** Match `/api/test` ,And returns a static content data
|
|
427
438
|
```ts
|
|
428
439
|
export default defineMock({
|
|
429
440
|
url: '/api/test',
|
|
430
|
-
body: {
|
|
431
|
-
a: 1
|
|
432
|
-
}
|
|
441
|
+
body: { a: 1 },
|
|
433
442
|
})
|
|
434
443
|
```
|
|
435
444
|
|
|
436
|
-
|
|
437
|
-
Only Support `GET` Method
|
|
445
|
+
|
|
446
|
+
**exp:** Only Support `GET` Method
|
|
438
447
|
```ts
|
|
439
448
|
export default defineMock({
|
|
440
449
|
url: '/api/test',
|
|
@@ -442,69 +451,56 @@ export default defineMock({
|
|
|
442
451
|
})
|
|
443
452
|
```
|
|
444
453
|
|
|
445
|
-
|
|
446
|
-
In the response header, add a custom header
|
|
454
|
+
**exp:** In the response header, add a custom header and cookie
|
|
447
455
|
```ts
|
|
448
456
|
export default defineMock({
|
|
449
457
|
url: '/api/test',
|
|
450
|
-
headers: {
|
|
451
|
-
|
|
452
|
-
}
|
|
458
|
+
headers: { 'X-Custom': '12345678' },
|
|
459
|
+
cookies: { 'my-cookie': '123456789' },
|
|
453
460
|
})
|
|
454
461
|
```
|
|
455
462
|
```ts
|
|
456
463
|
export default defineMock({
|
|
457
464
|
url: '/api/test',
|
|
458
465
|
headers({ query, body, params, headers }) {
|
|
459
|
-
return {
|
|
460
|
-
|
|
461
|
-
|
|
466
|
+
return { 'X-Custom': query.custom }
|
|
467
|
+
},
|
|
468
|
+
cookies() {
|
|
469
|
+
return { 'my-cookie': '123456789' }
|
|
462
470
|
}
|
|
463
471
|
})
|
|
464
472
|
```
|
|
465
473
|
|
|
466
|
-
|
|
467
|
-
Define multiple mock requests for the same url and match valid rules with validators
|
|
474
|
+
|
|
475
|
+
**exp:** Define multiple mock requests for the same url and match valid rules with validators
|
|
468
476
|
```ts
|
|
469
477
|
export default defineMock([
|
|
470
478
|
// Match /api/test?a=1
|
|
471
479
|
{
|
|
472
480
|
url: '/api/test',
|
|
473
481
|
validator: {
|
|
474
|
-
query: {
|
|
475
|
-
a: 1
|
|
476
|
-
}
|
|
482
|
+
query: { a: 1 },
|
|
477
483
|
},
|
|
478
|
-
body: {
|
|
479
|
-
message: 'query.a == 1'
|
|
480
|
-
}
|
|
484
|
+
body: { message: 'query.a == 1' },
|
|
481
485
|
},
|
|
482
486
|
// Match /api/test?a=2
|
|
483
487
|
{
|
|
484
488
|
url: '/api/test',
|
|
485
489
|
validator: {
|
|
486
|
-
query: {
|
|
487
|
-
a: 2
|
|
488
|
-
}
|
|
490
|
+
query: { a: 2 },
|
|
489
491
|
},
|
|
490
|
-
body: {
|
|
491
|
-
message: 'query.a == 2'
|
|
492
|
-
}
|
|
492
|
+
body: { message: 'query.a == 2' },
|
|
493
493
|
},
|
|
494
494
|
{
|
|
495
|
-
|
|
496
|
-
* `?a=3` will resolve to `validator.query`
|
|
497
|
-
*/
|
|
495
|
+
// `?a=3` will resolve to `validator.query`
|
|
498
496
|
url: '/api/test?a=3',
|
|
499
|
-
body: {
|
|
500
|
-
message: 'query.a == 3'
|
|
501
|
-
}
|
|
497
|
+
body: { message: 'query.a == 3' }
|
|
502
498
|
}
|
|
503
499
|
])
|
|
504
500
|
```
|
|
505
501
|
|
|
506
|
-
|
|
507
|
-
Response Delay
|
|
502
|
+
|
|
503
|
+
**exp:** Response Delay
|
|
508
504
|
```ts
|
|
509
505
|
export default defineMock({
|
|
510
506
|
url: '/api/test',
|
|
@@ -512,8 +508,8 @@ export default defineMock({
|
|
|
512
508
|
})
|
|
513
509
|
```
|
|
514
510
|
|
|
515
|
-
|
|
516
|
-
The interface request failed
|
|
511
|
+
|
|
512
|
+
**exp:** The interface request failed
|
|
517
513
|
```ts
|
|
518
514
|
export default defineMock({
|
|
519
515
|
url: '/api/test',
|
|
@@ -522,23 +518,57 @@ export default defineMock({
|
|
|
522
518
|
})
|
|
523
519
|
```
|
|
524
520
|
|
|
525
|
-
|
|
526
|
-
Dynamic route matching
|
|
521
|
+
|
|
522
|
+
**exp:** Dynamic route matching
|
|
527
523
|
```ts
|
|
528
524
|
export default defineMock({
|
|
529
525
|
url: '/api/user/:userId',
|
|
530
526
|
body({ params }) {
|
|
531
|
-
return {
|
|
532
|
-
userId: params.userId,
|
|
533
|
-
}
|
|
527
|
+
return { userId: params.userId }
|
|
534
528
|
}
|
|
535
529
|
})
|
|
536
530
|
```
|
|
537
531
|
|
|
538
532
|
The `userId` in the route will be resolved into the `request.params` object.
|
|
539
533
|
|
|
540
|
-
|
|
541
|
-
|
|
534
|
+
**exp:** Use buffer to respond data
|
|
535
|
+
```ts
|
|
536
|
+
// Since the default value of type is json,
|
|
537
|
+
// although buffer is used for body during transmission,
|
|
538
|
+
// the content-type is still json.
|
|
539
|
+
export default defineMock({
|
|
540
|
+
url: 'api/buffer',
|
|
541
|
+
body: Buffer.from(JSON.stringify({ a: 1 }))
|
|
542
|
+
})
|
|
543
|
+
```
|
|
544
|
+
```ts
|
|
545
|
+
// When the type is buffer, the content-type is application/octet-stream.
|
|
546
|
+
// The data passed in through body will be converted to a buffer.
|
|
547
|
+
export default defineMock({
|
|
548
|
+
url: 'api/buffer',
|
|
549
|
+
type: 'buffer',
|
|
550
|
+
// Convert using Buffer.from(body) for internal use
|
|
551
|
+
body: { a: 1 }
|
|
552
|
+
})
|
|
553
|
+
```
|
|
554
|
+
|
|
555
|
+
**exp:** Response file type
|
|
556
|
+
|
|
557
|
+
Simulate file download, pass in the file reading stream.
|
|
558
|
+
```ts
|
|
559
|
+
import { createReadStream } from 'node:fs'
|
|
560
|
+
export default defineMock({
|
|
561
|
+
url: '/api/download',
|
|
562
|
+
// When you are unsure of the type, you can pass in the file name for internal parsing by the plugin.
|
|
563
|
+
type: 'my-app.dmg',
|
|
564
|
+
body: createReadStream('./my-app.dmg')
|
|
565
|
+
})
|
|
566
|
+
```
|
|
567
|
+
```html
|
|
568
|
+
<a href="/api/download" download="my-app.dmg">Download File</a>
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
**exp:** Use `mockjs`:
|
|
542
572
|
```ts
|
|
543
573
|
import Mock from 'mockjs'
|
|
544
574
|
export default defineMock({
|
|
@@ -552,8 +582,8 @@ export default defineMock({
|
|
|
552
582
|
```
|
|
553
583
|
You need installed `mockjs`
|
|
554
584
|
|
|
555
|
-
|
|
556
|
-
Use `response` to customize the response
|
|
585
|
+
|
|
586
|
+
**exp:** Use `response` to customize the response
|
|
557
587
|
```ts
|
|
558
588
|
export default defineMock({
|
|
559
589
|
url: '/api/test',
|
|
@@ -572,11 +602,10 @@ export default defineMock({
|
|
|
572
602
|
})
|
|
573
603
|
```
|
|
574
604
|
|
|
575
|
-
|
|
576
|
-
Use json / json5
|
|
605
|
+
|
|
606
|
+
**exp:** Use json / json5
|
|
577
607
|
```json
|
|
578
608
|
{
|
|
579
|
-
// Support comment
|
|
580
609
|
"url": "/api/test",
|
|
581
610
|
"body": {
|
|
582
611
|
"a": 1
|
|
@@ -584,9 +613,9 @@ Use json / json5
|
|
|
584
613
|
}
|
|
585
614
|
```
|
|
586
615
|
|
|
587
|
-
### Example 12:
|
|
588
616
|
|
|
589
|
-
|
|
617
|
+
|
|
618
|
+
**exp:** multipart, upload file.
|
|
590
619
|
|
|
591
620
|
use [`formidable`](https://www.npmjs.com/package/formidable#readme) to supported.
|
|
592
621
|
``` html
|