rspack-plugin-mock 1.3.0 → 2.1.0
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 +413 -67
- package/README.zh-CN.md +412 -67
- package/dist/{helper.d.mts → helper.d.ts} +46 -8
- package/dist/helper.js +267 -0
- package/dist/{types-Bt_OTa7I.d.mts → index-BMMGM-eD.d.ts} +537 -172
- package/dist/index.d.ts +16 -0
- package/dist/index.js +87 -0
- package/dist/json5-loader.mjs +8 -0
- package/dist/logger-CzeuHKAL.js +1563 -0
- package/dist/options-CI0LRpJZ.js +487 -0
- package/dist/rsbuild.d.ts +8 -0
- package/dist/rsbuild.js +80 -0
- package/dist/server-pC78IYdH.d.ts +113 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +2 -0
- package/package.json +38 -34
- package/dist/helper.mjs +0 -4
- package/dist/index.d.mts +0 -13
- package/dist/index.mjs +0 -1
- package/dist/json5-loader.cjs +0 -1
- package/dist/options-BUfaThYe.mjs +0 -43
- package/dist/rsbuild.d.mts +0 -7
- package/dist/rsbuild.mjs +0 -1
- package/dist/server-4ETytB7L.d.mts +0 -89
- package/dist/server.d.mts +0 -3
- package/dist/server.mjs +0 -1
- package/dist/ws-BM06pHhL.mjs +0 -1
package/README.md
CHANGED
|
@@ -10,18 +10,23 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
|
|
|
10
10
|
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square&colorA=564341&colorB=EDED91&label=rspack">
|
|
11
11
|
<img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square&colorA=564341&colorB=EDED91&label=rsbuild">
|
|
12
12
|
<img alt="npm" src="https://img.shields.io/npm/dm/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
|
|
13
|
-
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/rspack-plugin-mock/
|
|
13
|
+
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/rspack-plugin-mock/release.yaml?style=flat-square&colorA=564341&colorB=EDED91">
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
16
|
<p align="center">
|
|
17
17
|
<span>English</span> | <a href="./README.zh-CN.md">简体中文</a>
|
|
18
18
|
</p>
|
|
19
19
|
|
|
20
|
+
> [!IMPORTANT]
|
|
21
|
+
> Starting from version 2.0 of the plugin, only `rspack@2.x` and `rsbuild@2.x` versions are supported.
|
|
22
|
+
>
|
|
23
|
+
> If you are using `rspack@1.x` or `rsbuild@1.x`, please use version `rspack-plugin-mock@1.x`.
|
|
24
|
+
|
|
20
25
|
## Features
|
|
21
26
|
|
|
22
27
|
- ⚡️ Lightweight, Flexible, Fast.
|
|
23
28
|
- 🧲 Not injection-based, non-intrusive to client code.
|
|
24
|
-
- 💡 ESModule
|
|
29
|
+
- 💡 Pure ESModule
|
|
25
30
|
- 🦾 Typescript.
|
|
26
31
|
- 🔥 HMR
|
|
27
32
|
- 🏷 Support `.[cm]?js`/ `.ts` /`.json` / `.json5`.
|
|
@@ -36,6 +41,7 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
|
|
|
36
41
|
- 📤 Support `multipart` content-type, mock upload file.
|
|
37
42
|
- 📥 Support mock download file.
|
|
38
43
|
- ⚜️ Support `WebSocket Mock` and `Server-Sent Events Mock`
|
|
44
|
+
- 📝 Support **recording** and **replay requests**
|
|
39
45
|
- 🗂 Support building small independent deployable mock services.
|
|
40
46
|
|
|
41
47
|
## Install
|
|
@@ -206,7 +212,15 @@ export default defineMock({
|
|
|
206
212
|
|
|
207
213
|
## Plugin Options
|
|
208
214
|
|
|
209
|
-
###
|
|
215
|
+
### enabled
|
|
216
|
+
|
|
217
|
+
- **Type:** `boolean`
|
|
218
|
+
- **Default:** `true`
|
|
219
|
+
- **Details:**
|
|
220
|
+
|
|
221
|
+
Whether to enable mock server, if set to `false`, the plugin will not work.
|
|
222
|
+
|
|
223
|
+
### prefix
|
|
210
224
|
|
|
211
225
|
- **Type:** `string | string[]`
|
|
212
226
|
- **Details:**
|
|
@@ -215,7 +229,7 @@ export default defineMock({
|
|
|
215
229
|
any request path starting with prefix will be intercepted and proxied.
|
|
216
230
|
If the prefix starts with `^`, it will be recognized as a `RegExp`.
|
|
217
231
|
|
|
218
|
-
###
|
|
232
|
+
### wsPrefix
|
|
219
233
|
|
|
220
234
|
- **Type:** `string | string[]`
|
|
221
235
|
- **Details:**
|
|
@@ -227,7 +241,7 @@ export default defineMock({
|
|
|
227
241
|
|
|
228
242
|
Please avoid having the configurations in `wsPrefix` appear in `devServer.proxy` / `server.proxy`, as this may lead to conflicts in the rules.
|
|
229
243
|
|
|
230
|
-
###
|
|
244
|
+
### cwd
|
|
231
245
|
|
|
232
246
|
- **Type:** `string`
|
|
233
247
|
- **Default:** `process.cwd()`
|
|
@@ -235,31 +249,31 @@ export default defineMock({
|
|
|
235
249
|
|
|
236
250
|
Configure the matching context for `include` and `exclude`.
|
|
237
251
|
|
|
238
|
-
###
|
|
252
|
+
### dir
|
|
239
253
|
|
|
240
254
|
- **Type:** `string`
|
|
241
|
-
- **Default:** `mock` (relative to [`
|
|
255
|
+
- **Default:** `mock` (relative to [`cwd`](#cwd))
|
|
242
256
|
- **Details:**
|
|
243
257
|
|
|
244
258
|
Configure the directory where mock files are located
|
|
245
259
|
|
|
246
|
-
###
|
|
260
|
+
### include
|
|
247
261
|
|
|
248
262
|
- **Type:** `string | string[]`
|
|
249
|
-
- **Default:** `['**/*.mock.{js,ts,cjs,mjs,json,json5}']` (relative to [`
|
|
263
|
+
- **Default:** `['**/*.mock.{js,ts,cjs,mjs,json,json5}']` (relative to [`dir`](#dir))
|
|
250
264
|
- **Details:**
|
|
251
265
|
|
|
252
266
|
glob string matching mock includes files. see [picomatch](https://github.com/micromatch/picomatch#globbing-features)
|
|
253
267
|
|
|
254
|
-
###
|
|
268
|
+
### exclude
|
|
255
269
|
|
|
256
270
|
- **Type:** `string | string[]`
|
|
257
|
-
- **Default:** `[]` (relative to [`
|
|
271
|
+
- **Default:** `[]` (relative to [`dir`](#dir))
|
|
258
272
|
- **Details:**
|
|
259
273
|
|
|
260
274
|
glob string matching mock excluded files. see [picomatch](https://github.com/micromatch/picomatch#globbing-features)
|
|
261
275
|
|
|
262
|
-
###
|
|
276
|
+
### log
|
|
263
277
|
|
|
264
278
|
- **Type:** `boolean | 'info' | 'warn' | 'error' | 'silent' | 'debug'`
|
|
265
279
|
- **Default:** `info`
|
|
@@ -267,7 +281,7 @@ export default defineMock({
|
|
|
267
281
|
|
|
268
282
|
Enable log and configure log level
|
|
269
283
|
|
|
270
|
-
###
|
|
284
|
+
### reload
|
|
271
285
|
|
|
272
286
|
- **Type:** `boolean`
|
|
273
287
|
- **Default:** `false`
|
|
@@ -276,7 +290,17 @@ export default defineMock({
|
|
|
276
290
|
If you want to refresh the page every time you modify a mock file,
|
|
277
291
|
you can open this option.
|
|
278
292
|
|
|
279
|
-
###
|
|
293
|
+
### activeScene
|
|
294
|
+
|
|
295
|
+
- **Type:** `string | string[]`
|
|
296
|
+
- **Default:** `''`
|
|
297
|
+
- **Details:**
|
|
298
|
+
|
|
299
|
+
Active scenario(s) for filtering mocks.
|
|
300
|
+
|
|
301
|
+
Only mocks whose [`scene`](#optionsscene) intersects with this value (or have no `scene` configured) will be considered for matching. Can be overridden per-request via the `X-Mock-Scene` header.
|
|
302
|
+
|
|
303
|
+
### cors
|
|
280
304
|
|
|
281
305
|
- **Type:** `boolean | CorsOptions`
|
|
282
306
|
- **Default:** `true`
|
|
@@ -284,7 +308,7 @@ export default defineMock({
|
|
|
284
308
|
|
|
285
309
|
Configure to [cors](https://github.com/expressjs/cors#configuration-options)
|
|
286
310
|
|
|
287
|
-
###
|
|
311
|
+
### formidableOptions
|
|
288
312
|
|
|
289
313
|
- **Type:** `FormidableOptions`
|
|
290
314
|
- **Default:** `{ multiples: true }`
|
|
@@ -292,21 +316,21 @@ export default defineMock({
|
|
|
292
316
|
|
|
293
317
|
Configure to [formidable](https://github.com/node-formidable/formidable#options)
|
|
294
318
|
|
|
295
|
-
###
|
|
319
|
+
### cookiesOptions
|
|
296
320
|
|
|
297
321
|
- **Type:** `CookiesOptions`
|
|
298
322
|
- **Details:**
|
|
299
323
|
|
|
300
324
|
Configure to [cookies](https://github.com/pillarjs/cookies#new-cookiesrequest-response--options)
|
|
301
325
|
|
|
302
|
-
###
|
|
326
|
+
### bodyParserOptions
|
|
303
327
|
|
|
304
328
|
- **Type:** `BodyParserOptions`
|
|
305
329
|
- **Details:**
|
|
306
330
|
|
|
307
331
|
Configure to [co-body](https://github.com/cojs/co-body#options)
|
|
308
332
|
|
|
309
|
-
##
|
|
333
|
+
## build
|
|
310
334
|
|
|
311
335
|
- **Type:** `boolean | ServerBuildOption`
|
|
312
336
|
|
|
@@ -336,8 +360,143 @@ export default defineMock({
|
|
|
336
360
|
|
|
337
361
|
When you need to build a small mock service, you can configure this option.
|
|
338
362
|
|
|
363
|
+
### record
|
|
364
|
+
|
|
365
|
+
- **Type:** `false | RecordOptions`
|
|
366
|
+
- **Default:** `false`
|
|
367
|
+
- **Details:**
|
|
368
|
+
|
|
369
|
+
Whether to enable the request recording feature. Once enabled, the plugin will record all request data for subsequent request playback.
|
|
370
|
+
|
|
371
|
+
Based on `proxy`, the plugin records request data proxied by `http-proxy`.
|
|
372
|
+
After receiving a response, the plugin will record the request data and response data to the specified directory.
|
|
373
|
+
|
|
374
|
+
```ts
|
|
375
|
+
interface RecordOptions {
|
|
376
|
+
/**
|
|
377
|
+
* Whether to enable the record feature
|
|
378
|
+
* - true: Enable, automatically record proxy responses
|
|
379
|
+
* - false: Disable (default)
|
|
380
|
+
* @default false
|
|
381
|
+
*/
|
|
382
|
+
enabled?: boolean
|
|
383
|
+
/**
|
|
384
|
+
* Filter requests to record
|
|
385
|
+
* - Function: Custom filter function, return true to record
|
|
386
|
+
* - Object: Include/exclude patterns with glob or path-to-regexp mode
|
|
387
|
+
* @example
|
|
388
|
+
* ```ts
|
|
389
|
+
* // Record all requests
|
|
390
|
+
* filter: (req) => true
|
|
391
|
+
* // Record requests using glob pattern
|
|
392
|
+
* filter: { mode: 'glob', include: '/api/**' }
|
|
393
|
+
* // Record requests using path-to-regexp pattern
|
|
394
|
+
* filter: { mode: 'path-to-regexp', include: '/api/:id' }
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
filter?: ((req: RecordedReq) => boolean) | {
|
|
398
|
+
/**
|
|
399
|
+
* Include the request links that need to be recorded
|
|
400
|
+
*
|
|
401
|
+
* String: Glob pattern or path-to-regexp pattern
|
|
402
|
+
* (Use the mode option to set the mode, default is glob)
|
|
403
|
+
*/
|
|
404
|
+
include?: string | string[]
|
|
405
|
+
/**
|
|
406
|
+
* Exclude request links that do not need to be recorded
|
|
407
|
+
*
|
|
408
|
+
* String: Glob pattern or path-to-regexp pattern
|
|
409
|
+
* (Use the mode option to set the mode, default is glob)
|
|
410
|
+
*/
|
|
411
|
+
exclude?: string | string[]
|
|
412
|
+
/**
|
|
413
|
+
* Matching mode for include/exclude patterns
|
|
414
|
+
* - 'glob': Glob pattern matching (default)
|
|
415
|
+
* - 'path-to-regexp': Path-to-regexp pattern matching
|
|
416
|
+
*/
|
|
417
|
+
mode: 'glob' | 'path-to-regexp'
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Directory to store recorded data
|
|
422
|
+
* Relative to project root
|
|
423
|
+
* @default 'mock/.recordings'
|
|
424
|
+
*/
|
|
425
|
+
dir?: string
|
|
426
|
+
/**
|
|
427
|
+
* Whether to overwrite existing recorded data
|
|
428
|
+
* - true: Overwrite old data for the same request (default)
|
|
429
|
+
* - false: Keep old data, do not record new data
|
|
430
|
+
* @default true
|
|
431
|
+
*/
|
|
432
|
+
overwrite?: boolean
|
|
433
|
+
/**
|
|
434
|
+
* Expiration time for recorded data in seconds
|
|
435
|
+
* - 0: Never expire (default)
|
|
436
|
+
* - Positive number: Expire after specified seconds
|
|
437
|
+
* @default 0
|
|
438
|
+
*/
|
|
439
|
+
expires?: number
|
|
440
|
+
/**
|
|
441
|
+
* Status codes to record
|
|
442
|
+
* - Empty array: Record all status codes (default)
|
|
443
|
+
* - Specify one or more status codes to filter
|
|
444
|
+
* @default []
|
|
445
|
+
*/
|
|
446
|
+
status?: number | number[]
|
|
447
|
+
/**
|
|
448
|
+
* Should a .gitignore be added to the recording directory
|
|
449
|
+
* - true: Add (default)
|
|
450
|
+
* - false: Do not add
|
|
451
|
+
* @default true
|
|
452
|
+
*/
|
|
453
|
+
gitignore?: boolean
|
|
454
|
+
}
|
|
455
|
+
```
|
|
456
|
+
|
|
457
|
+
### replay
|
|
458
|
+
|
|
459
|
+
- **Type:** `boolean`
|
|
460
|
+
- **Default:** `false`
|
|
461
|
+
- **Details:**
|
|
462
|
+
|
|
463
|
+
Whether to enable the request playback feature. Once enabled, the plugin will simulate responses based on the recorded request data.
|
|
464
|
+
|
|
465
|
+
### priority
|
|
466
|
+
|
|
467
|
+
- **Type:** `MockMatchPriority`
|
|
468
|
+
- **Details:**
|
|
469
|
+
|
|
470
|
+
Custom path matching rule priority。[read more](#custom-path-matching-priority)
|
|
471
|
+
|
|
339
472
|
## Mock Options
|
|
340
473
|
|
|
474
|
+
**http mock**
|
|
475
|
+
|
|
476
|
+
```ts
|
|
477
|
+
import { defineMock } from 'rspack-plugin-mock/helper'
|
|
478
|
+
export default defineMock({
|
|
479
|
+
url: '/api/test',
|
|
480
|
+
body: { message: 'hello world' }
|
|
481
|
+
})
|
|
482
|
+
```
|
|
483
|
+
|
|
484
|
+
**websocket mock**
|
|
485
|
+
|
|
486
|
+
```ts
|
|
487
|
+
import { defineMock } from 'rspack-plugin-mock/helper'
|
|
488
|
+
|
|
489
|
+
export default defineMock({
|
|
490
|
+
url: '/socket.io',
|
|
491
|
+
ws: true,
|
|
492
|
+
setup(wss) {
|
|
493
|
+
wss.on('connection', (ws, req) => {
|
|
494
|
+
console.log('connected')
|
|
495
|
+
})
|
|
496
|
+
}
|
|
497
|
+
})
|
|
498
|
+
```
|
|
499
|
+
|
|
341
500
|
### options.url
|
|
342
501
|
|
|
343
502
|
- **Type:** `string`
|
|
@@ -354,6 +513,16 @@ export default defineMock({
|
|
|
354
513
|
Whether to enable mock for this interface. In most scenarios, we only need to mock some interfaces instead of all requests that have been configured with mock.
|
|
355
514
|
Therefore, it is important to be able to configure whether to enable it or not.
|
|
356
515
|
|
|
516
|
+
### options.scene
|
|
517
|
+
|
|
518
|
+
- **Type:** `string | string[]`
|
|
519
|
+
- **Default:** `''`
|
|
520
|
+
- **Details:**
|
|
521
|
+
|
|
522
|
+
Scenario identifier for this mock.
|
|
523
|
+
|
|
524
|
+
When not configured, the mock is universal and always matches regardless of active scenario.When configured, the mock only matches when at least one of its scenarios matches one of the active scenarios.
|
|
525
|
+
|
|
357
526
|
### options.method
|
|
358
527
|
|
|
359
528
|
- **Type:** `Method | Method[]`
|
|
@@ -519,55 +688,181 @@ interface WebSocketSetupContext {
|
|
|
519
688
|
}
|
|
520
689
|
```
|
|
521
690
|
|
|
522
|
-
###
|
|
691
|
+
### options.error
|
|
523
692
|
|
|
524
|
-
|
|
525
|
-
|
|
693
|
+
- **Type:** `MockErrorConfig | undefined`
|
|
694
|
+
- **Details:**
|
|
526
695
|
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* Set cookie in response
|
|
530
|
-
* @see [cookies](https://github.com/pillarjs/cookies#cookiessetname--values--options)
|
|
531
|
-
*/
|
|
532
|
-
setCookie: (
|
|
533
|
-
name: string,
|
|
534
|
-
value?: string | null,
|
|
535
|
-
option?: Cookies.SetOption,
|
|
536
|
-
) => void
|
|
537
|
-
}
|
|
696
|
+
Configure error simulation, including error probability, error status code, error status text, and custom error response body.
|
|
538
697
|
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
* The query string located after `?` in the request address has been parsed into JSON.
|
|
542
|
-
*/
|
|
543
|
-
query: Record<string, any>
|
|
544
|
-
/**
|
|
545
|
-
* The queryString located after `?` in the referer request has been parsed as JSON.
|
|
546
|
-
*/
|
|
547
|
-
refererQuery: Record<string, any>
|
|
698
|
+
```ts
|
|
699
|
+
interface MockErrorConfig {
|
|
548
700
|
/**
|
|
549
|
-
*
|
|
701
|
+
* Error probability (0-1), default is 0.5
|
|
702
|
+
* @default 0.5
|
|
550
703
|
*/
|
|
551
|
-
|
|
704
|
+
probability?: number
|
|
552
705
|
/**
|
|
553
|
-
*
|
|
706
|
+
* Error status code, default is 500
|
|
707
|
+
* @default 500
|
|
554
708
|
*/
|
|
555
|
-
|
|
709
|
+
status?: number
|
|
556
710
|
/**
|
|
557
|
-
*
|
|
711
|
+
* Error status text
|
|
558
712
|
*/
|
|
559
|
-
|
|
713
|
+
statusText?: string
|
|
560
714
|
/**
|
|
561
|
-
*
|
|
562
|
-
* @
|
|
715
|
+
* Custom error response body, suitable for when the status is 200, but the response body needs to simulate an error scenario
|
|
716
|
+
* @example
|
|
717
|
+
* { code: 500, msg: 'Internal Server Error', result: null }
|
|
563
718
|
*/
|
|
719
|
+
body?: ResponseBody | ResponseBodyFn
|
|
720
|
+
}
|
|
721
|
+
```
|
|
722
|
+
|
|
723
|
+
## Request/Response Enhance
|
|
724
|
+
|
|
725
|
+
When defining methods using `headers`, `body`, and `response`, the plugin adds new content to the `request` and `response` parameters.
|
|
726
|
+
|
|
727
|
+
**In Request:**
|
|
728
|
+
|
|
729
|
+
The original type of `request` is [`http.IncomingMessage`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/connect/index.d.ts). The plugin adds data such as `query`, `params`, `body`, `refererQuery`, and the `getCookie(name)` method for obtaining cookie information on this basis.
|
|
730
|
+
|
|
731
|
+
```ts
|
|
732
|
+
type Request = http.IncomingMessage & {
|
|
733
|
+
query: object
|
|
734
|
+
params: object
|
|
735
|
+
body: any
|
|
736
|
+
refererQuery: object
|
|
564
737
|
getCookie: (name: string, option?: Cookies.GetOption) => string | undefined
|
|
565
738
|
}
|
|
566
739
|
```
|
|
567
740
|
|
|
741
|
+
**In Response:**
|
|
742
|
+
|
|
743
|
+
The original type of `response` is `http.ServerResponse<http.IncomingMessage>`. The plugin adds `setCookie(name, value)` method for configuration cookies on this basis.
|
|
744
|
+
|
|
745
|
+
``` ts
|
|
746
|
+
type Response = http.ServerResponse<http.IncomingMessage> & {
|
|
747
|
+
setCookie: (
|
|
748
|
+
name: string,
|
|
749
|
+
value?: string | null,
|
|
750
|
+
option?: Cookies.SetOption,
|
|
751
|
+
) => void
|
|
752
|
+
}
|
|
753
|
+
```
|
|
754
|
+
|
|
755
|
+
## Share Mock Data
|
|
756
|
+
|
|
757
|
+
Due to each `mock` file being compiled as a separate entry point, the local files they depend on are also compiled within. Additionally, each mock file has an independent scope. This means that even if multiple mock files collectively depend on a `data.ts` file, they cannot share data. If one mock file modifies the data in `data.ts`, other mock files will not receive the updated data.
|
|
758
|
+
|
|
759
|
+
To address this, the plugin offers a `defineMockData` function, which allows using `data.ts` as a shared data source within mock files.
|
|
760
|
+
|
|
761
|
+
```ts
|
|
762
|
+
type defineMockData<T> = (
|
|
763
|
+
key: string, // key
|
|
764
|
+
initialData: T, // initial data
|
|
765
|
+
options?: {
|
|
766
|
+
persistOnHMR?: boolean // persist the data value on HMR
|
|
767
|
+
} // options
|
|
768
|
+
) => [getter, setter] & { value: T }
|
|
769
|
+
```
|
|
770
|
+
|
|
771
|
+
### Examples
|
|
772
|
+
|
|
773
|
+
`data.ts`
|
|
774
|
+
|
|
775
|
+
```ts
|
|
776
|
+
import { defineMockData } from 'rspack-plugin-mock/helper'
|
|
777
|
+
|
|
778
|
+
export default defineMockData('posts', [
|
|
779
|
+
{ id: '1', title: 'title1', content: 'content1' },
|
|
780
|
+
{ id: '2', title: 'title2', content: 'content2' },
|
|
781
|
+
])
|
|
782
|
+
```
|
|
783
|
+
|
|
784
|
+
`*.mock.ts`
|
|
785
|
+
|
|
786
|
+
```ts
|
|
787
|
+
import { defineMock } from 'rspack-plugin-mock/helper'
|
|
788
|
+
import posts from './data'
|
|
789
|
+
|
|
790
|
+
export default defineMock([
|
|
791
|
+
{
|
|
792
|
+
url: '/api/posts',
|
|
793
|
+
body: () => posts.value
|
|
794
|
+
},
|
|
795
|
+
{
|
|
796
|
+
url: '/api/posts/delete/:id',
|
|
797
|
+
body: (params) => {
|
|
798
|
+
const id = params.id
|
|
799
|
+
posts.value = posts.value.filter(post => post.id !== id)
|
|
800
|
+
return { success: true }
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
])
|
|
804
|
+
```
|
|
805
|
+
|
|
806
|
+
> **Tips:**
|
|
807
|
+
>
|
|
808
|
+
> The `defineMockData` function relies solely on the shared data support provided by `memory`.
|
|
809
|
+
> If persistent mock data is required, it is recommended to use a `nosql` database like `lowdb` or `level`.
|
|
810
|
+
|
|
811
|
+
## Custom-Path-Matching-Priority
|
|
812
|
+
|
|
813
|
+
> Custom rules only affect links with dynamic parameters, such as: `/api/user/:id`
|
|
814
|
+
|
|
815
|
+
The priority of the path matching rules built into the plugin can already meet most needs, but if you need more flexible customization of the matching rule priority, you can use the `priority` parameter.
|
|
816
|
+
|
|
817
|
+
Exp:
|
|
818
|
+
|
|
819
|
+
```ts
|
|
820
|
+
import { MockServerPlugin } from 'rspack-plugin-mock'
|
|
821
|
+
|
|
822
|
+
export default {
|
|
823
|
+
plugins: [
|
|
824
|
+
new MockServerPlugin({
|
|
825
|
+
priority: {
|
|
826
|
+
// The priority of matching rules is global.
|
|
827
|
+
// The rules declared in this option will take priority over the default rules.
|
|
828
|
+
// The higher the position of the rule in the array, the higher the priority.
|
|
829
|
+
global: ['/api/:a/b/c', '/api/a/:b/c', '/api/a/b/:c'],
|
|
830
|
+
// For some special cases where the priority of certain rules needs to be adjusted,
|
|
831
|
+
// this option can be used. For example, when a request matches both Rule A and Rule B,
|
|
832
|
+
// and Rule A has a higher priority than Rule B, but it is desired for Rule B to take effect.
|
|
833
|
+
special: {
|
|
834
|
+
// When both A and B or C match, and B or C is at the top of the sort order,
|
|
835
|
+
// insert A into the top position.
|
|
836
|
+
// The `when` option is used to further constrain the priority adjustment to
|
|
837
|
+
// be effective only for certain requests.
|
|
838
|
+
'/api/:a/:b/c': {
|
|
839
|
+
rules: ['/api/a/:b/:c', '/api/a/b/:c'],
|
|
840
|
+
when: ['/api/a/b/c']
|
|
841
|
+
},
|
|
842
|
+
// If no `when` is specified, it means that all requests matching the rules need to have their priorities adjusted. It can be abbreviated as `[key]: [...rules]`
|
|
843
|
+
'/api/:a/b': ['/api/a/:b'],
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
})
|
|
847
|
+
]
|
|
848
|
+
}
|
|
849
|
+
```
|
|
850
|
+
|
|
851
|
+
> **Tip:**
|
|
852
|
+
>
|
|
853
|
+
> `priority` although it can adjust the priority,
|
|
854
|
+
> most of the time you do not need to do so. For some special requests,
|
|
855
|
+
> you can use static rules instead of `priority`,
|
|
856
|
+
> as static rules always have the highest priority.
|
|
857
|
+
|
|
568
858
|
## Examples
|
|
569
859
|
|
|
570
|
-
|
|
860
|
+
`mock/**/*.mock.{ts,js,mjs,cjs,json,json5}`
|
|
861
|
+
|
|
862
|
+
See more examples: [example](/example/)
|
|
863
|
+
|
|
864
|
+
<details>
|
|
865
|
+
<summary>Match <code>/api/test</code>, And returns a response body content with empty data</summary>
|
|
571
866
|
|
|
572
867
|
``` ts
|
|
573
868
|
export default defineMock({
|
|
@@ -575,7 +870,10 @@ export default defineMock({
|
|
|
575
870
|
})
|
|
576
871
|
```
|
|
577
872
|
|
|
578
|
-
|
|
873
|
+
</details>
|
|
874
|
+
|
|
875
|
+
<details>
|
|
876
|
+
<summary>Match <code>/api/test</code> , And returns a static content data</summary>
|
|
579
877
|
|
|
580
878
|
``` ts
|
|
581
879
|
export default defineMock({
|
|
@@ -584,7 +882,10 @@ export default defineMock({
|
|
|
584
882
|
})
|
|
585
883
|
```
|
|
586
884
|
|
|
587
|
-
|
|
885
|
+
</details>
|
|
886
|
+
|
|
887
|
+
<details>
|
|
888
|
+
<summary>Only Support <code>GET</code> Method</summary>
|
|
588
889
|
|
|
589
890
|
``` ts
|
|
590
891
|
export default defineMock({
|
|
@@ -593,7 +894,10 @@ export default defineMock({
|
|
|
593
894
|
})
|
|
594
895
|
```
|
|
595
896
|
|
|
596
|
-
|
|
897
|
+
</details>
|
|
898
|
+
|
|
899
|
+
<details>
|
|
900
|
+
<summary>In the response header, add a custom header and cookie</summary>
|
|
597
901
|
|
|
598
902
|
``` ts
|
|
599
903
|
export default defineMock({
|
|
@@ -615,7 +919,10 @@ export default defineMock({
|
|
|
615
919
|
})
|
|
616
920
|
```
|
|
617
921
|
|
|
618
|
-
|
|
922
|
+
</details>
|
|
923
|
+
|
|
924
|
+
<details>
|
|
925
|
+
<summary>Define multiple mock requests for the same URL and match valid rules with validators</summary>
|
|
619
926
|
|
|
620
927
|
``` ts
|
|
621
928
|
export default defineMock([
|
|
@@ -650,7 +957,10 @@ export default defineMock([
|
|
|
650
957
|
])
|
|
651
958
|
```
|
|
652
959
|
|
|
653
|
-
|
|
960
|
+
</details>
|
|
961
|
+
|
|
962
|
+
<details>
|
|
963
|
+
<summary>Response Delay</summary>
|
|
654
964
|
|
|
655
965
|
``` ts
|
|
656
966
|
export default defineMock({
|
|
@@ -659,7 +969,10 @@ export default defineMock({
|
|
|
659
969
|
})
|
|
660
970
|
```
|
|
661
971
|
|
|
662
|
-
|
|
972
|
+
</details>
|
|
973
|
+
|
|
974
|
+
<details>
|
|
975
|
+
<summary>The interface request failed</summary>
|
|
663
976
|
|
|
664
977
|
``` ts
|
|
665
978
|
export default defineMock({
|
|
@@ -669,7 +982,10 @@ export default defineMock({
|
|
|
669
982
|
})
|
|
670
983
|
```
|
|
671
984
|
|
|
672
|
-
|
|
985
|
+
</details>
|
|
986
|
+
|
|
987
|
+
<details>
|
|
988
|
+
<summary>Dynamic route matching</summary>
|
|
673
989
|
|
|
674
990
|
``` ts
|
|
675
991
|
export default defineMock({
|
|
@@ -682,7 +998,10 @@ export default defineMock({
|
|
|
682
998
|
|
|
683
999
|
The `userId` in the route will be resolved into the `request.params` object.
|
|
684
1000
|
|
|
685
|
-
|
|
1001
|
+
</details>
|
|
1002
|
+
|
|
1003
|
+
<details>
|
|
1004
|
+
<summary>Use the buffer to respond data</summary>
|
|
686
1005
|
|
|
687
1006
|
```ts
|
|
688
1007
|
import { Buffer } from 'node:buffer'
|
|
@@ -707,7 +1026,10 @@ export default defineMock({
|
|
|
707
1026
|
})
|
|
708
1027
|
```
|
|
709
1028
|
|
|
710
|
-
|
|
1029
|
+
</details>
|
|
1030
|
+
|
|
1031
|
+
<details>
|
|
1032
|
+
<summary>Response file type</summary>
|
|
711
1033
|
|
|
712
1034
|
Simulate file download, and pass in the file reading stream.
|
|
713
1035
|
|
|
@@ -726,7 +1048,10 @@ export default defineMock({
|
|
|
726
1048
|
<a href="/api/download" download="my-app.dmg">Download File</a>
|
|
727
1049
|
```
|
|
728
1050
|
|
|
729
|
-
|
|
1051
|
+
</details>
|
|
1052
|
+
|
|
1053
|
+
<details>
|
|
1054
|
+
<summary>Use <code>mockjs</code></summary>
|
|
730
1055
|
|
|
731
1056
|
``` ts
|
|
732
1057
|
import Mock from 'mockjs'
|
|
@@ -743,7 +1068,11 @@ export default defineMock({
|
|
|
743
1068
|
|
|
744
1069
|
You need install `mockjs`
|
|
745
1070
|
|
|
746
|
-
|
|
1071
|
+
</details>
|
|
1072
|
+
|
|
1073
|
+
<details>
|
|
1074
|
+
|
|
1075
|
+
<summary>Use <code>response</code> to customize the response</summary>
|
|
747
1076
|
|
|
748
1077
|
``` ts
|
|
749
1078
|
export default defineMock({
|
|
@@ -763,7 +1092,10 @@ export default defineMock({
|
|
|
763
1092
|
})
|
|
764
1093
|
```
|
|
765
1094
|
|
|
766
|
-
|
|
1095
|
+
</details>
|
|
1096
|
+
|
|
1097
|
+
<details>
|
|
1098
|
+
<summary>Use json / json5</summary>
|
|
767
1099
|
|
|
768
1100
|
``` json
|
|
769
1101
|
{
|
|
@@ -774,7 +1106,10 @@ export default defineMock({
|
|
|
774
1106
|
}
|
|
775
1107
|
```
|
|
776
1108
|
|
|
777
|
-
|
|
1109
|
+
</details>
|
|
1110
|
+
|
|
1111
|
+
<details>
|
|
1112
|
+
<summary>multipart, upload files.</summary>
|
|
778
1113
|
|
|
779
1114
|
use [`formidable`](https://www.npmjs.com/package/formidable#readme) to support.
|
|
780
1115
|
|
|
@@ -810,7 +1145,10 @@ export default defineMock({
|
|
|
810
1145
|
})
|
|
811
1146
|
```
|
|
812
1147
|
|
|
813
|
-
|
|
1148
|
+
</details>
|
|
1149
|
+
|
|
1150
|
+
<details>
|
|
1151
|
+
<summary>Graphql</summary>
|
|
814
1152
|
|
|
815
1153
|
``` ts
|
|
816
1154
|
import { buildSchema, graphql } from 'graphql'
|
|
@@ -840,7 +1178,10 @@ fetch('/api/graphql', {
|
|
|
840
1178
|
})
|
|
841
1179
|
```
|
|
842
1180
|
|
|
843
|
-
|
|
1181
|
+
</details>
|
|
1182
|
+
|
|
1183
|
+
<details>
|
|
1184
|
+
<summary>WebSocket Mock</summary>
|
|
844
1185
|
|
|
845
1186
|
``` ts
|
|
846
1187
|
// ws.mock.ts
|
|
@@ -873,7 +1214,7 @@ export default defineMock({
|
|
|
873
1214
|
|
|
874
1215
|
``` ts
|
|
875
1216
|
// app.ts
|
|
876
|
-
const ws = new WebSocket('ws://localhost:
|
|
1217
|
+
const ws = new WebSocket('ws://localhost:5173/socket.io')
|
|
877
1218
|
ws.addEventListener('open', () => {
|
|
878
1219
|
setInterval(() => {
|
|
879
1220
|
// heartbeat
|
|
@@ -885,7 +1226,10 @@ ws.addEventListener('message', (raw) => {
|
|
|
885
1226
|
})
|
|
886
1227
|
```
|
|
887
1228
|
|
|
888
|
-
|
|
1229
|
+
</details>
|
|
1230
|
+
|
|
1231
|
+
<details>
|
|
1232
|
+
<summary>EventSource Mock</summary>
|
|
889
1233
|
|
|
890
1234
|
```ts
|
|
891
1235
|
// sse.mock.ts
|
|
@@ -919,6 +1263,8 @@ es.addEventListener('count', (e) => {
|
|
|
919
1263
|
})
|
|
920
1264
|
```
|
|
921
1265
|
|
|
1266
|
+
</details>
|
|
1267
|
+
|
|
922
1268
|
## Mock Services
|
|
923
1269
|
|
|
924
1270
|
In some scenarios, it may be necessary to use the data provided by mock services for display purposes, but the project may have already been packaged, built and deployed without support from `rspack/rsbuild` and this plugin's mock service. Since this plugin supports importing various node modules in mock files at the design stage, the mock file cannot be inline into client build code.
|