vite-plugin-mock-dev-server 0.3.21 → 0.4.1
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 +23 -10
- package/dist/index.js +23 -10
- package/package.json +2 -1
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.
|
|
55
|
+
var version = "0.4.1";
|
|
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 =
|
|
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,27 +317,29 @@ 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
|
|
316
324
|
var import_co_body = __toESM(require("co-body"), 1);
|
|
317
325
|
var import_formidable = __toESM(require("formidable"), 1);
|
|
318
326
|
async function parseReqBody(req, options) {
|
|
327
|
+
var _a;
|
|
319
328
|
const method = req.method.toUpperCase();
|
|
320
329
|
if (["GET", "DELETE", "HEAD"].includes(method))
|
|
321
330
|
return void 0;
|
|
322
|
-
const type = req.headers["content-type"];
|
|
331
|
+
const type = ((_a = req.headers["content-type"]) == null ? void 0 : _a.toLocaleLowerCase()) || "";
|
|
323
332
|
try {
|
|
324
|
-
if (type
|
|
333
|
+
if (type.startsWith("application/json")) {
|
|
325
334
|
return await import_co_body.default.json(req);
|
|
326
335
|
}
|
|
327
|
-
if (type
|
|
336
|
+
if (type.startsWith("application/x-www-form-urlencoded")) {
|
|
328
337
|
return await import_co_body.default.form(req);
|
|
329
338
|
}
|
|
330
|
-
if (type
|
|
339
|
+
if (type.startsWith("text/plain")) {
|
|
331
340
|
return await import_co_body.default.text(req);
|
|
332
341
|
}
|
|
333
|
-
if (type
|
|
342
|
+
if (type.startsWith("multipart/form-data")) {
|
|
334
343
|
return await parseMultipart(req, options);
|
|
335
344
|
}
|
|
336
345
|
} catch (e) {
|
|
@@ -417,7 +426,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
417
426
|
await sleep(currentMock.delay);
|
|
418
427
|
}
|
|
419
428
|
res.statusCode = currentMock.status || 200;
|
|
420
|
-
res.statusMessage = currentMock.statusText ||
|
|
429
|
+
res.statusMessage = currentMock.statusText || getHTTPStatusText(res.statusCode);
|
|
421
430
|
const urlMatch = (0, import_path_to_regexp.match)(currentMock.url, { decode: decodeURIComponent })(
|
|
422
431
|
pathname
|
|
423
432
|
) || { params: {} };
|
|
@@ -427,6 +436,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
427
436
|
req.refererQuery = refererQuery;
|
|
428
437
|
req.params = params;
|
|
429
438
|
res.setHeader("Content-Type", "application/json");
|
|
439
|
+
res.setHeader("Cache-Control", "no-cache,max-age=0");
|
|
430
440
|
res.setHeader("X-Mock", "generate by vite:mock-dev-server");
|
|
431
441
|
if (currentMock.headers) {
|
|
432
442
|
const headers = isFunction(currentMock.headers) ? await currentMock.headers({
|
|
@@ -470,6 +480,9 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
470
480
|
function doesProxyContextMatchUrl(context, url) {
|
|
471
481
|
return context.startsWith("^") && new RegExp(context).test(url) || url.startsWith(context);
|
|
472
482
|
}
|
|
483
|
+
function getHTTPStatusText(status) {
|
|
484
|
+
return import_http_status.default[status] || "Unknown";
|
|
485
|
+
}
|
|
473
486
|
|
|
474
487
|
// src/MockLoader.ts
|
|
475
488
|
var import_node_events = __toESM(require("events"), 1);
|
|
@@ -732,7 +745,7 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
732
745
|
});
|
|
733
746
|
await loader.load();
|
|
734
747
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
735
|
-
const proxies =
|
|
748
|
+
const proxies = ensureProxies(config.server.proxy || {});
|
|
736
749
|
const prefix = ensureArray(options.prefix);
|
|
737
750
|
return baseMiddleware(loader, {
|
|
738
751
|
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.
|
|
12
|
+
var version = "0.4.1";
|
|
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 =
|
|
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,27 +274,29 @@ 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
|
|
273
281
|
import bodyParser from "co-body";
|
|
274
282
|
import formidable from "formidable";
|
|
275
283
|
async function parseReqBody(req, options) {
|
|
284
|
+
var _a;
|
|
276
285
|
const method = req.method.toUpperCase();
|
|
277
286
|
if (["GET", "DELETE", "HEAD"].includes(method))
|
|
278
287
|
return void 0;
|
|
279
|
-
const type = req.headers["content-type"];
|
|
288
|
+
const type = ((_a = req.headers["content-type"]) == null ? void 0 : _a.toLocaleLowerCase()) || "";
|
|
280
289
|
try {
|
|
281
|
-
if (type
|
|
290
|
+
if (type.startsWith("application/json")) {
|
|
282
291
|
return await bodyParser.json(req);
|
|
283
292
|
}
|
|
284
|
-
if (type
|
|
293
|
+
if (type.startsWith("application/x-www-form-urlencoded")) {
|
|
285
294
|
return await bodyParser.form(req);
|
|
286
295
|
}
|
|
287
|
-
if (type
|
|
296
|
+
if (type.startsWith("text/plain")) {
|
|
288
297
|
return await bodyParser.text(req);
|
|
289
298
|
}
|
|
290
|
-
if (type
|
|
299
|
+
if (type.startsWith("multipart/form-data")) {
|
|
291
300
|
return await parseMultipart(req, options);
|
|
292
301
|
}
|
|
293
302
|
} catch (e) {
|
|
@@ -374,7 +383,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
374
383
|
await sleep(currentMock.delay);
|
|
375
384
|
}
|
|
376
385
|
res.statusCode = currentMock.status || 200;
|
|
377
|
-
res.statusMessage = currentMock.statusText ||
|
|
386
|
+
res.statusMessage = currentMock.statusText || getHTTPStatusText(res.statusCode);
|
|
378
387
|
const urlMatch = match(currentMock.url, { decode: decodeURIComponent })(
|
|
379
388
|
pathname
|
|
380
389
|
) || { params: {} };
|
|
@@ -384,6 +393,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
384
393
|
req.refererQuery = refererQuery;
|
|
385
394
|
req.params = params;
|
|
386
395
|
res.setHeader("Content-Type", "application/json");
|
|
396
|
+
res.setHeader("Cache-Control", "no-cache,max-age=0");
|
|
387
397
|
res.setHeader("X-Mock", "generate by vite:mock-dev-server");
|
|
388
398
|
if (currentMock.headers) {
|
|
389
399
|
const headers = isFunction(currentMock.headers) ? await currentMock.headers({
|
|
@@ -427,6 +437,9 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
427
437
|
function doesProxyContextMatchUrl(context, url) {
|
|
428
438
|
return context.startsWith("^") && new RegExp(context).test(url) || url.startsWith(context);
|
|
429
439
|
}
|
|
440
|
+
function getHTTPStatusText(status) {
|
|
441
|
+
return HTTP_STATUS[status] || "Unknown";
|
|
442
|
+
}
|
|
430
443
|
|
|
431
444
|
// src/MockLoader.ts
|
|
432
445
|
import EventEmitter from "events";
|
|
@@ -689,7 +702,7 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
689
702
|
});
|
|
690
703
|
await loader.load();
|
|
691
704
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
692
|
-
const proxies =
|
|
705
|
+
const proxies = ensureProxies(config.server.proxy || {});
|
|
693
706
|
const prefix = ensureArray(options.prefix);
|
|
694
707
|
return baseMiddleware(loader, {
|
|
695
708
|
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
|
+
"version": "0.4.1",
|
|
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"
|