rspack-plugin-mock 0.1.0 → 0.2.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 CHANGED
@@ -4,6 +4,15 @@
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
+ <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">
14
+ </p>
15
+
7
16
  ## Features
8
17
 
9
18
  - ⚡️ Lightweight, Flexible, Fast.
@@ -18,10 +27,11 @@ Implement a mock-dev-server in `rspack` and `rsbuild` that is fully consistent w
18
27
  - ⚙️ Support Enabled/Disabled any one of the API mock.
19
28
  - 📀 Supports response body content type such as `text/json/buffer/stream`.
20
29
  - ⚖️ Use `devServer.proxy` in rspack, or `server.proxy` in rsbuild.
21
- - 🍕 Support `viteConfig.define` and `env` in the mock file.
22
- - ⚓️ Support `viteConfig.resolve.alias` in the mock file.
30
+ - 🍕 Support `define` in the mock file.
31
+ - ⚓️ Support `alias` in the mock file.
23
32
  - 📤 Support `multipart` content-type, mock upload file.
24
33
  - 📥 Support mock download file.
34
+ - ⚜️ Support `WebSocket Mock`
25
35
 
26
36
  ## Usage
27
37
 
@@ -162,6 +172,8 @@ export default defineMock({
162
172
  Return a custom defineMock function to support preprocessing of mock config.
163
173
 
164
174
  ```ts
175
+ import { createDefineMock } from 'rspack-plugin-mock/helper'
176
+
165
177
  const definePostMock = createDefineMock((mock) => {
166
178
  mock.url = `/api/post/${mock.url}`
167
179
  })
@@ -183,6 +195,18 @@ export default definePostMock({
183
195
  any request path starting with prefix will be intercepted and proxied.
184
196
  If the prefix starts with `^`, it will be recognized as a `RegExp`.
185
197
 
198
+ ### options.wsPrefix
199
+
200
+ - **Type:** `string | string[]`
201
+ - **Details:**
202
+
203
+ Configure path matching rules for WebSocket mock service.
204
+ Any ws/wss requests with a request path starting with wsPrefix
205
+ will be intercepted by the proxy.
206
+ If wsPrefix starts with `^`, it will be recognized as a `RegExp`.
207
+
208
+ Please avoid having the configurations in `wsPrefix` appear in `devServer.proxy` / `server.proxy`, as this may lead to conflicts in the rules.
209
+
186
210
  ### options.cwd
187
211
 
188
212
  - **Type:** `string`
@@ -215,6 +239,15 @@ export default definePostMock({
215
239
 
216
240
  Enable log and configure log level
217
241
 
242
+ ### options.reload
243
+
244
+ - **Type:** `boolean`
245
+ - **Default:** `false`
246
+ - **Details:**
247
+
248
+ If you want to refresh the page every time you modify a mock file,
249
+ you can open this option.
250
+
218
251
  ### options.cors
219
252
 
220
253
  - **Type:** `boolean | CorsOptions`
@@ -266,15 +299,16 @@ export default definePostMock({
266
299
  ### options.method
267
300
 
268
301
  - **Type:** `Method | Method[]`
302
+
303
+ ```ts
304
+ type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH'
305
+ ```
306
+
269
307
  - **Default:** `['GET', 'POST']`
270
308
  - **Details:**
271
309
 
272
310
  The interface allows request methods
273
311
 
274
- ```ts
275
- type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH'
276
- ```
277
-
278
312
  ### options.type
279
313
 
280
314
  - **Type:** `'text' | 'json' | 'buffer' | string`
@@ -349,15 +383,43 @@ type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH'
349
383
 
350
384
  - **Type:** `CookiesOptions | (request: MockRequest) => CookiesOptions | Promise<CookiesOptions>`
351
385
 
352
- ```ts
353
- type CookiesOptions = Record<string, CookieValue>
354
- type CookieValue = string | [string, SetCookie]
355
- ```
386
+ ```ts
387
+ type CookiesOptions = Record<string, CookieValue>
388
+
389
+ type CookieValue = string | [string, SetCookie]
390
+ ```
356
391
 
357
392
  - **Details:**
358
393
 
359
394
  Configure response body cookies
360
395
 
396
+ ### options.ws
397
+
398
+ - **Type:** `boolean`
399
+ - **Default:** `false`
400
+ - **Details:**
401
+
402
+ Enable WebSocket interface simulation
403
+
404
+ ### options.setup
405
+
406
+ - **Type:** `(wss: WebSocketServer, ctx: WebSocketSetupContext) => void`
407
+ - **Details:**
408
+
409
+ Configure Websocket Server
410
+
411
+ ```ts
412
+ interface WebSocketSetupContext {
413
+ /**
414
+ * When defining WSS, you may perform some automatic or looping tasks.
415
+ * However, when hot updating, the plugin will re-execute `setup()`,
416
+ * which may result in duplicate registration of listening events and looping tasks
417
+ * such as setTimeout. You can use `onCleanup()` to clear these automatic or looping tasks.
418
+ */
419
+ onCleanup: (cleanup: () => void) => void
420
+ }
421
+ ```
422
+
361
423
  ### Types
362
424
 
363
425
  ```ts
@@ -679,6 +741,51 @@ fetch('/api/graphql', {
679
741
  })
680
742
  ```
681
743
 
744
+ **exp:** WebSocket Mock
745
+
746
+ ``` ts
747
+ // ws.mock.ts
748
+ export default defineMock({
749
+ url: '/socket.io',
750
+ ws: true,
751
+ setup(wss, { onCleanup }) {
752
+ const wsMap = new Map()
753
+ wss.on('connection', (ws, req) => {
754
+ const token = req.getCookie('token')
755
+ wsMap.set(token, ws)
756
+ ws.on('message', (raw) => {
757
+ const data = JSON.parse(String(raw))
758
+ if (data.type === 'ping')
759
+ return
760
+ // Broadcast
761
+ for (const [_token, _ws] of wsMap.entires()) {
762
+ if (_token !== token)
763
+ _ws.send(raw)
764
+ }
765
+ })
766
+ })
767
+ wss.on('error', (err) => {
768
+ console.error(err)
769
+ })
770
+ onCleanup(() => wsMap.clear())
771
+ }
772
+ })
773
+ ```
774
+
775
+ ``` ts
776
+ // app.ts
777
+ const ws = new WebSocket('ws://localhost:3000/socket.io')
778
+ ws.addEventListener('open', () => {
779
+ setInterval(() => {
780
+ // heartbeat
781
+ ws.send(JSON.stringify({ type: 'ping' }))
782
+ }, 1000)
783
+ }, { once: true })
784
+ ws.addEventListener('message', (raw) => {
785
+ console.log(raw)
786
+ })
787
+ ```
788
+
682
789
  ## Redirect
683
790
 
684
791
  - [rspack](https://rspack.dev)