mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
import { mock } from "../api/service";
|
|
import * as tools from "../api/tools";
|
|
import _ from "lodash";
|
|
const commonMocks = import.meta.globEager("./common/mock.*.js");
|
|
const apiMocks = import.meta.globEager("../api/modules/*.mock.ts");
|
|
const viewMocks = import.meta.globEager("../views/**/mock.js");
|
|
|
|
const list = [];
|
|
_.forEach(commonMocks, (value) => {
|
|
list.push(value.default);
|
|
});
|
|
_.forEach(apiMocks, (value) => {
|
|
list.push(value.default);
|
|
});
|
|
_.forEach(viewMocks, (value) => {
|
|
list.push(value.default);
|
|
});
|
|
|
|
list.forEach((apiFile) => {
|
|
for (const item of apiFile) {
|
|
mock.onAny(new RegExp(item.path)).reply(async (config) => {
|
|
console.log("------------fake request start -------------");
|
|
console.log("request:", config);
|
|
const data = config.data ? JSON.parse(config.data) : {};
|
|
const query = config.url.indexOf("?") >= 0 ? config.url.substring(config.url.indexOf("?") + 1) : undefined;
|
|
const params = config.params || {};
|
|
if (query) {
|
|
const arr = query.split("&");
|
|
for (const item of arr) {
|
|
const kv = item.split("=");
|
|
params[kv[0]] = kv[1];
|
|
}
|
|
}
|
|
|
|
const req = {
|
|
body: data,
|
|
params: params
|
|
};
|
|
const ret = await item.handle(req);
|
|
console.log("response:", ret);
|
|
console.log("------------fake request end-------------");
|
|
if (ret.code === 0) {
|
|
return tools.responseSuccess(ret.data, ret.msg);
|
|
} else {
|
|
return tools.responseError(ret.data, ret.msg, ret.code);
|
|
}
|
|
});
|
|
}
|
|
});
|