vite-plugin-mock-dev-server 0.3.15 → 0.3.16
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 +9 -3
- package/README.zh-CN.md +20 -4
- package/dist/index.cjs +37 -8
- package/dist/index.js +37 -8
- package/package.json +1 -2
package/README.md
CHANGED
|
@@ -37,11 +37,17 @@
|
|
|
37
37
|
- ⚖️ Use `server.proxy`
|
|
38
38
|
- 🍕 Support `viteConfig.define` in mock file
|
|
39
39
|
- 📤 Support `multipart` content-type,mock upload file.
|
|
40
|
+
- 🌈 Support `vite preview` mode.
|
|
40
41
|
## Documentation
|
|
41
42
|
|
|
42
43
|
See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/) to learn more.
|
|
44
|
+
|
|
43
45
|
[](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
|
|
44
46
|
|
|
47
|
+
## Playground
|
|
48
|
+
|
|
49
|
+
[](https://stackblitz.com/github/pengzhanbo/vite-plugin-mock-dev-server/tree/main/playground)
|
|
50
|
+
|
|
45
51
|
## Usage
|
|
46
52
|
|
|
47
53
|
### Install
|
|
@@ -221,14 +227,14 @@ export default defineMock({
|
|
|
221
227
|
* configurations and determines which mock configuration
|
|
222
228
|
* is valid according to the validator.
|
|
223
229
|
*
|
|
224
|
-
* @type {
|
|
230
|
+
* @type { headers?: object; body?: object; query?: object; params?: object }
|
|
225
231
|
*
|
|
226
232
|
* If the validator incoming is an object,
|
|
227
233
|
* then the validation method is the comparison of the
|
|
228
234
|
* strict request of interface, headers/body/query/params
|
|
229
235
|
* each `key-value` congruent, congruent check through
|
|
230
236
|
*
|
|
231
|
-
* @type ({
|
|
237
|
+
* @type ({ headers: object; body: object; query: object; params: object }) => boolean
|
|
232
238
|
* If the validator is passed a function,
|
|
233
239
|
* it takes the requested interface-related data as an input,
|
|
234
240
|
* gives it to the consumer for custom validation,
|
|
@@ -260,7 +266,7 @@ export default defineMock({
|
|
|
260
266
|
*
|
|
261
267
|
* @type string | number | array | object
|
|
262
268
|
*
|
|
263
|
-
* @type (request: {
|
|
269
|
+
* @type (request: { headers, query, body, params }) => any | Promise<any>
|
|
264
270
|
*/
|
|
265
271
|
body: {},
|
|
266
272
|
|
package/README.zh-CN.md
CHANGED
|
@@ -38,12 +38,18 @@
|
|
|
38
38
|
- ⚖️ 使用 `server.proxy` 配置
|
|
39
39
|
- 🍕 支持在 mock文件中使用 `viteConfig.define`配置字段
|
|
40
40
|
- 📤 支持 multipart 类型,模拟文件上传
|
|
41
|
+
- 🌈 支持 `vite preview` 模式.
|
|
41
42
|
|
|
42
43
|
|
|
43
44
|
## 文档
|
|
44
45
|
|
|
45
46
|
查看 [Documentation](https://vite-plugin-mock-dev-server.netlify.app/) 了解更多。
|
|
47
|
+
|
|
46
48
|
[](https://app.netlify.com/sites/vite-plugin-mock-dev-server/deploys)
|
|
49
|
+
|
|
50
|
+
## Playground
|
|
51
|
+
|
|
52
|
+
[](https://stackblitz.com/github/pengzhanbo/vite-plugin-mock-dev-server/tree/main/playground)
|
|
47
53
|
## 使用
|
|
48
54
|
|
|
49
55
|
### 安装
|
|
@@ -133,7 +139,17 @@ export default defineConfig({
|
|
|
133
139
|
|
|
134
140
|
配置读取 mock文件时,需要排除的文件, 可以是一个 目录、glob、或者一个数组
|
|
135
141
|
|
|
136
|
-
|
|
142
|
+
默认值:
|
|
143
|
+
```ts
|
|
144
|
+
[
|
|
145
|
+
'**/node_modules/**',
|
|
146
|
+
'**/test/**',
|
|
147
|
+
'src/**',
|
|
148
|
+
'**/.vscode/**',
|
|
149
|
+
'**/.git/**',
|
|
150
|
+
'**/dist/**'
|
|
151
|
+
]
|
|
152
|
+
```
|
|
137
153
|
|
|
138
154
|
- `options.formidableOptions`
|
|
139
155
|
|
|
@@ -212,13 +228,13 @@ export default defineMock({
|
|
|
212
228
|
* 验证器可以很好的解决这一类问题,将同个 url 分为多个 mock配置,
|
|
213
229
|
* 根据 验证器来判断哪个mock配置生效。
|
|
214
230
|
*
|
|
215
|
-
* @type {
|
|
231
|
+
* @type { headers?: object; body?: object; query?: object; params?: object }
|
|
216
232
|
*
|
|
217
233
|
* 如果 validator 传入的是一个对象,那么验证方式是严格比较 请求的接口
|
|
218
234
|
* 中,headers/body/query/params 的各个`key`的`value`是否全等,
|
|
219
235
|
* 全等则校验通过
|
|
220
236
|
*
|
|
221
|
-
* @type ({
|
|
237
|
+
* @type ({ headers: object; body: object; query: object; params: object }) => boolean
|
|
222
238
|
* 如果 validator 传入的是一个函数,那么会讲 请求的接口相关数据作为入参,提供给使用者进行自定义校验,并返回一个 boolean
|
|
223
239
|
*
|
|
224
240
|
*/
|
|
@@ -250,7 +266,7 @@ export default defineMock({
|
|
|
250
266
|
* @type string | number | array | object
|
|
251
267
|
* 直接返回定义的数据
|
|
252
268
|
*
|
|
253
|
-
* @type (request: {
|
|
269
|
+
* @type (request: { headers, query, body, params }) => any | Promise<any>
|
|
254
270
|
* 如果传入一个函数,那么可以更加灵活的定义返回响应体数据
|
|
255
271
|
*/
|
|
256
272
|
body: {},
|
package/dist/index.cjs
CHANGED
|
@@ -88,7 +88,7 @@ var _MockLoader = class extends import_node_events.default {
|
|
|
88
88
|
this.options = options;
|
|
89
89
|
this.moduleCache = /* @__PURE__ */ new Map();
|
|
90
90
|
this.moduleDeps = /* @__PURE__ */ new Map();
|
|
91
|
-
this._mockList =
|
|
91
|
+
this._mockList = {};
|
|
92
92
|
this.moduleType = "cjs";
|
|
93
93
|
this.cwd = options.cwd || process.cwd();
|
|
94
94
|
try {
|
|
@@ -183,9 +183,15 @@ var _MockLoader = class extends import_node_events.default {
|
|
|
183
183
|
for (const [, handle] of this.moduleCache.entries()) {
|
|
184
184
|
isArray(handle) ? mockList.push(...handle) : mockList.push(handle);
|
|
185
185
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
186
|
+
const mocks = {};
|
|
187
|
+
mockList.filter((mock) => mock.enabled || typeof mock.enabled === "undefined").forEach((mock) => {
|
|
188
|
+
if (!mocks[mock.url]) {
|
|
189
|
+
mocks[mock.url] = [];
|
|
190
|
+
}
|
|
191
|
+
const list = mocks[mock.url];
|
|
192
|
+
mock.validator ? list.unshift(mock) : list.push(mock);
|
|
193
|
+
});
|
|
194
|
+
this._mockList = mocks;
|
|
189
195
|
}
|
|
190
196
|
updateModuleDeps(filepath, deps) {
|
|
191
197
|
Object.keys(deps).forEach((mPath) => {
|
|
@@ -388,13 +394,20 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
388
394
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
389
395
|
const proxies = Object.keys(config.server.proxy || {});
|
|
390
396
|
return async function(req, res, next) {
|
|
391
|
-
if (proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) {
|
|
392
|
-
return next();
|
|
393
|
-
}
|
|
394
397
|
const method = req.method.toUpperCase();
|
|
395
398
|
const { query, pathname } = (0, import_node_url3.parse)(req.url, true);
|
|
399
|
+
if (!pathname || proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) {
|
|
400
|
+
return next();
|
|
401
|
+
}
|
|
402
|
+
const mockUrl = Object.keys(loader._mockList).find((key) => {
|
|
403
|
+
return (0, import_path_to_regexp.pathToRegexp)(key).test(pathname);
|
|
404
|
+
});
|
|
405
|
+
if (!mockUrl) {
|
|
406
|
+
return next();
|
|
407
|
+
}
|
|
408
|
+
const mockList = loader.mockList[mockUrl];
|
|
396
409
|
const reqBody = await parseReqBody(req, options.formidableOptions);
|
|
397
|
-
const currentMock =
|
|
410
|
+
const currentMock = mockList.find((mock) => {
|
|
398
411
|
if (!pathname || !mock || !mock.url)
|
|
399
412
|
return false;
|
|
400
413
|
const methods = mock.method ? isArray(mock.method) ? mock.method : [mock.method] : ["GET", "POST"];
|
|
@@ -479,6 +492,7 @@ function doesProxyContextMatchUrl(context, url) {
|
|
|
479
492
|
}
|
|
480
493
|
|
|
481
494
|
// src/plugin.ts
|
|
495
|
+
var viteConfig = {};
|
|
482
496
|
function mockDevServerPlugin({
|
|
483
497
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
484
498
|
exclude = [
|
|
@@ -495,6 +509,10 @@ function mockDevServerPlugin({
|
|
|
495
509
|
name: "vite-plugin-mock-dev-server",
|
|
496
510
|
enforce: "pre",
|
|
497
511
|
apply: "serve",
|
|
512
|
+
configResolved(config) {
|
|
513
|
+
viteConfig = config;
|
|
514
|
+
config.logger.warn("");
|
|
515
|
+
},
|
|
498
516
|
async configureServer({ middlewares, config, httpServer }) {
|
|
499
517
|
const middleware = await mockServerMiddleware(httpServer, config, {
|
|
500
518
|
include,
|
|
@@ -505,6 +523,17 @@ function mockDevServerPlugin({
|
|
|
505
523
|
}
|
|
506
524
|
});
|
|
507
525
|
middlewares.use(middleware);
|
|
526
|
+
},
|
|
527
|
+
async configurePreviewServer({ middlewares, httpServer }) {
|
|
528
|
+
const middleware = await mockServerMiddleware(httpServer, viteConfig, {
|
|
529
|
+
include,
|
|
530
|
+
exclude,
|
|
531
|
+
formidableOptions: {
|
|
532
|
+
multiples: true,
|
|
533
|
+
...formidableOptions
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
middlewares.use(middleware);
|
|
508
537
|
}
|
|
509
538
|
};
|
|
510
539
|
}
|
package/dist/index.js
CHANGED
|
@@ -53,7 +53,7 @@ var _MockLoader = class extends EventEmitter {
|
|
|
53
53
|
this.options = options;
|
|
54
54
|
this.moduleCache = /* @__PURE__ */ new Map();
|
|
55
55
|
this.moduleDeps = /* @__PURE__ */ new Map();
|
|
56
|
-
this._mockList =
|
|
56
|
+
this._mockList = {};
|
|
57
57
|
this.moduleType = "cjs";
|
|
58
58
|
this.cwd = options.cwd || process.cwd();
|
|
59
59
|
try {
|
|
@@ -148,9 +148,15 @@ var _MockLoader = class extends EventEmitter {
|
|
|
148
148
|
for (const [, handle] of this.moduleCache.entries()) {
|
|
149
149
|
isArray(handle) ? mockList.push(...handle) : mockList.push(handle);
|
|
150
150
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
const mocks = {};
|
|
152
|
+
mockList.filter((mock) => mock.enabled || typeof mock.enabled === "undefined").forEach((mock) => {
|
|
153
|
+
if (!mocks[mock.url]) {
|
|
154
|
+
mocks[mock.url] = [];
|
|
155
|
+
}
|
|
156
|
+
const list = mocks[mock.url];
|
|
157
|
+
mock.validator ? list.unshift(mock) : list.push(mock);
|
|
158
|
+
});
|
|
159
|
+
this._mockList = mocks;
|
|
154
160
|
}
|
|
155
161
|
updateModuleDeps(filepath, deps) {
|
|
156
162
|
Object.keys(deps).forEach((mPath) => {
|
|
@@ -353,13 +359,20 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
353
359
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
354
360
|
const proxies = Object.keys(config.server.proxy || {});
|
|
355
361
|
return async function(req, res, next) {
|
|
356
|
-
if (proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) {
|
|
357
|
-
return next();
|
|
358
|
-
}
|
|
359
362
|
const method = req.method.toUpperCase();
|
|
360
363
|
const { query, pathname } = urlParse(req.url, true);
|
|
364
|
+
if (!pathname || proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) {
|
|
365
|
+
return next();
|
|
366
|
+
}
|
|
367
|
+
const mockUrl = Object.keys(loader._mockList).find((key) => {
|
|
368
|
+
return pathToRegexp(key).test(pathname);
|
|
369
|
+
});
|
|
370
|
+
if (!mockUrl) {
|
|
371
|
+
return next();
|
|
372
|
+
}
|
|
373
|
+
const mockList = loader.mockList[mockUrl];
|
|
361
374
|
const reqBody = await parseReqBody(req, options.formidableOptions);
|
|
362
|
-
const currentMock =
|
|
375
|
+
const currentMock = mockList.find((mock) => {
|
|
363
376
|
if (!pathname || !mock || !mock.url)
|
|
364
377
|
return false;
|
|
365
378
|
const methods = mock.method ? isArray(mock.method) ? mock.method : [mock.method] : ["GET", "POST"];
|
|
@@ -444,6 +457,7 @@ function doesProxyContextMatchUrl(context, url) {
|
|
|
444
457
|
}
|
|
445
458
|
|
|
446
459
|
// src/plugin.ts
|
|
460
|
+
var viteConfig = {};
|
|
447
461
|
function mockDevServerPlugin({
|
|
448
462
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
449
463
|
exclude = [
|
|
@@ -460,6 +474,10 @@ function mockDevServerPlugin({
|
|
|
460
474
|
name: "vite-plugin-mock-dev-server",
|
|
461
475
|
enforce: "pre",
|
|
462
476
|
apply: "serve",
|
|
477
|
+
configResolved(config) {
|
|
478
|
+
viteConfig = config;
|
|
479
|
+
config.logger.warn("");
|
|
480
|
+
},
|
|
463
481
|
async configureServer({ middlewares, config, httpServer }) {
|
|
464
482
|
const middleware = await mockServerMiddleware(httpServer, config, {
|
|
465
483
|
include,
|
|
@@ -470,6 +488,17 @@ function mockDevServerPlugin({
|
|
|
470
488
|
}
|
|
471
489
|
});
|
|
472
490
|
middlewares.use(middleware);
|
|
491
|
+
},
|
|
492
|
+
async configurePreviewServer({ middlewares, httpServer }) {
|
|
493
|
+
const middleware = await mockServerMiddleware(httpServer, viteConfig, {
|
|
494
|
+
include,
|
|
495
|
+
exclude,
|
|
496
|
+
formidableOptions: {
|
|
497
|
+
multiples: true,
|
|
498
|
+
...formidableOptions
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
middlewares.use(middleware);
|
|
473
502
|
}
|
|
474
503
|
};
|
|
475
504
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.16",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"vite",
|
|
6
6
|
"plugin",
|
|
@@ -54,7 +54,6 @@
|
|
|
54
54
|
"tsup": "^6.5.0",
|
|
55
55
|
"typescript": "^4.9.4",
|
|
56
56
|
"vite": "^4.0.2",
|
|
57
|
-
"vite-plugin-mock-dev-server": "file:",
|
|
58
57
|
"vitepress": "1.0.0-alpha.35",
|
|
59
58
|
"vue": "^3.2.45"
|
|
60
59
|
},
|