vite-plugin-mock-dev-server 0.3.9 → 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 CHANGED
@@ -33,7 +33,7 @@
33
33
  - 🔥 HMR
34
34
  - ⚖️ Use `server.proxy`
35
35
  - 🍕 Support `viteConfig.define` in mock file
36
-
36
+ - 📤 Support `multipart` content-type,mock upload file.
37
37
  ## Documentation
38
38
 
39
39
  See the [documentation](https://vite-plugin-mock-dev-server.netlify.app/) to learn more.
@@ -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
@@ -34,6 +34,7 @@
34
34
  - 🔥 热更新
35
35
  - ⚖️ 使用 `server.proxy` 配置
36
36
  - 🍕 支持在 mock文件中使用 `viteConfig.define`配置字段
37
+ - 📤 支持 multipart 类型,模拟文件上传
37
38
 
38
39
 
39
40
  ## 文档
@@ -131,6 +132,21 @@ export default defineConfig({
131
132
 
132
133
  默认值:`['**/node_modules/**','**/test/**','**/cypress/**','src/**','**/.vscode/**','**/.git/**','**/dist/**',]`
133
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
+
134
150
 
135
151
  ### defineMock(config)
136
152
 
@@ -408,6 +424,42 @@ export default defineMock({
408
424
  }
409
425
  ```
410
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
+
411
463
  ## Archives
412
464
 
413
465
  [awesome-vite](https://github.com/vitejs/awesome-vite#helpers)
package/dist/index.cjs CHANGED
@@ -322,7 +322,8 @@ MockLoader.EXT_JSON = /\.json5?$/;
322
322
 
323
323
  // src/parseReqBody.ts
324
324
  var import_co_body = __toESM(require("co-body"), 1);
325
- async function parseReqBody(req) {
325
+ var import_formidable = __toESM(require("formidable"), 1);
326
+ async function parseReqBody(req, options) {
326
327
  const method = req.method.toUpperCase();
327
328
  if (["GET", "DELETE", "HEAD"].includes(method))
328
329
  return void 0;
@@ -336,8 +337,24 @@ async function parseReqBody(req) {
336
337
  if (type === "text/plain") {
337
338
  return await import_co_body.default.text(req);
338
339
  }
340
+ if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
341
+ return await parseMultipart(req, options);
342
+ }
339
343
  return void 0;
340
344
  }
345
+ async function parseMultipart(req, options) {
346
+ const form = (0, import_formidable.default)(options);
347
+ debug("multiparty start");
348
+ return new Promise((resolve, reject) => {
349
+ form.parse(req, (error, fields, files) => {
350
+ if (error) {
351
+ reject(error);
352
+ return;
353
+ }
354
+ resolve({ ...fields, ...files });
355
+ });
356
+ });
357
+ }
341
358
 
342
359
  // src/validator.ts
343
360
  function validate(request, validator) {
@@ -371,7 +388,7 @@ async function mockServerMiddleware(httpServer, config, options) {
371
388
  }
372
389
  const method = req.method.toUpperCase();
373
390
  const { query, pathname } = (0, import_node_url3.parse)(req.url, true);
374
- const reqBody = await parseReqBody(req);
391
+ const reqBody = await parseReqBody(req, options.formidableOptions);
375
392
  const currentMock = loader.mockList.find((mock) => {
376
393
  if (!pathname || !mock || !mock.url)
377
394
  return false;
@@ -467,7 +484,8 @@ function mockDevServerPlugin({
467
484
  "**/.vscode/**",
468
485
  "**/.git/**",
469
486
  "**/dist/**"
470
- ]
487
+ ],
488
+ formidableOptions = {}
471
489
  } = {}) {
472
490
  return {
473
491
  name: "vite-plugin-mock-dev-server",
@@ -476,7 +494,8 @@ function mockDevServerPlugin({
476
494
  async configureServer({ middlewares, config, httpServer }) {
477
495
  const middleware = await mockServerMiddleware(httpServer, config, {
478
496
  include,
479
- exclude
497
+ exclude,
498
+ formidableOptions
480
499
  });
481
500
  middlewares.use(middleware);
482
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
@@ -287,7 +287,8 @@ MockLoader.EXT_JSON = /\.json5?$/;
287
287
 
288
288
  // src/parseReqBody.ts
289
289
  import bodyParser from "co-body";
290
- async function parseReqBody(req) {
290
+ import formidable from "formidable";
291
+ async function parseReqBody(req, options) {
291
292
  const method = req.method.toUpperCase();
292
293
  if (["GET", "DELETE", "HEAD"].includes(method))
293
294
  return void 0;
@@ -301,8 +302,24 @@ async function parseReqBody(req) {
301
302
  if (type === "text/plain") {
302
303
  return await bodyParser.text(req);
303
304
  }
305
+ if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
306
+ return await parseMultipart(req, options);
307
+ }
304
308
  return void 0;
305
309
  }
310
+ async function parseMultipart(req, options) {
311
+ const form = formidable(options);
312
+ debug("multiparty start");
313
+ return new Promise((resolve, reject) => {
314
+ form.parse(req, (error, fields, files) => {
315
+ if (error) {
316
+ reject(error);
317
+ return;
318
+ }
319
+ resolve({ ...fields, ...files });
320
+ });
321
+ });
322
+ }
306
323
 
307
324
  // src/validator.ts
308
325
  function validate(request, validator) {
@@ -336,7 +353,7 @@ async function mockServerMiddleware(httpServer, config, options) {
336
353
  }
337
354
  const method = req.method.toUpperCase();
338
355
  const { query, pathname } = urlParse(req.url, true);
339
- const reqBody = await parseReqBody(req);
356
+ const reqBody = await parseReqBody(req, options.formidableOptions);
340
357
  const currentMock = loader.mockList.find((mock) => {
341
358
  if (!pathname || !mock || !mock.url)
342
359
  return false;
@@ -432,7 +449,8 @@ function mockDevServerPlugin({
432
449
  "**/.vscode/**",
433
450
  "**/.git/**",
434
451
  "**/dist/**"
435
- ]
452
+ ],
453
+ formidableOptions = {}
436
454
  } = {}) {
437
455
  return {
438
456
  name: "vite-plugin-mock-dev-server",
@@ -441,7 +459,8 @@ function mockDevServerPlugin({
441
459
  async configureServer({ middlewares, config, httpServer }) {
442
460
  const middleware = await mockServerMiddleware(httpServer, config, {
443
461
  include,
444
- exclude
462
+ exclude,
463
+ formidableOptions
445
464
  });
446
465
  middlewares.use(middleware);
447
466
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-dev-server",
3
- "version": "0.3.9",
3
+ "version": "0.3.11",
4
4
  "keywords": [
5
5
  "vite",
6
6
  "plugin",
@@ -49,6 +49,7 @@
49
49
  "debug": "^4.3.4",
50
50
  "esbuild": "^0.16.9",
51
51
  "fast-glob": "^3.2.12",
52
+ "formidable": "v3",
52
53
  "json5": "^2.2.2",
53
54
  "path-to-regexp": "^6.2.1"
54
55
  },
@@ -57,6 +58,7 @@
57
58
  "@pengzhanbo/prettier-config": "^0.2.10",
58
59
  "@types/co-body": "^6.1.0",
59
60
  "@types/debug": "^4.1.7",
61
+ "@types/formidable": "^2.0.5",
60
62
  "@types/node": "^18.11.7",
61
63
  "bumpp": "^8.2.1",
62
64
  "eslint": "^8.30.0",