yapi/server_dist/middleware/mockServer.js

153 lines
5.8 KiB
JavaScript
Raw Normal View History

2017-07-10 20:02:44 +08:00
'use strict';
2017-07-10 20:02:44 +08:00
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
2017-07-10 20:02:44 +08:00
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
2017-07-10 20:02:44 +08:00
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
var _project2 = require('../models/project.js');
var _project3 = _interopRequireDefault(_project2);
var _interface = require('../models/interface.js');
var _interface2 = _interopRequireDefault(_interface);
var _mockjs = require('mockjs');
var _mockjs2 = _interopRequireDefault(_mockjs);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = function () {
var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee(ctx, next) {
2017-07-17 16:24:00 +08:00
var hostname, config, projectInst, projects, matchProject, maxBasepath, i, l, _project, project, interfaceData, interfaceInst;
2017-07-10 20:02:44 +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...');
hostname = ctx.hostname;
2017-07-10 20:02:44 +08:00
config = _yapi2.default.WEBCONFIG;
2017-07-11 12:12:43 +08:00
if (!(ctx.hostname === config.webhost)) {
2017-07-10 20:02:44 +08:00
_context.next = 8;
break;
}
if (!next) {
2017-07-10 20:02:44 +08:00
_context.next = 7;
break;
}
2017-07-10 20:02:44 +08:00
_context.next = 7;
return next();
2017-07-10 20:02:44 +08:00
case 7:
return _context.abrupt('return', true);
case 8:
2017-07-26 15:31:16 +08:00
_yapi2.default.commons.log('MockServer Running...');
2017-07-10 20:02:44 +08:00
projectInst = _yapi2.default.getInst(_project3.default), projects = void 0;
2017-07-26 15:31:16 +08:00
_context.prev = 10;
_context.next = 13;
2017-07-10 20:02:44 +08:00
return projectInst.getByDomain(hostname);
2017-07-26 15:31:16 +08:00
case 13:
2017-07-10 20:02:44 +08:00
projects = _context.sent;
2017-07-26 15:31:16 +08:00
_context.next = 19;
2017-07-10 20:02:44 +08:00
break;
2017-07-26 15:31:16 +08:00
case 16:
_context.prev = 16;
_context.t0 = _context['catch'](10);
2017-07-10 20:02:44 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 403, _context.t0.message));
2017-07-26 15:31:16 +08:00
case 19:
matchProject = [], maxBasepath = 0;
2017-07-10 20:02:44 +08:00
for (i = 0, l = projects.length; i < l; i++) {
_project = projects[i];
2017-07-26 15:31:16 +08:00
if (ctx.path && ctx.path.indexOf(_project.basepath) === 0) {
2017-07-10 20:02:44 +08:00
matchProject.push(_project);
2017-07-17 16:24:00 +08:00
if (_project.basepath.length > maxBasepath) {
maxBasepath = _project.basepath.length;
matchProject = _project;
}
2017-07-10 20:02:44 +08:00
}
}
2017-07-17 16:24:00 +08:00
if (!(matchProject === false)) {
2017-07-26 15:31:16 +08:00
_context.next = 23;
2017-07-10 20:02:44 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '不存在的domain'));
2017-07-26 15:31:16 +08:00
case 23:
2017-07-17 16:24:00 +08:00
project = matchProject, interfaceData = void 0;
2017-07-10 20:02:44 +08:00
interfaceInst = _yapi2.default.getInst(_interface2.default);
2017-07-26 15:31:16 +08:00
_context.prev = 25;
_context.next = 28;
2017-07-10 20:02:44 +08:00
return interfaceInst.getByPath(project._id, ctx.path.substr(project.basepath.length));
2017-07-26 15:31:16 +08:00
case 28:
2017-07-10 20:02:44 +08:00
interfaceData = _context.sent;
if (!(!interfaceData || interfaceData.length === 0)) {
2017-07-26 15:31:16 +08:00
_context.next = 31;
2017-07-10 20:02:44 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 404, '不存在的api'));
2017-07-26 15:31:16 +08:00
case 31:
2017-07-10 20:02:44 +08:00
if (!(interfaceData.length > 1)) {
2017-07-26 15:31:16 +08:00
_context.next = 33;
2017-07-10 20:02:44 +08:00
break;
}
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 405, '存在多个api请检查数据库'));
2017-07-26 15:31:16 +08:00
case 33:
2017-07-10 20:02:44 +08:00
interfaceData = interfaceData[0];
if (!(interfaceData.res_body_type === 'json')) {
2017-07-26 15:31:16 +08:00
_context.next = 36;
2017-07-10 20:02:44 +08:00
break;
}
return _context.abrupt('return', ctx.body = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body)));
2017-07-26 15:31:16 +08:00
case 36:
2017-07-10 20:02:44 +08:00
return _context.abrupt('return', ctx.body = interfaceData.res_body);
2017-07-26 15:31:16 +08:00
case 39:
_context.prev = 39;
_context.t1 = _context['catch'](25);
2017-07-10 20:02:44 +08:00
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t1.message));
2017-07-26 15:31:16 +08:00
case 42:
2017-07-10 20:02:44 +08:00
case 'end':
return _context.stop();
}
}
2017-07-26 15:31:16 +08:00
}, _callee, undefined, [[10, 16], [25, 39]]);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();