vite-plugin-mock-dev-server 0.3.21 → 0.4.0

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/dist/index.cjs CHANGED
@@ -52,7 +52,7 @@ var import_vite = require("vite");
52
52
 
53
53
  // package.json
54
54
  var name = "vite-plugin-mock-dev-server";
55
- var version = "0.3.21";
55
+ var version = "0.4.0";
56
56
 
57
57
  // src/esbuildPlugin.ts
58
58
  var import_promises = __toESM(require("fs/promises"), 1);
@@ -131,6 +131,13 @@ function lookupFile(dir, formats, options) {
131
131
  return lookupFile(parentDir, formats, options);
132
132
  }
133
133
  }
134
+ var ensureProxies = (serverProxy = {}) => {
135
+ const proxies = Object.keys(serverProxy).map((key) => {
136
+ const value = serverProxy[key];
137
+ return typeof value === "string" ? key : value.ws === true ? "" : key;
138
+ }).filter(Boolean);
139
+ return proxies;
140
+ };
134
141
 
135
142
  // src/build.ts
136
143
  async function generateMockServer(ctx, config, options) {
@@ -143,7 +150,7 @@ async function generateMockServer(ctx, config, options) {
143
150
  define[key] = typeof val === "string" ? val : JSON.stringify(val);
144
151
  }
145
152
  }
146
- const proxies = Object.keys(config.server.proxy || {});
153
+ const proxies = ensureProxies(config.server.proxy || {});
147
154
  const prefix = ensureArray(options.prefix);
148
155
  let pkg = {};
149
156
  try {
@@ -235,7 +242,7 @@ import mockData from './mock-data.js'
235
242
  const app = connect()
236
243
  app.use(baseMiddleware({ mockData }, {
237
244
  formidableOptions: { multiples: true },
238
- proxies: ${JSON.stringify(proxies)}
245
+ proxies: ${JSON.stringify(proxies)}
239
246
  }))
240
247
  app.listen(${port})
241
248
  console.log('listen: http://localhost:${port}')
@@ -310,6 +317,7 @@ async function buildMockEntry(inputFile, define) {
310
317
 
311
318
  // src/baseMiddleware.ts
312
319
  var import_node_url2 = require("url");
320
+ var import_http_status = __toESM(require("http-status"), 1);
313
321
  var import_path_to_regexp = require("path-to-regexp");
314
322
 
315
323
  // src/parseReqBody.ts
@@ -417,7 +425,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
417
425
  await sleep(currentMock.delay);
418
426
  }
419
427
  res.statusCode = currentMock.status || 200;
420
- res.statusMessage = currentMock.statusText || "OK";
428
+ res.statusMessage = currentMock.statusText || getHTTPStatusText(res.statusCode);
421
429
  const urlMatch = (0, import_path_to_regexp.match)(currentMock.url, { decode: decodeURIComponent })(
422
430
  pathname
423
431
  ) || { params: {} };
@@ -470,6 +478,9 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
470
478
  function doesProxyContextMatchUrl(context, url) {
471
479
  return context.startsWith("^") && new RegExp(context).test(url) || url.startsWith(context);
472
480
  }
481
+ function getHTTPStatusText(status) {
482
+ return import_http_status.default[status] || "Unknown";
483
+ }
473
484
 
474
485
  // src/MockLoader.ts
475
486
  var import_node_events = __toESM(require("events"), 1);
@@ -732,7 +743,7 @@ async function mockServerMiddleware(httpServer, config, options) {
732
743
  });
733
744
  await loader.load();
734
745
  httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
735
- const proxies = Object.keys(config.server.proxy || {});
746
+ const proxies = ensureProxies(config.server.proxy || {});
736
747
  const prefix = ensureArray(options.prefix);
737
748
  return baseMiddleware(loader, {
738
749
  formidableOptions: options.formidableOptions,
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { createFilter } from "vite";
9
9
 
10
10
  // package.json
11
11
  var name = "vite-plugin-mock-dev-server";
12
- var version = "0.3.21";
12
+ var version = "0.4.0";
13
13
 
14
14
  // src/esbuildPlugin.ts
15
15
  import fsp from "fs/promises";
@@ -88,6 +88,13 @@ function lookupFile(dir, formats, options) {
88
88
  return lookupFile(parentDir, formats, options);
89
89
  }
90
90
  }
91
+ var ensureProxies = (serverProxy = {}) => {
92
+ const proxies = Object.keys(serverProxy).map((key) => {
93
+ const value = serverProxy[key];
94
+ return typeof value === "string" ? key : value.ws === true ? "" : key;
95
+ }).filter(Boolean);
96
+ return proxies;
97
+ };
91
98
 
92
99
  // src/build.ts
93
100
  async function generateMockServer(ctx, config, options) {
@@ -100,7 +107,7 @@ async function generateMockServer(ctx, config, options) {
100
107
  define[key] = typeof val === "string" ? val : JSON.stringify(val);
101
108
  }
102
109
  }
103
- const proxies = Object.keys(config.server.proxy || {});
110
+ const proxies = ensureProxies(config.server.proxy || {});
104
111
  const prefix = ensureArray(options.prefix);
105
112
  let pkg = {};
106
113
  try {
@@ -192,7 +199,7 @@ import mockData from './mock-data.js'
192
199
  const app = connect()
193
200
  app.use(baseMiddleware({ mockData }, {
194
201
  formidableOptions: { multiples: true },
195
- proxies: ${JSON.stringify(proxies)}
202
+ proxies: ${JSON.stringify(proxies)}
196
203
  }))
197
204
  app.listen(${port})
198
205
  console.log('listen: http://localhost:${port}')
@@ -267,6 +274,7 @@ async function buildMockEntry(inputFile, define) {
267
274
 
268
275
  // src/baseMiddleware.ts
269
276
  import { parse as urlParse } from "url";
277
+ import HTTP_STATUS from "http-status";
270
278
  import { match, pathToRegexp } from "path-to-regexp";
271
279
 
272
280
  // src/parseReqBody.ts
@@ -374,7 +382,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
374
382
  await sleep(currentMock.delay);
375
383
  }
376
384
  res.statusCode = currentMock.status || 200;
377
- res.statusMessage = currentMock.statusText || "OK";
385
+ res.statusMessage = currentMock.statusText || getHTTPStatusText(res.statusCode);
378
386
  const urlMatch = match(currentMock.url, { decode: decodeURIComponent })(
379
387
  pathname
380
388
  ) || { params: {} };
@@ -427,6 +435,9 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
427
435
  function doesProxyContextMatchUrl(context, url) {
428
436
  return context.startsWith("^") && new RegExp(context).test(url) || url.startsWith(context);
429
437
  }
438
+ function getHTTPStatusText(status) {
439
+ return HTTP_STATUS[status] || "Unknown";
440
+ }
430
441
 
431
442
  // src/MockLoader.ts
432
443
  import EventEmitter from "events";
@@ -689,7 +700,7 @@ async function mockServerMiddleware(httpServer, config, options) {
689
700
  });
690
701
  await loader.load();
691
702
  httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
692
- const proxies = Object.keys(config.server.proxy || {});
703
+ const proxies = ensureProxies(config.server.proxy || {});
693
704
  const prefix = ensureArray(options.prefix);
694
705
  return baseMiddleware(loader, {
695
706
  formidableOptions: options.formidableOptions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-dev-server",
3
- "version": "0.3.21",
3
+ "version": "0.4.0",
4
4
  "keywords": [
5
5
  "vite",
6
6
  "plugin",
@@ -36,6 +36,7 @@
36
36
  "esbuild": "^0.17.6",
37
37
  "fast-glob": "^3.2.12",
38
38
  "formidable": "^2.1.1",
39
+ "http-status": "^1.6.2",
39
40
  "is-core-module": "^2.11.0",
40
41
  "json5": "^2.2.3",
41
42
  "path-to-regexp": "^6.2.1"