vite-plugin-mock-dev-server 0.3.10 → 0.3.12
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 +50 -0
- package/README.zh-CN.md +51 -0
- package/dist/index.cjs +23 -18
- package/dist/index.d.ts +7 -1
- package/dist/index.js +23 -18
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -140,6 +140,20 @@ export default defineConfig({
|
|
|
140
140
|
]
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
- `options.formidableOptions`
|
|
144
|
+
|
|
145
|
+
Configure to `formidable`,see [formidable options](https://github.com/node-formidable/formidable#options)
|
|
146
|
+
|
|
147
|
+
Default: `{}`
|
|
148
|
+
|
|
149
|
+
example: Configure to file upload dir
|
|
150
|
+
```ts
|
|
151
|
+
MockDevServerPlugin({
|
|
152
|
+
formidableOptions: {
|
|
153
|
+
uploadDir: path.join(process.cwd(), 'uploads'),
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
```
|
|
143
157
|
|
|
144
158
|
### defineMock(config)
|
|
145
159
|
|
|
@@ -416,6 +430,42 @@ Use json / json5
|
|
|
416
430
|
}
|
|
417
431
|
```
|
|
418
432
|
|
|
433
|
+
### Example 11:
|
|
434
|
+
|
|
435
|
+
multipart, upload file.
|
|
436
|
+
|
|
437
|
+
use [`formidable`](https://www.npmjs.com/package/formidable#readme) to supported.
|
|
438
|
+
``` html
|
|
439
|
+
<form action="/api/upload" method="post" enctype="multipart/form-data">
|
|
440
|
+
<p>
|
|
441
|
+
<span>file: </span>
|
|
442
|
+
<input type="file" name="files" multiple />
|
|
443
|
+
</p>
|
|
444
|
+
<p>
|
|
445
|
+
<span>name:</span>
|
|
446
|
+
<input type="text" name="name" value="mark">
|
|
447
|
+
</p>
|
|
448
|
+
<p>
|
|
449
|
+
<input type="submit" value="submit">
|
|
450
|
+
</p>
|
|
451
|
+
</form>
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
fields `files` mapping to `formidable.File`
|
|
455
|
+
``` ts
|
|
456
|
+
export default defineMock({
|
|
457
|
+
url: '/api/upload',
|
|
458
|
+
method: 'POST',
|
|
459
|
+
body(req) {
|
|
460
|
+
const body = req.body
|
|
461
|
+
return {
|
|
462
|
+
name: body.name,
|
|
463
|
+
files: body.files.map((file: any) => file.originalFilename),
|
|
464
|
+
}
|
|
465
|
+
},
|
|
466
|
+
})
|
|
467
|
+
```
|
|
468
|
+
|
|
419
469
|
## Archives
|
|
420
470
|
|
|
421
471
|
[awesome-vite](https://github.com/vitejs/awesome-vite#helpers)
|
package/README.zh-CN.md
CHANGED
|
@@ -132,6 +132,21 @@ export default defineConfig({
|
|
|
132
132
|
|
|
133
133
|
默认值:`['**/node_modules/**','**/test/**','**/cypress/**','src/**','**/.vscode/**','**/.git/**','**/dist/**',]`
|
|
134
134
|
|
|
135
|
+
- `options.formidableOptions`
|
|
136
|
+
|
|
137
|
+
配置 `formidable`,查看 [formidable options](https://github.com/node-formidable/formidable#options)
|
|
138
|
+
|
|
139
|
+
默认值: `{}`
|
|
140
|
+
|
|
141
|
+
示例: 配置文件上传的存放目录
|
|
142
|
+
```ts
|
|
143
|
+
MockDevServerPlugin({
|
|
144
|
+
formidableOptions: {
|
|
145
|
+
uploadDir: path.join(process.cwd(), 'uploads'),
|
|
146
|
+
}
|
|
147
|
+
})
|
|
148
|
+
```
|
|
149
|
+
|
|
135
150
|
|
|
136
151
|
### defineMock(config)
|
|
137
152
|
|
|
@@ -409,6 +424,42 @@ export default defineMock({
|
|
|
409
424
|
}
|
|
410
425
|
```
|
|
411
426
|
|
|
427
|
+
### Example 11:
|
|
428
|
+
|
|
429
|
+
multipart, 文件上传.
|
|
430
|
+
|
|
431
|
+
通过 [`formidable`](https://www.npmjs.com/package/formidable#readme) 支持。
|
|
432
|
+
``` html
|
|
433
|
+
<form action="/api/upload" method="post" enctype="multipart/form-data">
|
|
434
|
+
<p>
|
|
435
|
+
<span>file: </span>
|
|
436
|
+
<input type="file" name="files" multiple />
|
|
437
|
+
</p>
|
|
438
|
+
<p>
|
|
439
|
+
<span>name:</span>
|
|
440
|
+
<input type="text" name="name" value="mark">
|
|
441
|
+
</p>
|
|
442
|
+
<p>
|
|
443
|
+
<input type="submit" value="submit">
|
|
444
|
+
</p>
|
|
445
|
+
</form>
|
|
446
|
+
```
|
|
447
|
+
|
|
448
|
+
fields `files` 映射为 `formidable.File` 类型。
|
|
449
|
+
``` ts
|
|
450
|
+
export default defineMock({
|
|
451
|
+
url: '/api/upload',
|
|
452
|
+
method: 'POST',
|
|
453
|
+
body(req) {
|
|
454
|
+
const body = req.body
|
|
455
|
+
return {
|
|
456
|
+
name: body.name,
|
|
457
|
+
files: body.files.map((file: any) => file.originalFilename),
|
|
458
|
+
}
|
|
459
|
+
},
|
|
460
|
+
})
|
|
461
|
+
```
|
|
462
|
+
|
|
412
463
|
## Archives
|
|
413
464
|
|
|
414
465
|
[awesome-vite](https://github.com/vitejs/awesome-vite#helpers)
|
package/dist/index.cjs
CHANGED
|
@@ -323,28 +323,31 @@ MockLoader.EXT_JSON = /\.json5?$/;
|
|
|
323
323
|
// src/parseReqBody.ts
|
|
324
324
|
var import_co_body = __toESM(require("co-body"), 1);
|
|
325
325
|
var import_formidable = __toESM(require("formidable"), 1);
|
|
326
|
-
async function parseReqBody(req) {
|
|
326
|
+
async function parseReqBody(req, options) {
|
|
327
327
|
const method = req.method.toUpperCase();
|
|
328
328
|
if (["GET", "DELETE", "HEAD"].includes(method))
|
|
329
329
|
return void 0;
|
|
330
330
|
const type = req.headers["content-type"];
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
331
|
+
try {
|
|
332
|
+
if (type === "application/json") {
|
|
333
|
+
return await import_co_body.default.json(req);
|
|
334
|
+
}
|
|
335
|
+
if (type === "application/x-www-form-urlencoded") {
|
|
336
|
+
return await import_co_body.default.form(req);
|
|
337
|
+
}
|
|
338
|
+
if (type === "text/plain") {
|
|
339
|
+
return await import_co_body.default.text(req);
|
|
340
|
+
}
|
|
341
|
+
if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
|
|
342
|
+
return await parseMultipart(req, options);
|
|
343
|
+
}
|
|
344
|
+
} catch (e) {
|
|
345
|
+
console.error(e);
|
|
342
346
|
}
|
|
343
347
|
return void 0;
|
|
344
348
|
}
|
|
345
|
-
async function parseMultipart(req) {
|
|
346
|
-
const form = (0, import_formidable.default)(
|
|
347
|
-
debug("multiparty start");
|
|
349
|
+
async function parseMultipart(req, options) {
|
|
350
|
+
const form = (0, import_formidable.default)(options);
|
|
348
351
|
return new Promise((resolve, reject) => {
|
|
349
352
|
form.parse(req, (error, fields, files) => {
|
|
350
353
|
if (error) {
|
|
@@ -388,7 +391,7 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
388
391
|
}
|
|
389
392
|
const method = req.method.toUpperCase();
|
|
390
393
|
const { query, pathname } = (0, import_node_url3.parse)(req.url, true);
|
|
391
|
-
const reqBody = await parseReqBody(req);
|
|
394
|
+
const reqBody = await parseReqBody(req, options.formidableOptions);
|
|
392
395
|
const currentMock = loader.mockList.find((mock) => {
|
|
393
396
|
if (!pathname || !mock || !mock.url)
|
|
394
397
|
return false;
|
|
@@ -484,7 +487,8 @@ function mockDevServerPlugin({
|
|
|
484
487
|
"**/.vscode/**",
|
|
485
488
|
"**/.git/**",
|
|
486
489
|
"**/dist/**"
|
|
487
|
-
]
|
|
490
|
+
],
|
|
491
|
+
formidableOptions = {}
|
|
488
492
|
} = {}) {
|
|
489
493
|
return {
|
|
490
494
|
name: "vite-plugin-mock-dev-server",
|
|
@@ -493,7 +497,8 @@ function mockDevServerPlugin({
|
|
|
493
497
|
async configureServer({ middlewares, config, httpServer }) {
|
|
494
498
|
const middleware = await mockServerMiddleware(httpServer, config, {
|
|
495
499
|
include,
|
|
496
|
-
exclude
|
|
500
|
+
exclude,
|
|
501
|
+
formidableOptions
|
|
497
502
|
});
|
|
498
503
|
middlewares.use(middleware);
|
|
499
504
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { Connect, Plugin } from 'vite';
|
|
2
2
|
import http from 'node:http';
|
|
3
|
+
import formidable from 'formidable';
|
|
3
4
|
|
|
4
5
|
interface MockServerPluginOptions {
|
|
5
6
|
/**
|
|
@@ -13,6 +14,11 @@ interface MockServerPluginOptions {
|
|
|
13
14
|
* @see https://github.com/micromatch/picomatch#globbing-features
|
|
14
15
|
*/
|
|
15
16
|
exclude?: string | string[];
|
|
17
|
+
/**
|
|
18
|
+
* formidable options
|
|
19
|
+
* @see https://github.com/node-formidable/formidable#options
|
|
20
|
+
*/
|
|
21
|
+
formidableOptions?: formidable.Options;
|
|
16
22
|
}
|
|
17
23
|
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'TRACE' | 'OPTIONS';
|
|
18
24
|
type ResponseBody = Record<string, any> | any[] | string | number | null;
|
|
@@ -109,7 +115,7 @@ interface MockOptionsItem {
|
|
|
109
115
|
}
|
|
110
116
|
type MockOptions = MockOptionsItem[];
|
|
111
117
|
|
|
112
|
-
declare function mockDevServerPlugin({ include, exclude, }?: MockServerPluginOptions): Plugin;
|
|
118
|
+
declare function mockDevServerPlugin({ include, exclude, formidableOptions, }?: MockServerPluginOptions): Plugin;
|
|
113
119
|
|
|
114
120
|
/**
|
|
115
121
|
* mock config helper
|
package/dist/index.js
CHANGED
|
@@ -288,28 +288,31 @@ MockLoader.EXT_JSON = /\.json5?$/;
|
|
|
288
288
|
// src/parseReqBody.ts
|
|
289
289
|
import bodyParser from "co-body";
|
|
290
290
|
import formidable from "formidable";
|
|
291
|
-
async function parseReqBody(req) {
|
|
291
|
+
async function parseReqBody(req, options) {
|
|
292
292
|
const method = req.method.toUpperCase();
|
|
293
293
|
if (["GET", "DELETE", "HEAD"].includes(method))
|
|
294
294
|
return void 0;
|
|
295
295
|
const type = req.headers["content-type"];
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
296
|
+
try {
|
|
297
|
+
if (type === "application/json") {
|
|
298
|
+
return await bodyParser.json(req);
|
|
299
|
+
}
|
|
300
|
+
if (type === "application/x-www-form-urlencoded") {
|
|
301
|
+
return await bodyParser.form(req);
|
|
302
|
+
}
|
|
303
|
+
if (type === "text/plain") {
|
|
304
|
+
return await bodyParser.text(req);
|
|
305
|
+
}
|
|
306
|
+
if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
|
|
307
|
+
return await parseMultipart(req, options);
|
|
308
|
+
}
|
|
309
|
+
} catch (e) {
|
|
310
|
+
console.error(e);
|
|
307
311
|
}
|
|
308
312
|
return void 0;
|
|
309
313
|
}
|
|
310
|
-
async function parseMultipart(req) {
|
|
311
|
-
const form = formidable(
|
|
312
|
-
debug("multiparty start");
|
|
314
|
+
async function parseMultipart(req, options) {
|
|
315
|
+
const form = formidable(options);
|
|
313
316
|
return new Promise((resolve, reject) => {
|
|
314
317
|
form.parse(req, (error, fields, files) => {
|
|
315
318
|
if (error) {
|
|
@@ -353,7 +356,7 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
353
356
|
}
|
|
354
357
|
const method = req.method.toUpperCase();
|
|
355
358
|
const { query, pathname } = urlParse(req.url, true);
|
|
356
|
-
const reqBody = await parseReqBody(req);
|
|
359
|
+
const reqBody = await parseReqBody(req, options.formidableOptions);
|
|
357
360
|
const currentMock = loader.mockList.find((mock) => {
|
|
358
361
|
if (!pathname || !mock || !mock.url)
|
|
359
362
|
return false;
|
|
@@ -449,7 +452,8 @@ function mockDevServerPlugin({
|
|
|
449
452
|
"**/.vscode/**",
|
|
450
453
|
"**/.git/**",
|
|
451
454
|
"**/dist/**"
|
|
452
|
-
]
|
|
455
|
+
],
|
|
456
|
+
formidableOptions = {}
|
|
453
457
|
} = {}) {
|
|
454
458
|
return {
|
|
455
459
|
name: "vite-plugin-mock-dev-server",
|
|
@@ -458,7 +462,8 @@ function mockDevServerPlugin({
|
|
|
458
462
|
async configureServer({ middlewares, config, httpServer }) {
|
|
459
463
|
const middleware = await mockServerMiddleware(httpServer, config, {
|
|
460
464
|
include,
|
|
461
|
-
exclude
|
|
465
|
+
exclude,
|
|
466
|
+
formidableOptions
|
|
462
467
|
});
|
|
463
468
|
middlewares.use(middleware);
|
|
464
469
|
}
|
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.12",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"vite",
|
|
6
6
|
"plugin",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"debug": "^4.3.4",
|
|
50
50
|
"esbuild": "^0.16.9",
|
|
51
51
|
"fast-glob": "^3.2.12",
|
|
52
|
-
"formidable": "
|
|
52
|
+
"formidable": "v3",
|
|
53
53
|
"json5": "^2.2.2",
|
|
54
54
|
"path-to-regexp": "^6.2.1"
|
|
55
55
|
},
|