mirror of
https://gitee.com/WeBank/fes.js.git
synced 2024-12-02 19:58:18 +08:00
fix: mock插件修改pr问题
This commit is contained in:
parent
5d2271bcd3
commit
0cccac38f7
@ -5,7 +5,6 @@ import bodyParser from 'body-parser';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import Mock from 'mockjs';
|
||||
|
||||
|
||||
export default (api) => {
|
||||
let mockFlag = false; // mock 开关flag
|
||||
let mockPrefix = '/'; // mock 过滤前缀
|
||||
@ -21,58 +20,56 @@ export default (api) => {
|
||||
}
|
||||
});
|
||||
|
||||
const createMock = () => {
|
||||
// 判断是否为 Object,仅 {}
|
||||
function isObject(value) {
|
||||
return Object.prototype.toString.call(value) === '[object Object]';
|
||||
// 对 array、object 遍历处理
|
||||
function traversalHandler(val, callback) {
|
||||
if (lodash.isArray(val)) {
|
||||
val.forEach(callback);
|
||||
}
|
||||
// 对 array、object 遍历处理
|
||||
function traversalHandler(val, callback) {
|
||||
if (lodash.isArray(val)) {
|
||||
val.forEach(callback);
|
||||
}
|
||||
if (isObject(val)) {
|
||||
Object.keys(val).forEach((key) => { callback(val[key], key); });
|
||||
}
|
||||
}
|
||||
// 根据参数个数获取配置
|
||||
function getOption(arg) {
|
||||
const len = arg.length;
|
||||
// 默认配置
|
||||
const option = {
|
||||
headers: {
|
||||
'Cache-Control': 'no-cache'
|
||||
},
|
||||
statusCode: 200,
|
||||
cookies: [],
|
||||
timeout: 0
|
||||
};
|
||||
if (len === 0) return option;
|
||||
if (len === 1) {
|
||||
const newOption = arg[0];
|
||||
if (isObject(newOption)) {
|
||||
traversalHandler(newOption, (value, key) => {
|
||||
if (key === 'headers') {
|
||||
traversalHandler(newOption.headers, (headervalue, headerkey) => {
|
||||
option.headers[headerkey] = newOption.headers[headerkey];
|
||||
});
|
||||
} else {
|
||||
option[key] = newOption[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
option.url = arg[0];
|
||||
option.result = arg[1];
|
||||
}
|
||||
return option;
|
||||
}
|
||||
// 把基于 cgiMockfile 的相对绝对转成绝对路径
|
||||
function parsePath(value) {
|
||||
const PROJECT_DIR = process.env.PWD || process.cwd();
|
||||
return resolve(PROJECT_DIR, value);
|
||||
if (lodash.isPlainObject(val)) {
|
||||
Object.keys(val).forEach((key) => { callback(val[key], key); });
|
||||
}
|
||||
}
|
||||
|
||||
// 根据参数个数获取配置
|
||||
function getOption(arg) {
|
||||
const len = arg.length;
|
||||
// 默认配置
|
||||
const option = {
|
||||
headers: {
|
||||
'Cache-Control': 'no-cache'
|
||||
},
|
||||
statusCode: 200,
|
||||
cookies: [],
|
||||
timeout: 0
|
||||
};
|
||||
if (len === 0) return option;
|
||||
if (len === 1) {
|
||||
const newOption = arg[0];
|
||||
if (lodash.isPlainObject(newOption)) {
|
||||
traversalHandler(newOption, (value, key) => {
|
||||
if (key === 'headers') {
|
||||
traversalHandler(newOption.headers, (headervalue, headerkey) => {
|
||||
option.headers[headerkey] = newOption.headers[headerkey];
|
||||
});
|
||||
} else {
|
||||
option[key] = newOption[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
option.url = arg[0];
|
||||
option.result = arg[1];
|
||||
}
|
||||
return option;
|
||||
}
|
||||
|
||||
// 把基于 cgiMockfile 的相对绝对转成绝对路径
|
||||
function parsePath(value) {
|
||||
const PROJECT_DIR = process.env.PWD || api.cwd;
|
||||
return resolve(PROJECT_DIR, value);
|
||||
}
|
||||
|
||||
const createMock = () => {
|
||||
const requestList = [];
|
||||
const cgiMock = (...arg) => {
|
||||
const option = getOption(arg);
|
||||
@ -83,11 +80,9 @@ export default (api) => {
|
||||
return readFileSync(parsePath(file));
|
||||
};
|
||||
|
||||
// mock是否打开
|
||||
mockFlag = isObject(api.config.mock) ? true : api.config.mock;
|
||||
if (!mockFlag) return;
|
||||
// mock打开情况下,配置的过滤前缀
|
||||
mockPrefix = api.config.mock.prefix || mockPrefix;
|
||||
const mockPrefixTemp = api.config.mock.prefix || mockPrefix;
|
||||
mockPrefix = mockPrefixTemp === mockPrefix ? mockPrefixTemp : `${mockPrefixTemp}/`;
|
||||
// mock文件处理
|
||||
mockFile = parsePath('./mock.js');
|
||||
if (!existsSync(mockFile)) {
|
||||
@ -97,21 +92,24 @@ export default (api) => {
|
||||
if (require.cache[mockFile]) {
|
||||
delete require.cache[mockFile];
|
||||
}
|
||||
const projectMock = require(mockFile);
|
||||
if (!lodash.isFunction(projectMock)) {
|
||||
api.logger.info('mock.js should export Function'); return;
|
||||
// require最新的 mock.js 文件
|
||||
try {
|
||||
const projectMock = require(mockFile);
|
||||
if (!lodash.isFunction(projectMock)) {
|
||||
api.logger.info('mock.js should export Function'); return;
|
||||
}
|
||||
projectMock({ cgiMock, Mock });
|
||||
} catch (err) {
|
||||
api.logger.info('mock.js run fail!');
|
||||
}
|
||||
// mock对象与 mock.js 结合
|
||||
projectMock(cgiMock, Mock);
|
||||
|
||||
return (req, res, next) => {
|
||||
// 如果请求不是以 cgiMock.prefix 开头,直接 next `${mockPrefix}/`
|
||||
if (!req.path.startsWith(`${mockPrefix}/`)) {
|
||||
// 如果请求不是以 cgiMock.prefix 开头,直接 next
|
||||
if (!req.path.startsWith(mockPrefix)) {
|
||||
return next();
|
||||
}
|
||||
|
||||
// 请求以 cgiMock.prefix 开头,匹配处理
|
||||
const matchRequet = requestList.filter(item => req.path.search(item.url) !== -1)[0];
|
||||
const matchRequet = requestList.find(item => req.path.search(item.url) !== -1);
|
||||
if (!matchRequet) {
|
||||
return next();
|
||||
}
|
||||
@ -135,7 +133,7 @@ export default (api) => {
|
||||
if (lodash.isFunction(matchRequet.result)) {
|
||||
matchRequet.result(req, res);
|
||||
} else if (
|
||||
lodash.isArray(matchRequet.result) || isObject(matchRequet.result)
|
||||
lodash.isArray(matchRequet.result) || lodash.isPlainObject(matchRequet.result)
|
||||
) {
|
||||
!matchRequet.type && res.type('json');
|
||||
res.json(matchRequet.result);
|
||||
@ -147,9 +145,11 @@ export default (api) => {
|
||||
};
|
||||
|
||||
api.onStart(() => {
|
||||
loadMock = createMock();
|
||||
// 获取mock配置: 是否打开
|
||||
mockFlag = lodash.isPlainObject(api.config.mock) ? true : api.config.mock;
|
||||
if (!mockFlag) return;
|
||||
|
||||
loadMock = createMock();
|
||||
chokidar.watch(mockFile, {
|
||||
ignoreInitial: true
|
||||
}).on('change', () => {
|
||||
@ -157,6 +157,7 @@ export default (api) => {
|
||||
loadMock = createMock();
|
||||
});
|
||||
});
|
||||
|
||||
api.addBeforeMiddlewares(() => {
|
||||
if (!mockFlag) return [];
|
||||
return [
|
||||
|
@ -1,4 +1,4 @@
|
||||
module.exports = (cgiMock, Mock) => {
|
||||
module.exports = ({ cgiMock, Mock }) => {
|
||||
const { Random } = Mock;
|
||||
|
||||
// 测试 proxy 与 mock 用例集合
|
||||
|
Loading…
Reference in New Issue
Block a user