vite-plugin-mock-dev-server 1.3.4-beta.1 → 1.4.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 +49 -47
- package/README.zh-CN.md +28 -27
- package/dist/index.cjs +1673 -27
- package/dist/index.d.cts +35 -9
- package/dist/index.d.ts +35 -9
- package/dist/index.js +1644 -27
- package/package.json +46 -50
package/README.md
CHANGED
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
|
|
50
50
|
## Documentation
|
|
51
51
|
|
|
52
|
-
See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/) to learn more.
|
|
52
|
+
See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/en/) to learn more.
|
|
53
53
|
|
|
54
54
|
[](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
|
|
55
55
|
|
|
@@ -287,16 +287,16 @@ export default defineMock({
|
|
|
287
287
|
* Supported request methods of the interface.
|
|
288
288
|
* @type string | string[]
|
|
289
289
|
* @default ['POST','GET']
|
|
290
|
-
*
|
|
290
|
+
*
|
|
291
291
|
*/
|
|
292
292
|
method: ['GET', 'POST'],
|
|
293
293
|
/**
|
|
294
|
-
* In practical scenarios,
|
|
295
|
-
* we usually only need certain mock interfaces to take effect,
|
|
296
|
-
* rather than enabling all mock interfaces.
|
|
297
|
-
* For interfaces that do not currently require mocking,
|
|
294
|
+
* In practical scenarios,
|
|
295
|
+
* we usually only need certain mock interfaces to take effect,
|
|
296
|
+
* rather than enabling all mock interfaces.
|
|
297
|
+
* For interfaces that do not currently require mocking,
|
|
298
298
|
* they can be set to false.
|
|
299
|
-
*
|
|
299
|
+
*
|
|
300
300
|
* @default true
|
|
301
301
|
*/
|
|
302
302
|
enabled: true,
|
|
@@ -350,7 +350,7 @@ export default defineMock({
|
|
|
350
350
|
|
|
351
351
|
/**
|
|
352
352
|
* Response Body
|
|
353
|
-
* Support `string/number/array/object/buffer/ReadableStream`
|
|
353
|
+
* Support `string/number/array/object/buffer/ReadableStream`
|
|
354
354
|
* You can also use libraries such as' mockjs' to generate data content
|
|
355
355
|
* @type string | number | array | object | ReadableStream | buffer
|
|
356
356
|
* @type (request: { headers, query, body, params, refererQuery, getCookie }) => any | Promise<any>
|
|
@@ -358,33 +358,33 @@ export default defineMock({
|
|
|
358
358
|
body: '',
|
|
359
359
|
|
|
360
360
|
/**
|
|
361
|
-
* If the mock requirement cannot be solved through `body` configuration,
|
|
362
|
-
* then it can be achieved by configuring response and exposing the interface of http server
|
|
363
|
-
* to realize fully controllable custom configuration in req parameters.
|
|
364
|
-
* The parsing of query, body and params has been built-in, so you can use them directly.
|
|
361
|
+
* If the mock requirement cannot be solved through `body` configuration,
|
|
362
|
+
* then it can be achieved by configuring response and exposing the interface of http server
|
|
363
|
+
* to realize fully controllable custom configuration in req parameters.
|
|
364
|
+
* The parsing of query, body and params has been built-in, so you can use them directly.
|
|
365
365
|
* Don't forget to return response data through `res.end()` or skip mock by calling `next()`.
|
|
366
366
|
*/
|
|
367
367
|
response(req, res, next) {
|
|
368
368
|
res.end()
|
|
369
369
|
},
|
|
370
370
|
/**
|
|
371
|
-
* Request validator, return mock data if validated, otherwise do not use current mock.
|
|
372
|
-
* This is useful in scenarios where an interface needs to return different data based
|
|
373
|
-
* on different input parameters.
|
|
374
|
-
* Validators can solve this type of problem well by dividing the same URL into multiple
|
|
371
|
+
* Request validator, return mock data if validated, otherwise do not use current mock.
|
|
372
|
+
* This is useful in scenarios where an interface needs to return different data based
|
|
373
|
+
* on different input parameters.
|
|
374
|
+
* Validators can solve this type of problem well by dividing the same URL into multiple
|
|
375
375
|
* mock configurations and determining which one is effective based on the validator.
|
|
376
|
-
*
|
|
376
|
+
*
|
|
377
377
|
* @type { headers, body, query, params, refererQuery }
|
|
378
|
-
* If the validator passed in is an object,
|
|
379
|
-
* then the validation method is to deeply compare whether
|
|
380
|
-
* `headers/body/query/params/refererQuery` of the requested interface contain
|
|
378
|
+
* If the validator passed in is an object,
|
|
379
|
+
* then the validation method is to deeply compare whether
|
|
380
|
+
* `headers/body/query/params/refererQuery` of the requested interface contain
|
|
381
381
|
* the `key-value` of the validator.
|
|
382
|
-
*
|
|
382
|
+
*
|
|
383
383
|
* @type (request) => boolean
|
|
384
|
-
* If the validator passed in is a function,
|
|
385
|
-
* then the data related to the requested interface will be provided as input parameters
|
|
384
|
+
* If the validator passed in is a function,
|
|
385
|
+
* then the data related to the requested interface will be provided as input parameters
|
|
386
386
|
* for users to perform custom validation and return a boolean.
|
|
387
|
-
*
|
|
387
|
+
*
|
|
388
388
|
*/
|
|
389
389
|
validator: {
|
|
390
390
|
headers: {},
|
|
@@ -392,8 +392,8 @@ export default defineMock({
|
|
|
392
392
|
query: {},
|
|
393
393
|
params: {},
|
|
394
394
|
/**
|
|
395
|
-
* refererQuery validates the query parameters in the URL of the request source page,
|
|
396
|
-
* which allows for direct modification of parameters in the browser address bar
|
|
395
|
+
* refererQuery validates the query parameters in the URL of the request source page,
|
|
396
|
+
* which allows for direct modification of parameters in the browser address bar
|
|
397
397
|
* to obtain different simulated data.
|
|
398
398
|
*/
|
|
399
399
|
refererQuery: {}
|
|
@@ -422,7 +422,7 @@ export default defineMock({
|
|
|
422
422
|
* `onCleanup()` to clear these tasks.
|
|
423
423
|
* This is because when the plugin is hot updated,
|
|
424
424
|
* it needs to re-execute setup and clear previous tasks; otherwise,
|
|
425
|
-
* duplicate tasks may cause conflicts.
|
|
425
|
+
* duplicate tasks may cause conflicts.
|
|
426
426
|
* `onCleanup()` can be called multiple times within setup.
|
|
427
427
|
* @type `(wss: WebSocketServer, context: SetupContext) => void`
|
|
428
428
|
*/
|
|
@@ -512,7 +512,7 @@ export default defineMock([
|
|
|
512
512
|
url: '/api/posts/delete/:id',
|
|
513
513
|
body: (params) => {
|
|
514
514
|
const id = params.id
|
|
515
|
-
posts.value = posts.value.filter(
|
|
515
|
+
posts.value = posts.value.filter(post => post.id !== id)
|
|
516
516
|
return { success: true }
|
|
517
517
|
}
|
|
518
518
|
}
|
|
@@ -554,10 +554,10 @@ export default defineConfig({
|
|
|
554
554
|
'/api/:a/:b/c': {
|
|
555
555
|
rules: ['/api/a/:b/:c', '/api/a/b/:c'],
|
|
556
556
|
when: ['/api/a/b/c']
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
557
|
+
},
|
|
558
|
+
// 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]`
|
|
559
|
+
'/api/:a/b': ['/api/a/:b'],
|
|
560
|
+
}
|
|
561
561
|
}
|
|
562
562
|
})
|
|
563
563
|
]
|
|
@@ -782,18 +782,18 @@ export default defineMock({
|
|
|
782
782
|
use [`formidable`](https://www.npmjs.com/package/formidable#readme) to support.
|
|
783
783
|
``` html
|
|
784
784
|
<form action="/api/upload" method="post" enctype="multipart/form-data">
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
785
|
+
<p>
|
|
786
|
+
<span>file: </span>
|
|
787
|
+
<input type="file" name="files" multiple>
|
|
788
|
+
</p>
|
|
789
|
+
<p>
|
|
790
|
+
<span>name:</span>
|
|
791
|
+
<input type="text" name="name" value="mark">
|
|
792
|
+
</p>
|
|
793
|
+
<p>
|
|
794
|
+
<input type="submit" value="submit">
|
|
795
|
+
</p>
|
|
796
|
+
</form>
|
|
797
797
|
```
|
|
798
798
|
|
|
799
799
|
fields `files` mapping to `formidable.File`
|
|
@@ -836,7 +836,7 @@ export default defineMock({
|
|
|
836
836
|
``` ts
|
|
837
837
|
fetch('/api/graphql', {
|
|
838
838
|
method: 'POST',
|
|
839
|
-
body: JSON.stringify({ source: '{ hello }' })
|
|
839
|
+
body: JSON.stringify({ source: '{ hello }' })
|
|
840
840
|
})
|
|
841
841
|
```
|
|
842
842
|
|
|
@@ -853,7 +853,8 @@ export default defineMock({
|
|
|
853
853
|
wsMap.set(token, ws)
|
|
854
854
|
ws.on('message', (raw) => {
|
|
855
855
|
const data = JSON.parse(String(raw))
|
|
856
|
-
if (data.type === 'ping')
|
|
856
|
+
if (data.type === 'ping')
|
|
857
|
+
return
|
|
857
858
|
// Broadcast
|
|
858
859
|
for (const [_token, _ws] of wsMap.entires()) {
|
|
859
860
|
if (_token !== token)
|
|
@@ -909,7 +910,7 @@ You can access related `mock` interfaces through `localhost:8080/`.
|
|
|
909
910
|
## Contributors
|
|
910
911
|
|
|
911
912
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
912
|
-
[](#contributors-)
|
|
913
914
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
914
915
|
|
|
915
916
|
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
|
|
@@ -921,6 +922,7 @@ You can access related `mock` interfaces through `localhost:8080/`.
|
|
|
921
922
|
<td align="center" valign="top" width="14.28%"><a href="https://pengzhanbo.cn"><img src="https://avatars.githubusercontent.com/u/16745751?v=4?s=100" width="100px;" alt="pengzhanbo"/><br /><sub><b>pengzhanbo</b></sub></a><br /><a href="https://github.com/pengzhanbo/vite-plugin-mock-dev-server/commits?author=pengzhanbo" title="Documentation">📖</a> <a href="#ideas-pengzhanbo" title="Ideas, Planning, & Feedback">🤔</a> <a href="#example-pengzhanbo" title="Examples">💡</a> <a href="https://github.com/pengzhanbo/vite-plugin-mock-dev-server/commits?author=pengzhanbo" title="Code">💻</a></td>
|
|
922
923
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jiadesen"><img src="https://avatars.githubusercontent.com/u/22772994?v=4?s=100" width="100px;" alt="jiadesen"/><br /><sub><b>jiadesen</b></sub></a><br /><a href="#ideas-jiadesen" title="Ideas, Planning, & Feedback">🤔</a> <a href="https://github.com/pengzhanbo/vite-plugin-mock-dev-server/issues?q=author%3Ajiadesen" title="Bug reports">🐛</a></td>
|
|
923
924
|
<td align="center" valign="top" width="14.28%"><a href="https://github.com/yogibaba"><img src="https://avatars.githubusercontent.com/u/152670?v=4?s=100" width="100px;" alt="yogibaba"/><br /><sub><b>yogibaba</b></sub></a><br /><a href="https://github.com/pengzhanbo/vite-plugin-mock-dev-server/commits?author=yogibaba" title="Code">💻</a></td>
|
|
925
|
+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/pfdgithub"><img src="https://avatars.githubusercontent.com/u/3262762?v=4?s=100" width="100px;" alt="pfdgithub"/><br /><sub><b>pfdgithub</b></sub></a><br /><a href="https://github.com/pengzhanbo/vite-plugin-mock-dev-server/commits?author=pfdgithub" title="Code">💻</a> <a href="https://github.com/pengzhanbo/vite-plugin-mock-dev-server/issues?q=author%3Apfdgithub" title="Bug reports">🐛</a></td>
|
|
924
926
|
</tr>
|
|
925
927
|
</tbody>
|
|
926
928
|
</table>
|
package/README.zh-CN.md
CHANGED
|
@@ -282,7 +282,7 @@ export default defineMock({
|
|
|
282
282
|
* 接口支持的请求方法
|
|
283
283
|
* @type string | string[]
|
|
284
284
|
* @default ['POST','GET']
|
|
285
|
-
*
|
|
285
|
+
*
|
|
286
286
|
*/
|
|
287
287
|
method: ['GET', 'POST'],
|
|
288
288
|
/**
|
|
@@ -345,10 +345,10 @@ export default defineMock({
|
|
|
345
345
|
* 定义返回的响应体数据内容。
|
|
346
346
|
* 在这里,你可以直接返回JavaScript支持的数据类型如 `string/number/array/object` 等
|
|
347
347
|
* 同时,你也可以使用如 `mockjs` 等库来生成数据内容
|
|
348
|
-
*
|
|
348
|
+
*
|
|
349
349
|
* @type string | number | array | object
|
|
350
350
|
* 直接返回定义的数据
|
|
351
|
-
*
|
|
351
|
+
*
|
|
352
352
|
* @type (request: { headers, query, body, params }) => any | Promise<any>
|
|
353
353
|
* 如果传入一个函数,那么可以更加灵活的定义返回响应体数据
|
|
354
354
|
*/
|
|
@@ -370,11 +370,11 @@ export default defineMock({
|
|
|
370
370
|
* 请求验证器,通过验证器则返回 mock数据,否则不使用当前mock。
|
|
371
371
|
* 这对于一些场景中,某个接口需要通过不同的入参来返回不同的数据,验证器可以很好的解决这一类问题,
|
|
372
372
|
* 将同个 url 分为多个 mock配置,根据 验证器来判断哪个mock配置生效。
|
|
373
|
-
*
|
|
373
|
+
*
|
|
374
374
|
* @type { headers, body, query, params, refererQuery }
|
|
375
375
|
* 如果 validator 传入的是一个对象,那么验证方式是 深度比较 请求的接口
|
|
376
376
|
* 中 headers/body/query/params/refererQuery 是否包含 validator 的 key-value。
|
|
377
|
-
*
|
|
377
|
+
*
|
|
378
378
|
* @type (request) => boolean
|
|
379
379
|
* 如果 validator 传入的是一个函数,那么会将 请求的接口相关数据作为入参,
|
|
380
380
|
* 提供给使用者进行自定义校验,并返回一个 boolean
|
|
@@ -414,7 +414,7 @@ export default defineMock({
|
|
|
414
414
|
* 那么需要在 `onCleanup()` 传入一个回调,用于清除这些任务,
|
|
415
415
|
* 这是由于插件在热更新时,需要重新执行 setup,需要清除之前的任务,否则可能会导致重复任务产生冲突。
|
|
416
416
|
* `onCleanup()`可以在 setup 内部多次调用
|
|
417
|
-
* @type `(wss: WebSocketServer, context: SetupContext) => void`
|
|
417
|
+
* @type `(wss: WebSocketServer, context: SetupContext) => void`
|
|
418
418
|
*/
|
|
419
419
|
setup(wss, { onCleanup }) {
|
|
420
420
|
wss.on('connection', (ws, request) => {
|
|
@@ -502,7 +502,7 @@ export default defineMock([
|
|
|
502
502
|
url: '/api/posts/delete/:id',
|
|
503
503
|
body: (params) => {
|
|
504
504
|
const id = params.id
|
|
505
|
-
posts.value = posts.value.filter(
|
|
505
|
+
posts.value = posts.value.filter(post => post.id !== id)
|
|
506
506
|
return { success: true }
|
|
507
507
|
}
|
|
508
508
|
}
|
|
@@ -542,11 +542,11 @@ export default defineConfig({
|
|
|
542
542
|
'/api/:a/:b/c': {
|
|
543
543
|
rules: ['/api/a/:b/:c', '/api/a/b/:c'],
|
|
544
544
|
when: ['/api/a/b/c']
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
545
|
+
},
|
|
546
|
+
// 如果不需要 when, 则表示命中规则的请求都需要调整优先级。
|
|
547
|
+
// 可以简写为 [key]: [...rules]
|
|
548
|
+
'/api/:a/b': ['/api/a/:b'],
|
|
549
|
+
}
|
|
550
550
|
}
|
|
551
551
|
})
|
|
552
552
|
]
|
|
@@ -694,7 +694,7 @@ export default defineMock({
|
|
|
694
694
|
export default defineMock({
|
|
695
695
|
url: 'api/buffer',
|
|
696
696
|
type: 'buffer',
|
|
697
|
-
// 内部使用 Buffer.from(body) 进行转换
|
|
697
|
+
// 内部使用 Buffer.from(body) 进行转换
|
|
698
698
|
body: { a: 1 }
|
|
699
699
|
})
|
|
700
700
|
```
|
|
@@ -768,18 +768,18 @@ export default defineMock({
|
|
|
768
768
|
通过 [`formidable`](https://www.npmjs.com/package/formidable#readme) 支持。
|
|
769
769
|
``` html
|
|
770
770
|
<form action="/api/upload" method="post" enctype="multipart/form-data">
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
771
|
+
<p>
|
|
772
|
+
<span>file: </span>
|
|
773
|
+
<input type="file" name="files" multiple>
|
|
774
|
+
</p>
|
|
775
|
+
<p>
|
|
776
|
+
<span>name:</span>
|
|
777
|
+
<input type="text" name="name" value="mark">
|
|
778
|
+
</p>
|
|
779
|
+
<p>
|
|
780
|
+
<input type="submit" value="submit">
|
|
781
|
+
</p>
|
|
782
|
+
</form>
|
|
783
783
|
```
|
|
784
784
|
|
|
785
785
|
fields `files` 映射为 `formidable.File` 类型。
|
|
@@ -822,7 +822,7 @@ export default defineMock({
|
|
|
822
822
|
```ts
|
|
823
823
|
fetch('/api/graphql', {
|
|
824
824
|
method: 'POST',
|
|
825
|
-
body: JSON.stringify({ source: '{ hello }' })
|
|
825
|
+
body: JSON.stringify({ source: '{ hello }' })
|
|
826
826
|
})
|
|
827
827
|
```
|
|
828
828
|
|
|
@@ -839,7 +839,8 @@ export default defineMock({
|
|
|
839
839
|
wsMap.set(token, ws)
|
|
840
840
|
ws.on('message', (raw) => {
|
|
841
841
|
const data = JSON.parse(String(raw))
|
|
842
|
-
if (data.type === 'ping')
|
|
842
|
+
if (data.type === 'ping')
|
|
843
|
+
return
|
|
843
844
|
// Broadcast
|
|
844
845
|
for (const [_token, _ws] of wsMap.entires()) {
|
|
845
846
|
if (_token !== token)
|