vite-plugin-mock-data 6.0.3 → 8.0.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/configureServer.d.ts +1 -1
- package/dist/index.js +255 -144
- package/dist/logger.d.ts +3 -0
- package/package.json +14 -14
- package/dist/index.mjs +0 -155
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { OutgoingHttpHeaders } from 'node:http';
|
|
2
2
|
import { Config as SirvConfig, HTTPVersion } from 'find-my-way';
|
|
3
|
-
import {
|
|
3
|
+
import { Options as SirvOptions } from 'sirv';
|
|
4
4
|
import { ViteDevServer } from 'vite';
|
|
5
5
|
import { RouteConfig } from './types';
|
|
6
6
|
export declare function sirvOptions(headers?: OutgoingHttpHeaders): SirvOptions;
|
package/dist/index.js
CHANGED
|
@@ -1,155 +1,266 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
var
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { isObject } from "is-what-type";
|
|
2
|
+
import { banner, logFactory, toAbsolutePath } from "vp-runtime-helper";
|
|
3
|
+
import { join, parse } from "node:path";
|
|
4
|
+
import getRouter from "find-my-way";
|
|
5
|
+
import sirv from "sirv";
|
|
6
|
+
import { send, transformWithOxc } from "vite";
|
|
7
|
+
import { readFileSync } from "node:fs";
|
|
8
|
+
import { unlink, writeFile } from "node:fs/promises";
|
|
9
|
+
import { createRequire } from "node:module";
|
|
10
|
+
import { glob } from "tinyglobby";
|
|
11
|
+
//#region \0rolldown/runtime.js
|
|
12
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, { get: (a, b) => (typeof require !== "undefined" ? require : a)[b] }) : x)(function(x) {
|
|
13
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
14
|
+
throw Error("Calling `require` for \"" + x + "\" in an environment that doesn't expose the `require` function. See https://rolldown.rs/in-depth/bundling-cjs#require-external-modules for more details.");
|
|
15
|
+
});
|
|
16
|
+
//#endregion
|
|
17
|
+
//#region package.json
|
|
18
|
+
var name = "vite-plugin-mock-data";
|
|
19
|
+
var package_default = {
|
|
20
|
+
name,
|
|
21
|
+
version: "8.0.1",
|
|
22
|
+
description: "Provides a simple way to mock data.",
|
|
23
|
+
type: "module",
|
|
24
|
+
types: "./dist/index.d.ts",
|
|
25
|
+
exports: { ".": {
|
|
26
|
+
"import": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts"
|
|
28
|
+
} },
|
|
29
|
+
engines: {
|
|
30
|
+
"node": "^20.19.0 || >=22.12.0",
|
|
31
|
+
"vite": ">=8.0.0"
|
|
32
|
+
},
|
|
33
|
+
scripts: {
|
|
34
|
+
"build": "vite build",
|
|
35
|
+
"watch": "vite build --watch",
|
|
36
|
+
"prepublishOnly": "npm run build",
|
|
37
|
+
"release": "pnpm publish --no-git-checks",
|
|
38
|
+
"test": "vitest run",
|
|
39
|
+
"test:watch": "vitest"
|
|
40
|
+
},
|
|
41
|
+
repository: {
|
|
42
|
+
"type": "git",
|
|
43
|
+
"url": "git+https://github.com/fengxinming/vite-plugins.git",
|
|
44
|
+
"directory": "plugins/vite-plugin-mock-data"
|
|
45
|
+
},
|
|
46
|
+
keywords: ["vite-plugin", "vite-plugin-mock-data"],
|
|
47
|
+
author: "Jesse Feng <fxm0016@126.com>",
|
|
48
|
+
license: "MIT",
|
|
49
|
+
bugs: { "url": "https://github.com/fengxinming/vite-plugins/issues" },
|
|
50
|
+
homepage: "https://fengxinming.github.io/vite-plugins/plugins/vite-plugin-mock-data/quick-start",
|
|
51
|
+
dependencies: {
|
|
52
|
+
"find-my-way": "^9.2.0",
|
|
53
|
+
"is-what-type": "^1.1.4",
|
|
54
|
+
"sirv": "^3.0.1",
|
|
55
|
+
"tinyglobby": "^0.2.12",
|
|
56
|
+
"vp-runtime-helper": "workspace:^"
|
|
57
|
+
},
|
|
58
|
+
devDependencies: {
|
|
59
|
+
"vite": "^8.0.0",
|
|
60
|
+
"vite-plugin-dts": "^5.0.3",
|
|
61
|
+
"vite-plugin-external": "workspace:^"
|
|
62
|
+
},
|
|
63
|
+
files: ["dist"]
|
|
16
64
|
};
|
|
65
|
+
//#endregion
|
|
66
|
+
//#region src/configureServer.ts
|
|
67
|
+
/**
|
|
68
|
+
* Simple request body parser for JSON / urlencoded payloads.
|
|
69
|
+
* Populates `req.body` so that route handlers can read it directly,
|
|
70
|
+
* matching the documented handler signature `(req) => req.body`.
|
|
71
|
+
*/
|
|
72
|
+
function readJsonBody(req) {
|
|
73
|
+
return new Promise((resolve, reject) => {
|
|
74
|
+
const chunks = [];
|
|
75
|
+
req.on("data", (c) => chunks.push(Buffer.isBuffer(c) ? c : Buffer.from(c)));
|
|
76
|
+
req.on("end", () => {
|
|
77
|
+
const raw = Buffer.concat(chunks).toString("utf8");
|
|
78
|
+
if (!raw) {
|
|
79
|
+
resolve(void 0);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
const type = (req.headers["content-type"] || "").toLowerCase();
|
|
83
|
+
try {
|
|
84
|
+
if (type.includes("application/json")) resolve(JSON.parse(raw));
|
|
85
|
+
else if (type.includes("application/x-www-form-urlencoded")) {
|
|
86
|
+
const out = {};
|
|
87
|
+
for (const [k, v] of new URLSearchParams(raw)) out[k] = v;
|
|
88
|
+
resolve(out);
|
|
89
|
+
} else resolve(raw);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
reject(e);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
req.on("error", reject);
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
function sendResult(req, res, ret, defaultHeaders) {
|
|
98
|
+
if (res.headersSent || ret === void 0) return;
|
|
99
|
+
send(req, res, typeof ret !== "string" ? JSON.stringify(ret) : ret, isObject(ret) ? "json" : "html", { headers: defaultHeaders });
|
|
100
|
+
}
|
|
17
101
|
function sirvOptions(headers) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
res.setHeader(key, val);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
};
|
|
102
|
+
return {
|
|
103
|
+
dev: true,
|
|
104
|
+
etag: true,
|
|
105
|
+
extensions: [],
|
|
106
|
+
setHeaders(res, pathname) {
|
|
107
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
108
|
+
if (/\.[tj]sx?$/.test(pathname)) res.setHeader("Content-Type", "application/javascript");
|
|
109
|
+
if (headers) Object.entries(headers).forEach(([key, val]) => {
|
|
110
|
+
if (val) res.setHeader(key, val);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
};
|
|
36
114
|
}
|
|
37
115
|
function configureServer(server, routerOpts, routes, cwd) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
116
|
+
const router = getRouter(routerOpts);
|
|
117
|
+
if (Array.isArray(routes)) routes.forEach((route) => {
|
|
118
|
+
Object.keys(route).forEach((xpath) => {
|
|
119
|
+
let [methods, pathname] = xpath.split(" ");
|
|
120
|
+
if (!pathname) {
|
|
121
|
+
pathname = methods;
|
|
122
|
+
methods = "GET";
|
|
123
|
+
}
|
|
124
|
+
methods = methods.toUpperCase();
|
|
125
|
+
let routeConfig = route[xpath];
|
|
126
|
+
if (!isObject(routeConfig)) routeConfig = { handler: routeConfig };
|
|
127
|
+
let handler;
|
|
128
|
+
if (typeof routeConfig.file === "string") handler = (req, res) => {
|
|
129
|
+
const parsedPath = parse(toAbsolutePath(routeConfig.file, cwd));
|
|
130
|
+
const serve = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
|
|
131
|
+
req.url = `/${parsedPath.base}`;
|
|
132
|
+
serve(req, res);
|
|
133
|
+
};
|
|
134
|
+
else if (typeof routeConfig.handler !== "function") {
|
|
135
|
+
const ret = routeConfig.handler;
|
|
136
|
+
const retType = typeof ret;
|
|
137
|
+
handler = (req, res) => {
|
|
138
|
+
send(req, res, retType !== "string" ? JSON.stringify(ret) : ret, isObject(ret) ? "json" : "html", { headers: server.config.server.headers });
|
|
139
|
+
};
|
|
140
|
+
} else {
|
|
141
|
+
const userHandler = routeConfig.handler;
|
|
142
|
+
const needsBody = methods.toUpperCase().split("/").some((m) => [
|
|
143
|
+
"POST",
|
|
144
|
+
"PUT",
|
|
145
|
+
"PATCH",
|
|
146
|
+
"DELETE"
|
|
147
|
+
].includes(m));
|
|
148
|
+
handler = async (req, res, params) => {
|
|
149
|
+
req.params = params ?? {};
|
|
150
|
+
try {
|
|
151
|
+
if (needsBody) req.body = await readJsonBody(req);
|
|
152
|
+
} catch (e) {
|
|
153
|
+
res.statusCode = 400;
|
|
154
|
+
res.setHeader("Content-Type", "application/json");
|
|
155
|
+
res.end(JSON.stringify({
|
|
156
|
+
error: "Invalid request body",
|
|
157
|
+
detail: String(e)
|
|
158
|
+
}));
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
try {
|
|
162
|
+
sendResult(req, res, await userHandler(req, res), server.config.server.headers);
|
|
163
|
+
} catch (e) {
|
|
164
|
+
server.config.logger.error(`[mock-data] handler error on ${methods} ${pathname}: ${e}`);
|
|
165
|
+
if (!res.headersSent) {
|
|
166
|
+
res.statusCode = 500;
|
|
167
|
+
res.setHeader("Content-Type", "application/json");
|
|
168
|
+
res.end(JSON.stringify({
|
|
169
|
+
error: "Mock handler error",
|
|
170
|
+
detail: String(e)
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
if (handler) router.on(methods.split("/"), pathname, {}, handler, routeConfig.store);
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
server.middlewares.use((req, res, next) => {
|
|
180
|
+
router.defaultRoute = () => next();
|
|
181
|
+
router.lookup(req, res);
|
|
182
|
+
});
|
|
81
183
|
}
|
|
82
|
-
|
|
83
|
-
|
|
184
|
+
//#endregion
|
|
185
|
+
//#region src/logger.ts
|
|
186
|
+
var PLUGIN_NAME = name;
|
|
187
|
+
var logger = logFactory.getLogger(PLUGIN_NAME);
|
|
188
|
+
//#endregion
|
|
189
|
+
//#region src/loadRoutes.ts
|
|
190
|
+
var _require = typeof __require === "function" ? __require : createRequire(import.meta.url);
|
|
84
191
|
async function getRoute(filename) {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
break;
|
|
108
|
-
case ".json":
|
|
109
|
-
config = JSON.parse(node_fs.readFileSync(filename, "utf-8"));
|
|
110
|
-
break;
|
|
111
|
-
}
|
|
112
|
-
return config;
|
|
192
|
+
logger.debug("Load mock file:", filename);
|
|
193
|
+
let { ext, dir, name } = parse(filename);
|
|
194
|
+
const isTs = ext === ".ts" || ext === ".mts";
|
|
195
|
+
if (isTs) {
|
|
196
|
+
const { code } = await transformWithOxc(readFileSync(filename, "utf-8"), filename);
|
|
197
|
+
filename = join(dir, `${name}-${PLUGIN_NAME}.mjs`);
|
|
198
|
+
ext = ".mjs";
|
|
199
|
+
await writeFile(filename, code);
|
|
200
|
+
}
|
|
201
|
+
let config;
|
|
202
|
+
switch (ext) {
|
|
203
|
+
case ".js":
|
|
204
|
+
config = _require(filename);
|
|
205
|
+
break;
|
|
206
|
+
case ".mjs":
|
|
207
|
+
config = (await import(filename)).default;
|
|
208
|
+
if (isTs) await unlink(filename);
|
|
209
|
+
break;
|
|
210
|
+
case ".json": config = JSON.parse(readFileSync(filename, "utf-8"));
|
|
211
|
+
}
|
|
212
|
+
return config;
|
|
113
213
|
}
|
|
114
214
|
async function loadRoutes(dir, routes) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (config) {
|
|
119
|
-
routes.push(config);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
215
|
+
const paths = await glob(`${dir}/**/*.{js,mjs,json,ts,mts}`, { absolute: true });
|
|
216
|
+
const configs = await Promise.all(paths.map(getRoute));
|
|
217
|
+
for (const config of configs) if (config) routes.push(config);
|
|
122
218
|
}
|
|
219
|
+
//#endregion
|
|
220
|
+
//#region src/index.ts
|
|
221
|
+
/**
|
|
222
|
+
* Provides a simple way to mock data.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```js
|
|
226
|
+
* import { defineConfig } from 'vite';
|
|
227
|
+
* import pluginMockDate from 'vite-plugin-mock-data';
|
|
228
|
+
*
|
|
229
|
+
* export default defineConfig({
|
|
230
|
+
* plugins: [
|
|
231
|
+
* pluginMockDate({
|
|
232
|
+
* routes: './mock'
|
|
233
|
+
* })
|
|
234
|
+
* ]
|
|
235
|
+
* });
|
|
236
|
+
* ```
|
|
237
|
+
*
|
|
238
|
+
* @param opts Options
|
|
239
|
+
* @returns a vite plugin
|
|
240
|
+
*/
|
|
123
241
|
function pluginMockDate(opts) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
}
|
|
147
|
-
} else if (isWhatType.isObject(routes)) {
|
|
148
|
-
logger.debug("Load routes from", routes);
|
|
149
|
-
allRoutes.push(routes);
|
|
150
|
-
}
|
|
151
|
-
return isAfter ? () => configureServer(server, routerOptions, allRoutes, cwd) : configureServer(server, routerOptions, allRoutes, cwd);
|
|
152
|
-
}
|
|
153
|
-
};
|
|
242
|
+
if (opts.enableBanner) banner(package_default.name);
|
|
243
|
+
const { isAfter, routerOptions, routes, logLevel, cwd = process.cwd() } = opts;
|
|
244
|
+
if (logLevel) logger.level = logLevel;
|
|
245
|
+
const allRoutes = [];
|
|
246
|
+
return {
|
|
247
|
+
name: PLUGIN_NAME,
|
|
248
|
+
async configureServer(server) {
|
|
249
|
+
if (typeof routes === "string") {
|
|
250
|
+
logger.debug("Load routes from", routes);
|
|
251
|
+
await loadRoutes(toAbsolutePath(routes, cwd), allRoutes);
|
|
252
|
+
} else if (Array.isArray(routes)) for (const route of routes) {
|
|
253
|
+
logger.debug("Load routes from", route);
|
|
254
|
+
if (typeof route === "string") await loadRoutes(toAbsolutePath(route, cwd), allRoutes);
|
|
255
|
+
else allRoutes.push(route);
|
|
256
|
+
}
|
|
257
|
+
else if (isObject(routes)) {
|
|
258
|
+
logger.debug("Load routes from", routes);
|
|
259
|
+
allRoutes.push(routes);
|
|
260
|
+
}
|
|
261
|
+
return isAfter ? () => configureServer(server, routerOptions, allRoutes, cwd) : configureServer(server, routerOptions, allRoutes, cwd);
|
|
262
|
+
}
|
|
263
|
+
};
|
|
154
264
|
}
|
|
155
|
-
|
|
265
|
+
//#endregion
|
|
266
|
+
export { pluginMockDate as default };
|
package/dist/logger.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-data",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "8.0.1",
|
|
4
4
|
"description": "Provides a simple way to mock data.",
|
|
5
|
-
"
|
|
6
|
-
"module": "./dist/index.mjs",
|
|
5
|
+
"type": "module",
|
|
7
6
|
"types": "./dist/index.d.ts",
|
|
8
7
|
"exports": {
|
|
9
8
|
".": {
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"require": "./dist/index.js"
|
|
9
|
+
"import": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts"
|
|
13
11
|
}
|
|
14
12
|
},
|
|
15
13
|
"engines": {
|
|
16
|
-
"node": ">=
|
|
17
|
-
"vite": ">=
|
|
14
|
+
"node": "^20.19.0 || >=22.12.0",
|
|
15
|
+
"vite": ">=8.0.0"
|
|
18
16
|
},
|
|
19
17
|
"repository": {
|
|
20
18
|
"type": "git",
|
|
21
19
|
"url": "git+https://github.com/fengxinming/vite-plugins.git",
|
|
22
|
-
"directory": "
|
|
20
|
+
"directory": "plugins/vite-plugin-mock-data"
|
|
23
21
|
},
|
|
24
22
|
"keywords": [
|
|
25
23
|
"vite-plugin",
|
|
@@ -36,12 +34,12 @@
|
|
|
36
34
|
"is-what-type": "^1.1.4",
|
|
37
35
|
"sirv": "^3.0.1",
|
|
38
36
|
"tinyglobby": "^0.2.12",
|
|
39
|
-
"vp-runtime-helper": "^
|
|
37
|
+
"vp-runtime-helper": "^8.0.0"
|
|
40
38
|
},
|
|
41
39
|
"devDependencies": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"vite": "^
|
|
40
|
+
"vite": "^8.0.0",
|
|
41
|
+
"vite-plugin-dts": "^5.0.3",
|
|
42
|
+
"vite-plugin-external": "^8.0.2"
|
|
45
43
|
},
|
|
46
44
|
"files": [
|
|
47
45
|
"dist"
|
|
@@ -49,6 +47,8 @@
|
|
|
49
47
|
"scripts": {
|
|
50
48
|
"build": "vite build",
|
|
51
49
|
"watch": "vite build --watch",
|
|
52
|
-
"release": "pnpm publish --no-git-checks"
|
|
50
|
+
"release": "pnpm publish --no-git-checks",
|
|
51
|
+
"test": "vitest run",
|
|
52
|
+
"test:watch": "vitest"
|
|
53
53
|
}
|
|
54
54
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { isObject } from "is-what-type";
|
|
2
|
-
import { toAbsolutePath, logFactory, banner } from "vp-runtime-helper";
|
|
3
|
-
import { parse, join } from "node:path";
|
|
4
|
-
import getRouter from "find-my-way";
|
|
5
|
-
import sirv from "sirv";
|
|
6
|
-
import { send, transformWithEsbuild } from "vite";
|
|
7
|
-
import { readFileSync } from "node:fs";
|
|
8
|
-
import { writeFile, unlink } from "node:fs/promises";
|
|
9
|
-
import { createRequire } from "node:module";
|
|
10
|
-
import { glob } from "tinyglobby";
|
|
11
|
-
const name = "vite-plugin-mock-data";
|
|
12
|
-
const pkg = {
|
|
13
|
-
name
|
|
14
|
-
};
|
|
15
|
-
function sirvOptions(headers) {
|
|
16
|
-
return {
|
|
17
|
-
dev: true,
|
|
18
|
-
etag: true,
|
|
19
|
-
extensions: [],
|
|
20
|
-
setHeaders(res, pathname) {
|
|
21
|
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
22
|
-
if (/\.[tj]sx?$/.test(pathname)) {
|
|
23
|
-
res.setHeader("Content-Type", "application/javascript");
|
|
24
|
-
}
|
|
25
|
-
if (headers) {
|
|
26
|
-
Object.entries(headers).forEach(([key, val]) => {
|
|
27
|
-
if (val) {
|
|
28
|
-
res.setHeader(key, val);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
}
|
|
35
|
-
function configureServer(server, routerOpts, routes, cwd) {
|
|
36
|
-
const router = getRouter(routerOpts);
|
|
37
|
-
if (Array.isArray(routes)) {
|
|
38
|
-
routes.forEach((route) => {
|
|
39
|
-
Object.keys(route).forEach((xpath) => {
|
|
40
|
-
let [methods, pathname] = xpath.split(" ");
|
|
41
|
-
if (!pathname) {
|
|
42
|
-
pathname = methods;
|
|
43
|
-
methods = "GET";
|
|
44
|
-
}
|
|
45
|
-
methods = methods.toUpperCase();
|
|
46
|
-
let routeConfig = route[xpath];
|
|
47
|
-
if (!isObject(routeConfig)) {
|
|
48
|
-
routeConfig = { handler: routeConfig };
|
|
49
|
-
}
|
|
50
|
-
let handler;
|
|
51
|
-
if (typeof routeConfig.file === "string") {
|
|
52
|
-
handler = (req, res) => {
|
|
53
|
-
const parsedPath = parse(toAbsolutePath(routeConfig.file, cwd));
|
|
54
|
-
const serve = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
|
|
55
|
-
req.url = `/${parsedPath.base}`;
|
|
56
|
-
serve(req, res);
|
|
57
|
-
};
|
|
58
|
-
} else if (typeof routeConfig.handler !== "function") {
|
|
59
|
-
const ret = routeConfig.handler;
|
|
60
|
-
const retType = typeof ret;
|
|
61
|
-
handler = (req, res) => {
|
|
62
|
-
send(req, res, retType !== "string" ? JSON.stringify(ret) : ret, isObject(ret) ? "json" : "html", {
|
|
63
|
-
headers: server.config.server.headers
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
} else {
|
|
67
|
-
handler = routeConfig.handler;
|
|
68
|
-
}
|
|
69
|
-
if (handler) {
|
|
70
|
-
router.on(methods.split("/"), pathname, {}, handler, routeConfig.store);
|
|
71
|
-
}
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
server.middlewares.use((req, res, next) => {
|
|
76
|
-
router.defaultRoute = () => next();
|
|
77
|
-
router.lookup(req, res);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
const PLUGIN_NAME = name;
|
|
81
|
-
const logger = logFactory.getLogger(PLUGIN_NAME);
|
|
82
|
-
async function getRoute(filename) {
|
|
83
|
-
logger.debug("Load mock file:", filename);
|
|
84
|
-
let { ext, dir, name: name2 } = parse(filename);
|
|
85
|
-
const isTs = ext === ".ts" || ext === ".mts";
|
|
86
|
-
if (isTs) {
|
|
87
|
-
const { code } = await transformWithEsbuild(readFileSync(filename, "utf-8"), filename, {
|
|
88
|
-
loader: "ts",
|
|
89
|
-
target: "esnext"
|
|
90
|
-
});
|
|
91
|
-
filename = join(dir, `${name2}-${PLUGIN_NAME}.mjs`);
|
|
92
|
-
ext = ".mjs";
|
|
93
|
-
await writeFile(filename, code);
|
|
94
|
-
}
|
|
95
|
-
let config;
|
|
96
|
-
switch (ext) {
|
|
97
|
-
case ".js":
|
|
98
|
-
config = createRequire(import.meta.url)(filename);
|
|
99
|
-
break;
|
|
100
|
-
case ".mjs":
|
|
101
|
-
config = (await import(filename)).default;
|
|
102
|
-
if (isTs) {
|
|
103
|
-
await unlink(filename);
|
|
104
|
-
}
|
|
105
|
-
break;
|
|
106
|
-
case ".json":
|
|
107
|
-
config = JSON.parse(readFileSync(filename, "utf-8"));
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
return config;
|
|
111
|
-
}
|
|
112
|
-
async function loadRoutes(dir, routes) {
|
|
113
|
-
const paths = await glob(`${dir}/**/*.{js,mjs,json,ts,mts}`, { absolute: true });
|
|
114
|
-
const configs = await Promise.all(paths.map(getRoute));
|
|
115
|
-
for (const config of configs) {
|
|
116
|
-
if (config) {
|
|
117
|
-
routes.push(config);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
function pluginMockDate(opts) {
|
|
122
|
-
if (opts.enableBanner) {
|
|
123
|
-
banner(pkg.name);
|
|
124
|
-
}
|
|
125
|
-
const { isAfter, routerOptions, routes, logLevel, cwd = process.cwd() } = opts;
|
|
126
|
-
if (logLevel) {
|
|
127
|
-
logger.level = logLevel;
|
|
128
|
-
}
|
|
129
|
-
const allRoutes = [];
|
|
130
|
-
return {
|
|
131
|
-
name: PLUGIN_NAME,
|
|
132
|
-
async configureServer(server) {
|
|
133
|
-
if (typeof routes === "string") {
|
|
134
|
-
logger.debug("Load routes from", routes);
|
|
135
|
-
await loadRoutes(toAbsolutePath(routes, cwd), allRoutes);
|
|
136
|
-
} else if (Array.isArray(routes)) {
|
|
137
|
-
for (const route of routes) {
|
|
138
|
-
logger.debug("Load routes from", route);
|
|
139
|
-
if (typeof route === "string") {
|
|
140
|
-
await loadRoutes(toAbsolutePath(route, cwd), allRoutes);
|
|
141
|
-
} else {
|
|
142
|
-
allRoutes.push(route);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
} else if (isObject(routes)) {
|
|
146
|
-
logger.debug("Load routes from", routes);
|
|
147
|
-
allRoutes.push(routes);
|
|
148
|
-
}
|
|
149
|
-
return isAfter ? () => configureServer(server, routerOptions, allRoutes, cwd) : configureServer(server, routerOptions, allRoutes, cwd);
|
|
150
|
-
}
|
|
151
|
-
};
|
|
152
|
-
}
|
|
153
|
-
export {
|
|
154
|
-
pluginMockDate as default
|
|
155
|
-
};
|