yapi/server/middleware/mockServer.js

118 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-07-10 20:02:44 +08:00
import yapi from '../yapi.js';
2017-07-27 19:49:26 +08:00
import projectModel from '../models/project.js';
import interfaceModel from '../models/interface.js';
import mockExtra from '../../common/mock-extra.js';
2017-08-16 17:13:14 +08:00
import _ from 'underscore';
2017-08-31 18:04:45 +08:00
import Mock from 'mockjs';
2017-08-16 17:13:14 +08:00
function matchApi(apiPath, apiRule) {
let apiPaths = apiPath.split("/");
let apiRules = apiRule.split("/");
if (apiPaths.length !== apiRules.length) {
return false;
}
for (let i = 0; i < apiRules.length; i++) {
if (apiRules[i] && apiRules[i].indexOf(":") !== 0) {
if (apiRules[i] !== apiPaths[i]) {
return false;
}
}
}
return true;
}
2017-07-10 20:02:44 +08:00
module.exports = async (ctx, next) => {
2017-07-27 19:49:26 +08:00
yapi.commons.log('Server Recevie Request...');
let hostname = ctx.hostname;
2017-07-10 20:02:44 +08:00
let config = yapi.WEBCONFIG;
2017-08-15 12:08:59 +08:00
let path = ctx.path;
2017-08-16 17:13:14 +08:00
2017-07-27 19:49:26 +08:00
2017-08-15 12:08:59 +08:00
if (path.indexOf('/mock/') !== 0) {
2017-07-27 19:49:26 +08:00
if (next) await next();
2017-07-10 20:02:44 +08:00
return true;
}
2017-07-27 19:49:26 +08:00
2017-08-15 12:08:59 +08:00
let paths = path.split("/");
let projectId = paths[2];
paths.splice(0, 3);
path = "/" + paths.join("/");
2017-08-16 17:13:14 +08:00
if (!projectId) {
2017-08-15 12:08:59 +08:00
return ctx.body = yapi.commons.resReturn(null, 400, 'projectId不能为空');
}
2017-07-27 19:49:26 +08:00
yapi.commons.log('MockServer Running...');
2017-08-15 12:08:59 +08:00
let projectInst = yapi.getInst(projectModel), project;
2017-07-27 19:49:26 +08:00
try {
2017-08-15 12:08:59 +08:00
project = await projectInst.get(projectId);
2017-07-27 19:49:26 +08:00
} catch (e) {
2017-07-10 20:02:44 +08:00
return ctx.body = yapi.commons.resReturn(null, 403, e.message);
}
2017-07-27 19:49:26 +08:00
2017-08-15 12:08:59 +08:00
if (project === false) {
return ctx.body = yapi.commons.resReturn(null, 400, '不存在的项目');
2017-07-10 20:02:44 +08:00
}
2017-08-16 17:13:14 +08:00
let interfaceData, newData, newpath;
2017-07-10 20:02:44 +08:00
let interfaceInst = yapi.getInst(interfaceModel);
2017-07-27 19:49:26 +08:00
try {
2017-08-16 17:13:14 +08:00
newpath = path.substr(project.basepath.length);
interfaceData = await interfaceInst.getByPath(project._id, newpath, ctx.method);
2017-07-27 19:49:26 +08:00
if (!interfaceData || interfaceData.length === 0) {
//非正常跨域预检请求回应
2017-08-16 17:13:14 +08:00
if (ctx.method === 'OPTIONS') {
ctx.set("Access-Control-Allow-Origin", "*")
ctx.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
return ctx.body = 'ok'
}
2017-08-16 17:13:14 +08:00
let newData = await interfaceInst.getVar(project._id, ctx.method);
let findInterface = _.find(newData, (item) => {
return matchApi(newpath, item.path)
});
if (!findInterface) {
return ctx.body = yapi.commons.resReturn(null, 404, '不存在的api');
}
interfaceData = [
await interfaceInst.get(findInterface._id)
]
2017-07-10 20:02:44 +08:00
}
2017-07-27 19:49:26 +08:00
if (interfaceData.length > 1) {
2017-07-10 20:02:44 +08:00
return ctx.body = yapi.commons.resReturn(null, 405, '存在多个api请检查数据库');
}
interfaceData = interfaceData[0];
ctx.set("Access-Control-Allow-Origin", "*")
2017-07-27 19:49:26 +08:00
if (interfaceData.res_body_type === 'json') {
2017-08-16 17:13:14 +08:00
try {
const res = mockExtra(
yapi.commons.json_parse(interfaceData.res_body),
{
query: ctx.request.query,
body: ctx.request.body
}
);
2017-09-01 10:58:37 +08:00
console.log(interfaceData.res_body);
console.log(res)
2017-08-31 18:04:45 +08:00
return ctx.body = Mock.mock(res);
2017-08-16 17:13:14 +08:00
} catch (e) {
yapi.commons.log(e, 'error')
return ctx.body = {
errcode: 400,
errmsg: 'mock json数据格式有误',
data: interfaceData.res_body
}
}
2017-07-10 20:02:44 +08:00
}
return ctx.body = interfaceData.res_body;
2017-07-27 19:49:26 +08:00
} catch (e) {
2017-08-16 17:13:14 +08:00
console.error(e)
2017-07-10 20:02:44 +08:00
return ctx.body = yapi.commons.resReturn(null, 409, e.message);
}
2017-08-10 12:14:35 +08:00
};