vite-plugin-mock-dev-server 0.3.7 → 0.3.10

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.
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
  ## 文档
package/dist/index.cjs CHANGED
@@ -322,6 +322,7 @@ MockLoader.EXT_JSON = /\.json5?$/;
322
322
 
323
323
  // src/parseReqBody.ts
324
324
  var import_co_body = __toESM(require("co-body"), 1);
325
+ var import_formidable = __toESM(require("formidable"), 1);
325
326
  async function parseReqBody(req) {
326
327
  const method = req.method.toUpperCase();
327
328
  if (["GET", "DELETE", "HEAD"].includes(method))
@@ -336,7 +337,23 @@ async function parseReqBody(req) {
336
337
  if (type === "text/plain") {
337
338
  return await import_co_body.default.text(req);
338
339
  }
339
- return await (0, import_co_body.default)(req);
340
+ if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
341
+ return await parseMultipart(req);
342
+ }
343
+ return void 0;
344
+ }
345
+ async function parseMultipart(req) {
346
+ const form = (0, import_formidable.default)({ multiples: true });
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
+ });
340
357
  }
341
358
 
342
359
  // src/validator.ts
package/dist/index.js CHANGED
@@ -287,6 +287,7 @@ MockLoader.EXT_JSON = /\.json5?$/;
287
287
 
288
288
  // src/parseReqBody.ts
289
289
  import bodyParser from "co-body";
290
+ import formidable from "formidable";
290
291
  async function parseReqBody(req) {
291
292
  const method = req.method.toUpperCase();
292
293
  if (["GET", "DELETE", "HEAD"].includes(method))
@@ -301,7 +302,23 @@ async function parseReqBody(req) {
301
302
  if (type === "text/plain") {
302
303
  return await bodyParser.text(req);
303
304
  }
304
- return await bodyParser(req);
305
+ if (type == null ? void 0 : type.startsWith("multipart/form-data;")) {
306
+ return await parseMultipart(req);
307
+ }
308
+ return void 0;
309
+ }
310
+ async function parseMultipart(req) {
311
+ const form = formidable({ multiples: true });
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
+ });
305
322
  }
306
323
 
307
324
  // src/validator.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-dev-server",
3
- "version": "0.3.7",
3
+ "version": "0.3.10",
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": "^2.1.1",
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",