mirror of
https://github.com/YMFE/yapi.git
synced 2024-12-21 05:19:42 +08:00
Merge branch 'dev' of gitlab.corp.qunar.com:mfe/yapi into dev
This commit is contained in:
commit
ef37d0daae
@ -6,7 +6,7 @@ import { handlePath } from '../../../../common.js'
|
||||
|
||||
import {
|
||||
Form, Select, Input, Tooltip,
|
||||
Button, Row, Col, Radio, Icon
|
||||
Button, Row, Col, Radio, Icon, AutoComplete
|
||||
} from 'antd';
|
||||
|
||||
const FormItem = Form.Item;
|
||||
@ -23,6 +23,7 @@ const dataTpl = {
|
||||
const mockEditor = require('./mockEditor.js');
|
||||
const HTTP_METHOD = constants.HTTP_METHOD;
|
||||
const HTTP_METHOD_KEYS = Object.keys(HTTP_METHOD);
|
||||
const HTTP_REQUEST_HEADER = constants.HTTP_REQUEST_HEADER;
|
||||
|
||||
class InterfaceEditForm extends Component {
|
||||
static propTypes = {
|
||||
@ -85,8 +86,8 @@ class InterfaceEditForm extends Component {
|
||||
handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
this.props.form.validateFields((err, values) => {
|
||||
if (!err) {
|
||||
if (values.res_body_type === 'json') values.res_body = this.state.res_body;
|
||||
if (!err) {
|
||||
if (values.res_body_type === 'json') values.res_body = this.state.res_body;
|
||||
values.method = this.state.method;
|
||||
let isfile = false, isHavaContentType = false;
|
||||
if (values.req_body_type === 'form') {
|
||||
@ -206,6 +207,10 @@ class InterfaceEditForm extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
onSelect = (name) => {
|
||||
console.log(name);
|
||||
}
|
||||
|
||||
render() {
|
||||
const { getFieldDecorator } = this.props.form;
|
||||
const formItemLayout = {
|
||||
@ -248,11 +253,15 @@ class InterfaceEditForm extends Component {
|
||||
|
||||
const headerTpl = (data, index) => {
|
||||
return <Row key={index} className="interface-edit-item-content">
|
||||
<Col span="4">
|
||||
<Col span="6">
|
||||
{getFieldDecorator('req_headers[' + index + '].name', {
|
||||
initialValue: data.name
|
||||
})(
|
||||
<Input placeholder="参数名称" />
|
||||
<AutoComplete
|
||||
dataSource={HTTP_REQUEST_HEADER}
|
||||
filterOption={(inputValue, option) => option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1}
|
||||
placeholder="参数名称"
|
||||
/>
|
||||
)}
|
||||
</Col>
|
||||
<Col span="6" >
|
||||
@ -612,4 +621,4 @@ class InterfaceEditForm extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
export default Form.create()(InterfaceEditForm);
|
||||
export default Form.create()(InterfaceEditForm);
|
||||
|
70
common/mock-extra.js
Normal file
70
common/mock-extra.js
Normal file
@ -0,0 +1,70 @@
|
||||
var mockjs = require('mockjs');
|
||||
var strRegex = /\${([a-zA-Z0-9_\.]+)\}/g
|
||||
var varSplit = ".";
|
||||
var mockSplit = "|";
|
||||
|
||||
function mock(mockJSON, context) {
|
||||
context = context || {};
|
||||
var filtersMap = {
|
||||
regexp: handleRegexp
|
||||
}
|
||||
return mockjs.mock(parse(mockJSON))
|
||||
|
||||
function parse(p, c) {
|
||||
c = c || {};
|
||||
for (var i in p) {
|
||||
if (!p.hasOwnProperty(i)) {
|
||||
continue;
|
||||
}
|
||||
if (typeof p[i] === 'object') {
|
||||
c[i] = (p[i].constructor === Array) ? [] : {};
|
||||
parse(p[i], c[i]);
|
||||
} else {
|
||||
p[i] = handleStr(p[i]);
|
||||
var filters = i.split(mockSplit), newFilters = [].concat(filters);
|
||||
if (filters.length > 1) {
|
||||
for (var f = 1, l = filters.length, index; f < l; f++) {
|
||||
if (filters[f] in filtersMap) {
|
||||
if ((index = newFilters.indexOf(filters[f])) !== -1) {
|
||||
newFilters.splice(index, 1);
|
||||
}
|
||||
c[newFilters.join(mockSplit)] = filtersMap[filters[f]].call(p, p[i])
|
||||
}
|
||||
}
|
||||
} else {
|
||||
c[i] = p[i]
|
||||
}
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
function handleRegexp(item) {
|
||||
return new RegExp(item)
|
||||
}
|
||||
|
||||
function handleStr(str) {
|
||||
if(typeof str !== 'string' ||str.indexOf("{") === -1 || str.indexOf("}") === -1 || str.indexOf("$") === -1){
|
||||
return str;
|
||||
}
|
||||
str = str.replace(strRegex, function (matchs, name) {
|
||||
var names = name.split(varSplit);
|
||||
var data = context;
|
||||
names.forEach(function(n){
|
||||
if(data === false) return false;
|
||||
if(n in data){
|
||||
data = data[n];
|
||||
}else{
|
||||
data = false;
|
||||
}
|
||||
})
|
||||
if(data === false){
|
||||
return matchs;
|
||||
}
|
||||
return data;
|
||||
})
|
||||
return str;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = mock;
|
@ -295,16 +295,18 @@ class interfaceColController extends baseController{
|
||||
}
|
||||
result = result.toObject();
|
||||
let data = await this.interfaceModel.get(result.interface_id);
|
||||
data = data.toObject();
|
||||
let projectData = await this.projectModel.getBaseInfo(data.project_id);
|
||||
result.path = projectData.basepath + data.path;
|
||||
result.method = data.method;
|
||||
result.req_body_type = data.req_body_type;
|
||||
result.req_headers = data.req_headers;
|
||||
|
||||
|
||||
result.req_body_form = this.handleParamsValue(data.req_body_form, result.req_body_form)
|
||||
result.req_query = this.handleParamsValue(data.req_query, result.req_query)
|
||||
result.req_params = this.handleParamsValue(data.req_params, result.req_params)
|
||||
|
||||
|
||||
ctx.body = yapi.commons.resReturn(result);
|
||||
}catch(e){
|
||||
ctx.body = yapi.commons.resReturn(null, 400, e.message)
|
||||
|
@ -1,7 +1,7 @@
|
||||
import yapi from '../yapi.js';
|
||||
import projectModel from '../models/project.js';
|
||||
import interfaceModel from '../models/interface.js';
|
||||
import Mock from 'mockjs';
|
||||
import mockExtra from '../../common/mock-extra.js';
|
||||
import _ from 'underscore';
|
||||
|
||||
|
||||
@ -90,11 +90,16 @@ module.exports = async (ctx, next) => {
|
||||
ctx.set("Access-Control-Allow-Origin", "*")
|
||||
if (interfaceData.res_body_type === 'json') {
|
||||
try {
|
||||
const res = Mock.mock(
|
||||
yapi.commons.json_parse(interfaceData.res_body)
|
||||
const res = mockExtra(
|
||||
yapi.commons.json_parse(interfaceData.res_body),
|
||||
{
|
||||
query: ctx.request.query,
|
||||
body: ctx.request.body
|
||||
}
|
||||
);
|
||||
return ctx.body = res;
|
||||
} catch (e) {
|
||||
yapi.commons.log(e, 'error')
|
||||
return ctx.body = {
|
||||
errcode: 400,
|
||||
errmsg: 'mock json数据格式有误',
|
||||
|
@ -637,10 +637,12 @@ var interfaceColController = function (_baseController) {
|
||||
|
||||
case 10:
|
||||
data = _context6.sent;
|
||||
_context6.next = 13;
|
||||
|
||||
data = data.toObject();
|
||||
_context6.next = 14;
|
||||
return this.projectModel.getBaseInfo(data.project_id);
|
||||
|
||||
case 13:
|
||||
case 14:
|
||||
projectData = _context6.sent;
|
||||
|
||||
result.path = projectData.basepath + data.path;
|
||||
@ -653,21 +655,21 @@ var interfaceColController = function (_baseController) {
|
||||
result.req_params = this.handleParamsValue(data.req_params, result.req_params);
|
||||
|
||||
ctx.body = _yapi2.default.commons.resReturn(result);
|
||||
_context6.next = 27;
|
||||
_context6.next = 28;
|
||||
break;
|
||||
|
||||
case 24:
|
||||
_context6.prev = 24;
|
||||
case 25:
|
||||
_context6.prev = 25;
|
||||
_context6.t0 = _context6['catch'](0);
|
||||
|
||||
ctx.body = _yapi2.default.commons.resReturn(null, 400, _context6.t0.message);
|
||||
|
||||
case 27:
|
||||
case 28:
|
||||
case 'end':
|
||||
return _context6.stop();
|
||||
}
|
||||
}
|
||||
}, _callee6, this, [[0, 24]]);
|
||||
}, _callee6, this, [[0, 25]]);
|
||||
}));
|
||||
|
||||
function getCase(_x6) {
|
||||
|
@ -20,9 +20,9 @@ var _interface = require('../models/interface.js');
|
||||
|
||||
var _interface2 = _interopRequireDefault(_interface);
|
||||
|
||||
var _mockjs = require('mockjs');
|
||||
var _mockExtra = require('../../common/mock-extra.js');
|
||||
|
||||
var _mockjs2 = _interopRequireDefault(_mockjs);
|
||||
var _mockExtra2 = _interopRequireDefault(_mockExtra);
|
||||
|
||||
var _underscore = require('underscore');
|
||||
|
||||
@ -181,39 +181,44 @@ module.exports = function () {
|
||||
ctx.set("Access-Control-Allow-Origin", "*");
|
||||
|
||||
if (!(interfaceData.res_body_type === 'json')) {
|
||||
_context.next = 63;
|
||||
_context.next = 64;
|
||||
break;
|
||||
}
|
||||
|
||||
_context.prev = 55;
|
||||
res = _mockjs2.default.mock(_yapi2.default.commons.json_parse(interfaceData.res_body));
|
||||
res = (0, _mockExtra2.default)(_yapi2.default.commons.json_parse(interfaceData.res_body), {
|
||||
query: ctx.request.query,
|
||||
body: ctx.request.body
|
||||
});
|
||||
return _context.abrupt('return', ctx.body = res);
|
||||
|
||||
case 60:
|
||||
_context.prev = 60;
|
||||
_context.t2 = _context['catch'](55);
|
||||
|
||||
_yapi2.default.commons.log(_context.t2, 'error');
|
||||
return _context.abrupt('return', ctx.body = {
|
||||
errcode: 400,
|
||||
errmsg: 'mock json数据格式有误',
|
||||
data: interfaceData.res_body
|
||||
});
|
||||
|
||||
case 63:
|
||||
case 64:
|
||||
return _context.abrupt('return', ctx.body = interfaceData.res_body);
|
||||
|
||||
case 66:
|
||||
_context.prev = 66;
|
||||
case 67:
|
||||
_context.prev = 67;
|
||||
_context.t3 = _context['catch'](30);
|
||||
|
||||
console.error(_context.t3);
|
||||
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 409, _context.t3.message));
|
||||
|
||||
case 70:
|
||||
case 71:
|
||||
case 'end':
|
||||
return _context.stop();
|
||||
}
|
||||
}
|
||||
}, _callee, undefined, [[17, 23], [30, 66], [55, 60]]);
|
||||
}, _callee, undefined, [[17, 23], [30, 67], [55, 60]]);
|
||||
}));
|
||||
|
||||
return function (_x, _x2) {
|
||||
|
Loading…
Reference in New Issue
Block a user