2017-07-10 20:02:44 +08:00
|
|
|
|
'use strict';
|
2017-07-05 16:17:58 +08:00
|
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
|
var _regenerator = require('babel-runtime/regenerator');
|
2017-07-05 16:17:58 +08:00
|
|
|
|
|
|
|
|
|
var _regenerator2 = _interopRequireDefault(_regenerator);
|
|
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
|
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
|
2017-07-05 16:17:58 +08:00
|
|
|
|
|
|
|
|
|
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
|
|
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
|
var _yapi = require('../yapi.js');
|
|
|
|
|
|
|
|
|
|
var _yapi2 = _interopRequireDefault(_yapi);
|
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
var _project = require('../models/project.js');
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
var _project2 = _interopRequireDefault(_project);
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
|
|
|
|
var _interface = require('../models/interface.js');
|
|
|
|
|
|
|
|
|
|
var _interface2 = _interopRequireDefault(_interface);
|
|
|
|
|
|
|
|
|
|
var _mockjs = require('mockjs');
|
|
|
|
|
|
|
|
|
|
var _mockjs2 = _interopRequireDefault(_mockjs);
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
var _underscore = require('underscore');
|
|
|
|
|
|
|
|
|
|
var _underscore2 = _interopRequireDefault(_underscore);
|
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
function matchApi(apiPath, apiRule) {
|
|
|
|
|
var apiPaths = apiPath.split("/");
|
|
|
|
|
var apiRules = apiRule.split("/");
|
|
|
|
|
if (apiPaths.length !== apiRules.length) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
for (var i = 0; i < apiRules.length; i++) {
|
|
|
|
|
if (apiRules[i] && apiRules[i].indexOf(":") !== 0) {
|
|
|
|
|
if (apiRules[i] !== apiPaths[i]) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
module.exports = function () {
|
|
|
|
|
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
|
2017-08-16 17:13:14 +08:00
|
|
|
|
var hostname, config, path, paths, projectId, projectInst, project, interfaceData, newData, newpath, interfaceInst, _newData, findInterface, res;
|
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
|
return _regenerator2.default.wrap(function _callee$(_context) {
|
|
|
|
|
while (1) {
|
|
|
|
|
switch (_context.prev = _context.next) {
|
|
|
|
|
case 0:
|
2017-07-26 15:31:16 +08:00
|
|
|
|
_yapi2.default.commons.log('Server Recevie Request...');
|
2017-07-27 19:49:26 +08:00
|
|
|
|
|
2017-07-20 15:26:26 +08:00
|
|
|
|
hostname = ctx.hostname;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
config = _yapi2.default.WEBCONFIG;
|
2017-08-15 12:08:59 +08:00
|
|
|
|
path = ctx.path;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
if (!(path.indexOf('/mock/') !== 0)) {
|
|
|
|
|
_context.next = 9;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-05 16:17:58 +08:00
|
|
|
|
|
|
|
|
|
if (!next) {
|
2017-08-15 12:08:59 +08:00
|
|
|
|
_context.next = 8;
|
2017-07-05 16:17:58 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
_context.next = 8;
|
2017-07-05 16:17:58 +08:00
|
|
|
|
return next();
|
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
case 8:
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return _context.abrupt('return', true);
|
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
case 9:
|
|
|
|
|
paths = path.split("/");
|
|
|
|
|
projectId = paths[2];
|
2017-07-27 19:49:26 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
paths.splice(0, 3);
|
|
|
|
|
path = "/" + paths.join("/");
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
if (projectId) {
|
|
|
|
|
_context.next = 15;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, 'projectId不能为空'));
|
2017-07-26 15:31:16 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
case 15:
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
_yapi2.default.commons.log('MockServer Running...');
|
|
|
|
|
projectInst = _yapi2.default.getInst(_project2.default), project = void 0;
|
|
|
|
|
_context.prev = 17;
|
|
|
|
|
_context.next = 20;
|
|
|
|
|
return projectInst.get(projectId);
|
|
|
|
|
|
|
|
|
|
case 20:
|
|
|
|
|
project = _context.sent;
|
|
|
|
|
_context.next = 26;
|
|
|
|
|
break;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
case 23:
|
|
|
|
|
_context.prev = 23;
|
|
|
|
|
_context.t0 = _context['catch'](17);
|
|
|
|
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message));
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
case 26:
|
|
|
|
|
if (!(project === false)) {
|
|
|
|
|
_context.next = 28;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的项目'));
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-15 12:08:59 +08:00
|
|
|
|
case 28:
|
2017-08-16 17:13:14 +08:00
|
|
|
|
interfaceData = void 0, newData = void 0, newpath = void 0;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
interfaceInst = _yapi2.default.getInst(_interface2.default);
|
2017-08-15 12:08:59 +08:00
|
|
|
|
_context.prev = 30;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
newpath = path.substr(project.basepath.length);
|
|
|
|
|
_context.next = 34;
|
|
|
|
|
return interfaceInst.getByPath(project._id, newpath, ctx.method);
|
|
|
|
|
|
|
|
|
|
case 34:
|
2017-07-10 20:02:44 +08:00
|
|
|
|
interfaceData = _context.sent;
|
|
|
|
|
|
|
|
|
|
if (!(!interfaceData || interfaceData.length === 0)) {
|
2017-08-16 17:13:14 +08:00
|
|
|
|
_context.next = 50;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-31 10:15:16 +08:00
|
|
|
|
if (!(ctx.method === 'OPTIONS')) {
|
2017-08-16 17:13:14 +08:00
|
|
|
|
_context.next = 40;
|
2017-07-31 10:15:16 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.set("Access-Control-Allow-Origin", "*");
|
|
|
|
|
ctx.set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
|
|
|
|
|
return _context.abrupt('return', ctx.body = 'ok');
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 40:
|
|
|
|
|
_context.next = 42;
|
|
|
|
|
return interfaceInst.getVar(project._id, ctx.method);
|
|
|
|
|
|
|
|
|
|
case 42:
|
|
|
|
|
_newData = _context.sent;
|
|
|
|
|
findInterface = _underscore2.default.find(_newData, function (item) {
|
|
|
|
|
return matchApi(newpath, item.path);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (findInterface) {
|
|
|
|
|
_context.next = 46;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-10 20:02:44 +08:00
|
|
|
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '不存在的api'));
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 46:
|
|
|
|
|
_context.next = 48;
|
|
|
|
|
return interfaceInst.get(findInterface._id);
|
|
|
|
|
|
|
|
|
|
case 48:
|
|
|
|
|
_context.t1 = _context.sent;
|
|
|
|
|
interfaceData = [_context.t1];
|
|
|
|
|
|
|
|
|
|
case 50:
|
2017-07-10 20:02:44 +08:00
|
|
|
|
if (!(interfaceData.length > 1)) {
|
2017-08-16 17:13:14 +08:00
|
|
|
|
_context.next = 52;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '存在多个api,请检查数据库'));
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 52:
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
|
|
|
|
interfaceData = interfaceData[0];
|
2017-07-31 10:15:16 +08:00
|
|
|
|
ctx.set("Access-Control-Allow-Origin", "*");
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
|
|
|
|
if (!(interfaceData.res_body_type === 'json')) {
|
2017-08-16 17:13:14 +08:00
|
|
|
|
_context.next = 63;
|
2017-07-10 20:02:44 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
_context.prev = 55;
|
2017-08-10 12:14:35 +08:00
|
|
|
|
res = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body));
|
|
|
|
|
return _context.abrupt('return', ctx.body = res);
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 60:
|
|
|
|
|
_context.prev = 60;
|
|
|
|
|
_context.t2 = _context['catch'](55);
|
2017-08-01 15:47:06 +08:00
|
|
|
|
return _context.abrupt('return', ctx.body = {
|
|
|
|
|
errcode: 400,
|
|
|
|
|
errmsg: 'mock json数据格式有误',
|
|
|
|
|
data: interfaceData.res_body
|
|
|
|
|
});
|
2017-07-10 20:02:44 +08:00
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 63:
|
2017-08-01 15:47:06 +08:00
|
|
|
|
return _context.abrupt('return', ctx.body = interfaceData.res_body);
|
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 66:
|
|
|
|
|
_context.prev = 66;
|
|
|
|
|
_context.t3 = _context['catch'](30);
|
|
|
|
|
|
|
|
|
|
console.error(_context.t3);
|
|
|
|
|
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t3.message));
|
2017-08-01 15:47:06 +08:00
|
|
|
|
|
2017-08-16 17:13:14 +08:00
|
|
|
|
case 70:
|
2017-07-10 20:02:44 +08:00
|
|
|
|
case 'end':
|
2017-07-05 16:17:58 +08:00
|
|
|
|
return _context.stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-16 17:13:14 +08:00
|
|
|
|
}, _callee, undefined, [[17, 23], [30, 66], [55, 60]]);
|
2017-07-05 16:17:58 +08:00
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return function (_x, _x2) {
|
|
|
|
|
return _ref.apply(this, arguments);
|
|
|
|
|
};
|
|
|
|
|
}();
|