fix: 接口无法删除 pathParams bug 和 新增参数值管道处理机制,支持多个参数值

This commit is contained in:
suxiaoxin 2017-11-14 21:24:38 +08:00
parent 0d74b1f841
commit a249decd5a
5 changed files with 89 additions and 70 deletions

View File

@ -4,6 +4,7 @@ import constants from './constants/variable'
import Mock from 'mockjs'
import json5 from 'json5'
import MockExtra from 'common/mock-extra.js'
import {filter} from 'common/power-string.js'
const Roles = {
0 : 'admin',
@ -191,7 +192,7 @@ function simpleJsonPathParse(key, json){
}catch(e){
json = null;
json = '';
break;
}
}
@ -203,6 +204,61 @@ function handleMockWord(word) {
return Mock.mock(word);
}
/**
*
* @param {*} data
* @param {*} handleValueFn 处理参数值函数
*/
function handleJson(data, handleValueFn) {
if (!data) {
return data;
}
if (typeof data === 'string') {
return handleValueFn(data);
} else if (typeof data === 'object') {
for (let i in data) {
data[i] = handleJson(data[i]);
}
} else {
return data;
}
return data;
}
function handleValueWithFilter(context){
return function(match){
if (match[0] === '@') {
return handleMockWord(match);
} else if (match.indexOf('$.') === 0) {
return simpleJsonPathParse(match, context);
} else{
return match;
}
}
}
function handleParamsValue (val, context){
const variableRegexp = /\{\s*((?:\$|\@)?.+?)\}/g;
if (!val || typeof val !== 'string') {
return val;
}
val = val.trim();
if (val[0] !== '{' && val.indexOf('{') === -1) {
val = '{' + val + '}';
}
return val.replace(variableRegexp, function(str, match){
match = match.trim();
try{
return filter(match, handleValueWithFilter(context))
}catch(err){
return match;
}
})
}
exports.handleJson = handleJson;
exports.handleParamsValue = handleParamsValue;
exports.getMockText = (mockTpl) => {
return JSON.stringify(Mock.mock(MockExtra(json5.parse(mockTpl), {})), null, " ")
}

View File

@ -10,7 +10,7 @@ import URL from 'url';
const MockExtra = require('common/mock-extra.js')
import './Postman.scss';
import json5 from 'json5'
import { handleMockWord, isJson } from '../../common.js'
import { isJson, handleJson, handleParamsValue } from '../../common.js'
import _ from "underscore"
function json_parse(data) {
@ -32,13 +32,15 @@ function isJsonData(headers) {
return isResJson;
}
const wordList = constants.MOCK_SOURCE;
// const wordList = constants.MOCK_SOURCE;
const mockDataSource = wordList.map(item => {
return <AutoComplete.Option key={item.mock} value={item.mock}>
{item.mock}&nbsp; &nbsp;随机{item.name}
</AutoComplete.Option>
});
// const mockDataSource = wordList.map(item => {
// return <AutoComplete.Option key={item.mock} value={item.mock}>
// {item.mock}&nbsp; &nbsp;随机{item.name}
// </AutoComplete.Option>
// });
const mockDataSource = []
// const { TextArea } = Input;
@ -120,6 +122,10 @@ export default class Run extends Component {
}
}
handleValue = (val) => {
return handleParamsValue(val, {});
}
@autobind
getInterfaceState(nextProps) {
const props = nextProps || this.props;
@ -201,8 +207,9 @@ export default class Run extends Component {
let path = pathname;
pathParam.forEach(item => {
path = path.replace(`:${item.name}`, handleMockWord(item.value) || `:${item.name}`);
path = path.replace(`:${item.name}`, this.handleValue(item.value) || `:${item.name}`);
});
const urlObj = URL.parse(URL.resolve(_.find(domains, item => item.name === caseEnv).domain, '.' + path));
let pathQuery = {};
@ -227,7 +234,7 @@ export default class Run extends Component {
if(resBody === false){
resBody = bodyOther;
}else{
reqBody = this.handleJson(resBody)
reqBody = handleJson(resBody, this.handleValue)
}
}
@ -474,7 +481,7 @@ export default class Run extends Component {
arr.forEach(item => {
if (item)
if (item.name && item.type !== 'file' && item.enable) {
obj[item.name] = handleMockWord(item.value);
obj[item.name] = this.handleValue(item.value);
}
})
return obj;
@ -493,7 +500,9 @@ export default class Run extends Component {
const queryObj = {};
query.forEach(item => {
if (item.name && item.enable) {
queryObj[item.name] = handleMockWord(item.value);
console.log(item.value)
queryObj[item.name] = this.handleValue(item.value);
console.log(queryObj[item.name])
}
})
return queryObj;
@ -502,28 +511,12 @@ export default class Run extends Component {
const headersObj = {};
headers.forEach(item => {
if (item.name && item.value) {
headersObj[item.name] = handleMockWord(item.value);
headersObj[item.name] = this.handleValue(item.value);
}
})
return headersObj;
}
handleJson = (data)=>{
if(!data){
return data;
}
if(typeof data === 'string'){
return handleMockWord(data);
}else if(typeof data === 'object'){
for(let i in data){
data[i] = this.handleJson(data[i]);
}
}else{
return data;
}
return data;
}
bindAceEditor = () => {
mockEditor({
container: 'res-body-pretty',
@ -569,6 +562,7 @@ export default class Run extends Component {
});
}
render() {
const { method, domains, pathParam, pathname, query, headers, bodyForm, caseEnv, bodyType, resHeader, loading, validRes } = this.state;
HTTP_METHOD[method] = HTTP_METHOD[method] || {}
@ -576,7 +570,7 @@ export default class Run extends Component {
let isResJson = isJsonData(resHeader);
let path = pathname;
pathParam.forEach(item => {
let val = handleMockWord(item.value);
let val = this.handleValue(item.value);
path = path.replace(`:${item.name}`, val || `:${item.name}`);
});
const pathObj = URL.parse(path);
@ -690,6 +684,7 @@ export default class Run extends Component {
dataSource={mockDataSource}
placeholder="参数值"
optionLabelProp="value"
/>
<Icon style={{ display: 'none' }} type="delete" className="icon-btn" onClick={() => this.deletePathParam(index)} />
</div>

View File

@ -8,7 +8,7 @@ import { Tooltip, Icon, Button, Spin, Modal, message, Select, Switch } from 'ant
import { fetchInterfaceColList, fetchCaseList, setColData } from '../../../../reducer/modules/interfaceCol'
import HTML5Backend from 'react-dnd-html5-backend';
import { DragDropContext } from 'react-dnd';
import { isJson, handleMockWord, simpleJsonPathParse } from '../../../../common.js'
import { isJson, handleJson, handleParamsValue } from '../../../../common.js'
import mockEditor from '../InterfaceList/mockEditor';
import * as Table from 'reactabular-table';
import * as dnd from 'reactabular-dnd';
@ -193,7 +193,6 @@ class InterfaceColContent extends Component {
}
handleTest = async (interfaceData) => {
console.log(1)
const { currProject } = this.props;
let requestParams = {};
let { case_env } = interfaceData;
@ -243,7 +242,7 @@ class InterfaceColContent extends Component {
if (reqBody === false) {
result.body = this.handleValue(interfaceData.req_body_other)
} else {
reqBody = this.handleJson(reqBody)
reqBody = handleJson(reqBody, this.handleValue)
requestParams = Object.assign(requestParams, reqBody);
result.body = JSON.stringify(reqBody)
}
@ -346,42 +345,11 @@ class InterfaceColContent extends Component {
}
}
handleJson = (data) => {
if (!data) {
return data;
}
if (typeof data === 'string') {
return this.handleValue(data);
} else if (typeof data === 'object') {
for (let i in data) {
data[i] = this.handleJson(data[i]);
}
} else {
return data;
}
return data;
handleValue = (val) => {
return handleParamsValue(val, this.recoreds);
}
handleValue = (val) => {
const regex = /(\{\$\.\d+\..*?\})|(\{\@\w+\})/g;
if (!val || typeof val !== 'string') {
return val;
} else if (val[0] === '@') {
return handleMockWord(val);
} else if (val.indexOf('$.') === 0) {
return simpleJsonPathParse(val, this.records);
} else if (val.match(regex) !== null) {
val.match(regex).forEach((match) => {
if (match.indexOf("{@") === 0) {
val = val.replace(match, handleMockWord(match.substr(1, match.length - 2)))
} else {
val = val.replace(match, simpleJsonPathParse(match.substr(1, match.length - 2), this.records));
}
});
return val;
}
return val;
}
arrToObj = (arr, requestParams) => {
arr = arr || [];

View File

@ -49,7 +49,8 @@
"sha1": "^1.1.1",
"tslib": "^1.8.0",
"underscore": "^1.8.3",
"url": "^0.11.0"
"url": "^0.11.0",
"yapi-plugin-qsso": "^1.0.7"
},
"devDependencies": {
"assets-webpack-plugin": "^3.5.1",

View File

@ -450,7 +450,7 @@ class interfaceController extends baseController {
data.req_body_form = this.requiredSort(params.req_body_form);
// data.req_body_form = params.req_body_form;
}
if (!_.isUndefined(params.req_params) && Array.isArray(params.req_params) && params.req_params.length > 0) {
if (!_.isUndefined(params.req_params)) {
if(Array.isArray(params.req_params) && params.req_params.length > 0){
data.type = 'var'
data.req_params = params.req_params;
@ -458,7 +458,6 @@ class interfaceController extends baseController {
data.type = 'static'
data.req_params = [];
}
}
if (!_.isUndefined(params.req_query)) {