mirror of
https://github.com/WeBankFinTech/fes.js.git
synced 2025-04-06 03:59:53 +08:00
feat: 优化mock
This commit is contained in:
parent
5117afc9c5
commit
81d3405251
@ -27,9 +27,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@fesjs/compiler": "^2.0.5",
|
"@fesjs/compiler": "^2.0.5",
|
||||||
"@fesjs/utils": "^2.0.4",
|
"@fesjs/utils": "^2.0.4",
|
||||||
"cookie": "^0.4.2",
|
|
||||||
"envinfo": "^7.7.3",
|
"envinfo": "^7.7.3",
|
||||||
"mime": "^3.0.0",
|
|
||||||
"mockjs": "^1.1.0",
|
"mockjs": "^1.1.0",
|
||||||
"express": "^4.17.3"
|
"express": "^4.17.3"
|
||||||
},
|
},
|
||||||
|
@ -2,26 +2,6 @@ import { existsSync, readFileSync } from 'fs';
|
|||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { chokidar, lodash, parseRequireDeps } from '@fesjs/utils';
|
import { chokidar, lodash, parseRequireDeps } from '@fesjs/utils';
|
||||||
|
|
||||||
function getContentType(type) {
|
|
||||||
const mime = require('mime');
|
|
||||||
return type.indexOf('/') === -1 ? mime.getType(type) : type;
|
|
||||||
}
|
|
||||||
|
|
||||||
function setCookie(res, name, value, opts = {}) {
|
|
||||||
const val = typeof value === 'object' ? `j:${JSON.stringify(value)}` : String(value);
|
|
||||||
|
|
||||||
if ('maxAge' in opts) {
|
|
||||||
opts.expires = new Date(Date.now() + opts.maxAge);
|
|
||||||
opts.maxAge /= 1000;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opts.path == null) {
|
|
||||||
opts.path = '/';
|
|
||||||
}
|
|
||||||
const cookie = require('cookie');
|
|
||||||
res.setHeader('Set-Cookie', cookie.serialize(name, String(val), opts));
|
|
||||||
}
|
|
||||||
|
|
||||||
export default (api) => {
|
export default (api) => {
|
||||||
let mockFlag = false; // mock 开关flag
|
let mockFlag = false; // mock 开关flag
|
||||||
let mockPrefix = '/'; // mock 过滤前缀
|
let mockPrefix = '/'; // mock 过滤前缀
|
||||||
@ -153,44 +133,39 @@ export default (api) => {
|
|||||||
|
|
||||||
app.use((req, res, next) => {
|
app.use((req, res, next) => {
|
||||||
// 如果请求不是以 cgiMock.prefix 开头,直接 next
|
// 如果请求不是以 cgiMock.prefix 开头,直接 next
|
||||||
if (!req.url.startsWith(mockPrefix)) {
|
if (!req.path.startsWith(mockPrefix)) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
// 请求以 cgiMock.prefix 开头,匹配处理
|
// 请求以 cgiMock.prefix 开头,匹配处理
|
||||||
const matchRequet = requestList.find((item) => req.url.search(item.url) !== -1);
|
const matchRequet = requestList.find((item) => req.path.search(item.url) !== -1);
|
||||||
if (!matchRequet) {
|
if (!matchRequet) {
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
const sendData = () => {
|
const sendData = () => {
|
||||||
if (matchRequet.headers) {
|
// set header
|
||||||
for (const [key, value] of Object.entries(matchRequet.headers)) {
|
res.set(matchRequet.headers);
|
||||||
res.setHeader(key, value);
|
// set Content-Type
|
||||||
}
|
matchRequet.type && res.type(matchRequet.type);
|
||||||
}
|
|
||||||
if (matchRequet.type) {
|
|
||||||
res.setHeader('Content-Type', getContentType(matchRequet.type));
|
|
||||||
}
|
|
||||||
// set status code
|
// set status code
|
||||||
res.statusCode = matchRequet.statusCode;
|
res.status(matchRequet.statusCode);
|
||||||
// set cookie
|
// set cookie
|
||||||
traversalHandler(matchRequet.cookies, (item) => {
|
traversalHandler(matchRequet.cookies, (item) => {
|
||||||
const name = item.name;
|
const name = item.name;
|
||||||
const value = item.value;
|
const value = item.value;
|
||||||
delete item.name;
|
delete item.name;
|
||||||
delete item.value;
|
delete item.value;
|
||||||
setCookie(res, name, value, item);
|
res.cookie(name, value, item);
|
||||||
});
|
});
|
||||||
|
|
||||||
// do result
|
// do result
|
||||||
if (lodash.isFunction(matchRequet.result)) {
|
if (lodash.isFunction(matchRequet.result)) {
|
||||||
matchRequet.result(req, res);
|
matchRequet.result(req, res);
|
||||||
} else if (lodash.isArray(matchRequet.result) || lodash.isPlainObject(matchRequet.result)) {
|
} else if (lodash.isArray(matchRequet.result) || lodash.isPlainObject(matchRequet.result)) {
|
||||||
!matchRequet.type && res.setHeader('Content-Type', getContentType('json'));
|
!matchRequet.type && res.type('json');
|
||||||
res.end(JSON.stringify(matchRequet.result));
|
res.json(matchRequet.result);
|
||||||
} else {
|
} else {
|
||||||
!matchRequet.type && res.setHeader('Content-Type', getContentType('text'));
|
!matchRequet.type && res.type('text');
|
||||||
res.end(matchRequet.result.toString());
|
res.send(matchRequet.result.toString());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user