rspack-plugin-mock 0.2.0 → 0.3.1

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,16 +1,20 @@
1
1
  # rspack-plugin-mock
2
2
 
3
- [rspack](https://rspack.dev) and [rsbuild](https://rsbuild.dev) plugin for API mock dev server.
3
+ [Rspack](https://rspack.dev) and [Rsbuild](https://rsbuild.dev) plugin for API mock dev server.
4
4
 
5
5
  Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent with [vite-plugin-mock-dev-server](https://github.com/pengzhanbo/vite-plugin-mock-dev-server).
6
6
 
7
7
  <p align="center">
8
- <a href="https://www.npmjs.com/package/rspack-plugin-mock"><img alt="npm" src="https://img.shields.io/npm/v/rspack-plugin-mock?style=flat-square"></a>
9
- <img alt="node-current" src="https://img.shields.io/node/v/rspack-plugin-mock?style=flat-square">
10
- <img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rspack/core?style=flat-square">
11
- <img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/rspack-plugin-mock/peer/@rsbuild/core?style=flat-square">
12
- <img alt="npm" src="https://img.shields.io/npm/dm/rspack-plugin-mock?style=flat-square">
13
- <img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/rspack-plugin-mock/lint.yml?style=flat-square">
8
+ <a href="https://www.npmjs.com/package/rspack-plugin-mock"><img alt="npm" src="https://img.shields.io/npm/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91"></a>
9
+ <img alt="node-current" src="https://img.shields.io/node/v/rspack-plugin-mock?style=flat-square&colorA=564341&colorB=EDED91">
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">
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">
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/lint.yml?style=flat-square&colorA=564341&colorB=EDED91">
14
+ </p>
15
+
16
+ <p align="center">
17
+ <span>English</span> | <a href="./README.zh-CN.md">简体中文</a>
14
18
  </p>
15
19
 
16
20
  ## Features
@@ -20,7 +24,7 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
20
24
  - 💡 ESModule/commonjs.
21
25
  - 🦾 Typescript.
22
26
  - 🔥 HMR
23
- - 🏷 Support `.[cm]js`/ `.ts` /`.json` / `.json5`.
27
+ - 🏷 Support `.[cm]?js`/ `.ts` /`.json` / `.json5`.
24
28
  - 📦 Auto import mock file.
25
29
  - 🎨 Support any lib, like `mockjs`, or do not use it.
26
30
  - 📥 Path rule matching, request parameter matching.
@@ -32,10 +36,9 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
32
36
  - 📤 Support `multipart` content-type, mock upload file.
33
37
  - 📥 Support mock download file.
34
38
  - ⚜️ Support `WebSocket Mock`
39
+ - 🗂 Support building small independent deployable mock services.
35
40
 
36
- ## Usage
37
-
38
- ### Install
41
+ ## Install
39
42
 
40
43
  ```sh
41
44
  # npm
@@ -46,7 +49,7 @@ yarn add rspack-plugin-mock
46
49
  pnpm add -D rspack-plugin-mock
47
50
  ```
48
51
 
49
- ### Configuration
52
+ ### Usage
50
53
 
51
54
  **In Rspack**
52
55
 
@@ -89,7 +92,7 @@ export default defineConfig({
89
92
 
90
93
  ### Edit Mock file
91
94
 
92
- By default, write mock data in the mock directory of your project's root directory:
95
+ By default, write mock data in the `mock` directory of your project's root directory:
93
96
 
94
97
  `mock/**/*.mock.ts` :
95
98
 
@@ -278,6 +281,36 @@ export default definePostMock({
278
281
 
279
282
  Configure to [co-body](https://github.com/cojs/co-body#options)
280
283
 
284
+ ## options.build
285
+
286
+ - **Type:** `boolean | ServerBuildOption`
287
+
288
+ ```ts
289
+ interface ServerBuildOption {
290
+ /**
291
+ * Service startup port
292
+ * @default 8080
293
+ */
294
+ serverPort?: number
295
+ /**
296
+ * Service application output directory
297
+ * @default 'mockServer'
298
+ */
299
+ dist?: string
300
+
301
+ /**
302
+ * Service application log level
303
+ * @default 'error'
304
+ */
305
+ log?: LogLevel
306
+ }
307
+ ```
308
+
309
+ - **Default:** `false`
310
+ - **Details:**
311
+
312
+ When you need to build a small mock service, you can configure this option.
313
+
281
314
  ## Mock Options
282
315
 
283
316
  ### options.url
@@ -393,6 +426,47 @@ export default definePostMock({
393
426
 
394
427
  Configure response body cookies
395
428
 
429
+ ### options.validator
430
+
431
+ - **Type:** `Validator | (request: MockRequest) => boolean`
432
+
433
+ ```ts
434
+ interface Validator {
435
+ /**
436
+ * The query string located after `?` in the request address has been parsed into JSON.
437
+ */
438
+ query: Record<string, any>
439
+ /**
440
+ * The queryString located after `?` in the referer request has been parsed as JSON.
441
+ */
442
+ refererQuery: Record<string, any>
443
+ /**
444
+ * Body data in the request
445
+ */
446
+ body: Record<string, any>
447
+ /**
448
+ * The params parameter parsed from the `/api/id/:id` in the request address.
449
+ */
450
+ params: Record<string, any>
451
+ /**
452
+ * headers data in the request
453
+ */
454
+ headers: Headers
455
+ }
456
+ ```
457
+
458
+ - **Details:**
459
+
460
+ Request Validator
461
+
462
+ Sometimes, for the same API request, data needs to be returned based
463
+ on different request parameters.
464
+
465
+ However, if all of this is written in a single mock's body or response,
466
+ the content can become cumbersome and difficult to manage.
467
+ The function of a validator allows you to configure multiple mocks with
468
+ the same URL simultaneously and determine which mock should be used through validation.
469
+
396
470
  ### options.ws
397
471
 
398
472
  - **Type:** `boolean`
@@ -786,7 +860,28 @@ ws.addEventListener('message', (raw) => {
786
860
  })
787
861
  ```
788
862
 
789
- ## Redirect
863
+ ## Mock Services
864
+
865
+ 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.
866
+
867
+ The plugin support for builds a small independent mock service application that can be deployed to relevant environments during `production build`. This can then be forwarded through other HTTP servers like Nginx to actual ports for mock support.
868
+
869
+ The default output is built into the directory dist/mockServer, generating files as follows:
870
+
871
+ ```sh
872
+ ./mockServer
873
+ ├── index.js
874
+ ├── mock-data.js
875
+ └── package.json
876
+ ```
877
+
878
+ In this directory, execute `npm install` to install dependencies, and then execute npm start to start the mock server.
879
+
880
+ The default port is `8080`.
881
+
882
+ You can access related mock interfaces through `localhost:8080/`.
883
+
884
+ ## Links
790
885
 
791
886
  - [rspack](https://rspack.dev)
792
887
  - [rsbuild](https://rsbuild.dev)
@@ -794,4 +889,4 @@ ws.addEventListener('message', (raw) => {
794
889
 
795
890
  ## License
796
891
 
797
- [MIT License](/LICENSE)
892
+ rspack-plugin-mock is licensed under the [MIT License](./LICENSE)