vite-plugin-mock-dev-server 0.3.10 → 0.3.11
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 +9 -7
- package/dist/index.d.ts +7 -1
- package/dist/index.js +9 -7
- 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,7 +323,7 @@ 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;
|
|
@@ -338,12 +338,12 @@ async function parseReqBody(req) {
|
|
|
338
338
|
return await import_co_body.default.text(req);
|
|
339
339
|
}
|
|
340
340
|
if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
|
|
341
|
-
return await parseMultipart(req);
|
|
341
|
+
return await parseMultipart(req, options);
|
|
342
342
|
}
|
|
343
343
|
return void 0;
|
|
344
344
|
}
|
|
345
|
-
async function parseMultipart(req) {
|
|
346
|
-
const form = (0, import_formidable.default)(
|
|
345
|
+
async function parseMultipart(req, options) {
|
|
346
|
+
const form = (0, import_formidable.default)(options);
|
|
347
347
|
debug("multiparty start");
|
|
348
348
|
return new Promise((resolve, reject) => {
|
|
349
349
|
form.parse(req, (error, fields, files) => {
|
|
@@ -388,7 +388,7 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
388
388
|
}
|
|
389
389
|
const method = req.method.toUpperCase();
|
|
390
390
|
const { query, pathname } = (0, import_node_url3.parse)(req.url, true);
|
|
391
|
-
const reqBody = await parseReqBody(req);
|
|
391
|
+
const reqBody = await parseReqBody(req, options.formidableOptions);
|
|
392
392
|
const currentMock = loader.mockList.find((mock) => {
|
|
393
393
|
if (!pathname || !mock || !mock.url)
|
|
394
394
|
return false;
|
|
@@ -484,7 +484,8 @@ function mockDevServerPlugin({
|
|
|
484
484
|
"**/.vscode/**",
|
|
485
485
|
"**/.git/**",
|
|
486
486
|
"**/dist/**"
|
|
487
|
-
]
|
|
487
|
+
],
|
|
488
|
+
formidableOptions = {}
|
|
488
489
|
} = {}) {
|
|
489
490
|
return {
|
|
490
491
|
name: "vite-plugin-mock-dev-server",
|
|
@@ -493,7 +494,8 @@ function mockDevServerPlugin({
|
|
|
493
494
|
async configureServer({ middlewares, config, httpServer }) {
|
|
494
495
|
const middleware = await mockServerMiddleware(httpServer, config, {
|
|
495
496
|
include,
|
|
496
|
-
exclude
|
|
497
|
+
exclude,
|
|
498
|
+
formidableOptions
|
|
497
499
|
});
|
|
498
500
|
middlewares.use(middleware);
|
|
499
501
|
}
|
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,7 +288,7 @@ 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;
|
|
@@ -303,12 +303,12 @@ async function parseReqBody(req) {
|
|
|
303
303
|
return await bodyParser.text(req);
|
|
304
304
|
}
|
|
305
305
|
if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
|
|
306
|
-
return await parseMultipart(req);
|
|
306
|
+
return await parseMultipart(req, options);
|
|
307
307
|
}
|
|
308
308
|
return void 0;
|
|
309
309
|
}
|
|
310
|
-
async function parseMultipart(req) {
|
|
311
|
-
const form = formidable(
|
|
310
|
+
async function parseMultipart(req, options) {
|
|
311
|
+
const form = formidable(options);
|
|
312
312
|
debug("multiparty start");
|
|
313
313
|
return new Promise((resolve, reject) => {
|
|
314
314
|
form.parse(req, (error, fields, files) => {
|
|
@@ -353,7 +353,7 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
353
353
|
}
|
|
354
354
|
const method = req.method.toUpperCase();
|
|
355
355
|
const { query, pathname } = urlParse(req.url, true);
|
|
356
|
-
const reqBody = await parseReqBody(req);
|
|
356
|
+
const reqBody = await parseReqBody(req, options.formidableOptions);
|
|
357
357
|
const currentMock = loader.mockList.find((mock) => {
|
|
358
358
|
if (!pathname || !mock || !mock.url)
|
|
359
359
|
return false;
|
|
@@ -449,7 +449,8 @@ function mockDevServerPlugin({
|
|
|
449
449
|
"**/.vscode/**",
|
|
450
450
|
"**/.git/**",
|
|
451
451
|
"**/dist/**"
|
|
452
|
-
]
|
|
452
|
+
],
|
|
453
|
+
formidableOptions = {}
|
|
453
454
|
} = {}) {
|
|
454
455
|
return {
|
|
455
456
|
name: "vite-plugin-mock-dev-server",
|
|
@@ -458,7 +459,8 @@ function mockDevServerPlugin({
|
|
|
458
459
|
async configureServer({ middlewares, config, httpServer }) {
|
|
459
460
|
const middleware = await mockServerMiddleware(httpServer, config, {
|
|
460
461
|
include,
|
|
461
|
-
exclude
|
|
462
|
+
exclude,
|
|
463
|
+
formidableOptions
|
|
462
464
|
});
|
|
463
465
|
middlewares.use(middleware);
|
|
464
466
|
}
|
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.11",
|
|
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
|
},
|