yapi/client/components/Postman/Postman.js

795 lines
26 KiB
JavaScript
Raw Normal View History

2017-08-22 09:43:33 +08:00
import React, { Component } from 'react'
import PropTypes from 'prop-types'
2017-08-31 15:18:37 +08:00
import Mock from 'mockjs'
import { Button, Input, Select, Alert, Spin, Icon, Collapse, Tooltip, message, AutoComplete, Switch } from 'antd'
2017-08-22 09:43:33 +08:00
import { autobind } from 'core-decorators';
2017-09-08 10:40:28 +08:00
import constants from '../../constants/variable.js'
2017-08-24 12:13:44 +08:00
import mockEditor from '../../containers/Project/Interface/InterfaceList/mockEditor'
2017-08-22 09:43:33 +08:00
import URL from 'url';
2017-08-31 18:04:45 +08:00
const MockExtra = require('common/mock-extra.js')
import './Postman.scss';
2017-09-07 20:39:58 +08:00
import json5 from 'json5'
2017-09-20 11:45:18 +08:00
import { handleMockWord } from '../../common.js'
2017-09-07 20:39:58 +08:00
function json_parse(data) {
try {
return json5.parse(data)
} catch (e) {
return data
2017-09-07 20:39:58 +08:00
}
}
2017-08-22 09:43:33 +08:00
2017-09-20 20:21:58 +08:00
function isJsonData(headers) {
2017-09-17 20:43:34 +08:00
if (!headers || typeof headers !== 'object') return false;
2017-09-08 15:21:41 +08:00
let isResJson = false;
Object.keys(headers).map(key => {
if (/content-type/i.test(key) && /application\/json/i.test(headers[key])) {
isResJson = true;
}
})
return isResJson;
}
2017-09-22 10:58:50 +08:00
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 { TextArea } = Input;
2017-08-22 09:43:33 +08:00
const InputGroup = Input.Group;
const Option = Select.Option;
const Panel = Collapse.Panel;
2017-09-08 10:40:28 +08:00
const HTTP_METHOD = constants.HTTP_METHOD;
2017-08-22 09:43:33 +08:00
export default class Run extends Component {
static propTypes = {
2017-08-23 11:41:46 +08:00
data: PropTypes.object,
2017-08-24 10:44:02 +08:00
save: PropTypes.func,
saveTip: PropTypes.string,
type: PropTypes.string
2017-08-22 09:43:33 +08:00
}
state = {
2017-08-31 16:00:41 +08:00
res: null,
resHeader: null,
2017-08-22 09:43:33 +08:00
method: 'GET',
domains: [],
pathname: '',
query: [],
bodyForm: [],
headers: [],
2017-08-29 10:08:55 +08:00
caseEnv: '',
2017-08-22 09:43:33 +08:00
bodyType: '',
2017-08-24 17:02:39 +08:00
bodyOther: '',
2017-08-31 15:18:37 +08:00
loading: false,
2017-09-20 11:45:18 +08:00
validRes: [],
hasPlugin: true,
test_status: null,
2017-09-29 15:55:41 +08:00
resMockTest: true,
resStatusCode: null,
resStatusText: ''
2017-08-22 09:43:33 +08:00
}
constructor(props) {
super(props)
}
componentWillMount() {
let startTime = 0;
2017-09-07 20:39:58 +08:00
this.interval = setInterval(() => {
startTime += 500;
2017-09-07 20:39:58 +08:00
if (startTime > 5000) {
clearInterval(this.interval);
}
2017-09-07 20:39:58 +08:00
if (window.crossRequest) {
clearInterval(this.interval);
this.setState({
hasPlugin: true
})
2017-09-07 20:39:58 +08:00
} else {
this.setState({
hasPlugin: false
})
}
}, 500)
2017-08-22 09:43:33 +08:00
this.getInterfaceState()
}
2017-09-07 20:39:58 +08:00
componentWillUnmount() {
clearInterval(this.interval)
}
2017-08-22 09:43:33 +08:00
componentWillReceiveProps(nextProps) {
2017-08-23 14:59:24 +08:00
if (nextProps.data._id !== this.props.data._id) {
this.getInterfaceState(nextProps)
}
2017-08-22 09:43:33 +08:00
}
2017-08-24 17:02:39 +08:00
componentDidMount() {
2017-08-24 17:15:58 +08:00
const { bodyType } = this.state;
2017-09-07 20:39:58 +08:00
if (bodyType && bodyType !== 'file' && bodyType !== 'form') {
2017-08-24 17:15:58 +08:00
this.loadBodyEditor()
}
2017-08-24 17:02:39 +08:00
}
2017-08-22 09:43:33 +08:00
@autobind
getInterfaceState(nextProps) {
const props = nextProps || this.props;
2017-08-24 10:44:02 +08:00
const { data, type } = props;
2017-08-22 09:43:33 +08:00
const {
method = '',
path: url = '',
req_headers = [],
req_body_type,
req_query = [],
req_params = [],
req_body_other = '',
req_body_form = [],
basepath = '',
2017-08-23 11:41:46 +08:00
env = [],
2017-09-20 11:45:18 +08:00
case_env = '',
test_status = '',
test_res_body = '',
test_report = [],
2017-09-29 15:55:41 +08:00
test_res_header = '',
mock_verify = true
2017-08-22 09:43:33 +08:00
} = data;
2017-09-20 11:45:18 +08:00
2017-08-24 10:44:02 +08:00
// case 任意编辑 pathname不管项目的 basepath
const pathname = (type === 'inter' ? (basepath + url) : url).replace(/\/+/g, '/');
2017-08-22 09:43:33 +08:00
2017-08-29 10:08:55 +08:00
// let hasContentType = false;
// req_headers.forEach(headerItem => {
// // TODO 'Content-Type' 排除大小写不同格式影响
// if (headerItem.name === 'Content-Type'){
// hasContentType = true;
// headerItem.value = headerItem.value || 'application/x-www-form-urlencoded';
// }
// })
// if (!hasContentType) {
// req_headers.push({name: 'Content-Type', value: 'application/x-www-form-urlencoded'});
// }
// const domains = env.concat();
// if (domain && !env.find(item => item.domain === domain)) {
// domains.push({name: 'default', domain})
// }
2017-08-22 09:43:33 +08:00
this.setState({
method,
2017-08-29 10:08:55 +08:00
domains: env.concat(),
2017-08-22 09:43:33 +08:00
pathParam: req_params.concat(),
pathname,
query: req_query.concat(),
bodyForm: req_body_form.concat(),
headers: req_headers.concat(),
bodyOther: req_body_other,
2017-08-29 10:08:55 +08:00
caseEnv: case_env || (env[0] && env[0].name),
2017-08-22 09:43:33 +08:00
bodyType: req_body_type || 'form',
2017-09-20 11:45:18 +08:00
loading: false,
test_status: test_status,
validRes: test_report,
res: test_res_body,
2017-09-29 15:55:41 +08:00
resHeader: test_res_header,
resMockTest: mock_verify
2017-08-24 17:15:58 +08:00
}, () => {
2017-09-07 20:39:58 +08:00
if (req_body_type && req_body_type !== 'file' && req_body_type !== 'form') {
2017-08-24 17:15:58 +08:00
this.loadBodyEditor()
}
2017-09-22 10:58:50 +08:00
if (test_res_body) {
2017-09-20 11:45:18 +08:00
this.bindAceEditor();
}
2017-09-22 10:58:50 +08:00
2017-08-22 09:43:33 +08:00
});
2017-09-22 10:58:50 +08:00
2017-08-22 09:43:33 +08:00
}
@autobind
reqRealInterface() {
2017-08-29 11:28:28 +08:00
if (this.state.loading) {
this.setState({ loading: false })
2017-09-07 20:39:58 +08:00
return;
2017-08-29 11:28:28 +08:00
}
2017-08-30 14:28:07 +08:00
const { headers, bodyForm, pathParam, bodyOther, caseEnv, domains, method, pathname, query, bodyType } = this.state;
2017-08-29 11:28:28 +08:00
const urlObj = URL.parse(domains.find(item => item.name === caseEnv).domain);
2017-08-30 14:28:07 +08:00
let path = pathname
pathParam.forEach(item => {
path = path.replace(`:${item.name}`, item.value || `:${item.name}`);
});
2017-10-10 16:42:07 +08:00
if(urlObj.pathname){
if(urlObj.pathname[urlObj.pathname.length - 1] !== '/'){
urlObj.pathname += '/'
}
}
2017-08-22 09:43:33 +08:00
const href = URL.format({
protocol: urlObj.protocol || 'http',
host: urlObj.host,
2017-10-10 16:42:07 +08:00
pathname: urlObj.pathname ? URL.resolve(urlObj.pathname, '.' + path) : path,
2017-08-22 09:43:33 +08:00
query: this.getQueryObj(query)
});
this.setState({ loading: true })
2017-09-07 20:39:58 +08:00
let that = this;
window.crossRequest({
2017-08-22 09:43:33 +08:00
url: href,
method,
headers: this.getHeadersObj(headers),
data: bodyType === 'form' ? this.arrToObj(bodyForm) : bodyOther,
files: bodyType === 'form' ? this.getFiles(bodyForm) : {},
2017-09-09 09:11:08 +08:00
file: bodyType === 'file' ? 'single-file' : null,
success: (res, header, third) => {
2017-09-29 15:55:41 +08:00
console.log('suc', third);
this.setState({
resStatusCode: third.res.status,
resStatusText: third.res.statusText
})
2017-08-24 12:13:44 +08:00
try {
2017-09-17 20:43:34 +08:00
if (isJsonData(header)) {
2017-09-08 15:21:41 +08:00
res = json_parse(res);
2017-08-31 16:00:41 +08:00
}
2017-09-07 21:15:53 +08:00
const { res_body, res_body_type } = that.props.data;
2017-09-20 11:45:18 +08:00
let validRes = [];
2017-09-07 21:15:53 +08:00
let query = {};
that.state.query.forEach(item => {
query[item.name] = item.value;
2017-08-31 18:04:45 +08:00
})
2017-09-07 21:15:53 +08:00
let body = {};
if (that.state.bodyType === 'form') {
that.state.bodyForm.forEach(item => {
body[item.name] = item.value;
})
} else if (that.state.bodyType === 'json') {
2017-09-08 15:21:41 +08:00
body = json_parse(that.state.bodyOther);
2017-08-31 18:04:45 +08:00
}
2017-09-07 21:15:53 +08:00
if (res_body && res_body_type === 'json' && typeof res === 'object') {
let tpl = MockExtra(json_parse(res_body), {
query: query,
body: body
})
validRes = Mock.valid(tpl, res)
2017-08-31 18:04:45 +08:00
}
2017-09-07 20:39:58 +08:00
2017-09-20 11:45:18 +08:00
if (Array.isArray(validRes) && validRes.length > 0) {
message.warn('请求完成, 返回数据跟接口定义不匹配');
validRes = validRes.map(item => {
return item.message
})
that.setState({ res, resHeader: header, validRes, test_status: 'invalid' })
} else if (Array.isArray(validRes) && validRes.length === 0) {
message.success('请求完成');
that.setState({ res, resHeader: header, validRes: ['验证通过'], test_status: 'ok' })
}
2017-09-07 21:15:53 +08:00
that.setState({ loading: false })
that.bindAceEditor()
} catch (e) {
console.error(e.message)
2017-08-31 18:04:45 +08:00
}
2017-08-22 09:43:33 +08:00
},
error: (err, header, third) => {
2017-09-29 15:55:41 +08:00
console.log('err', third);
this.setState({
resStatusCode: third.res.status,
resStatusText: third.res.statusText
})
2017-08-24 12:13:44 +08:00
try {
2017-09-22 17:35:12 +08:00
err = json_parse(err);
2017-08-24 12:13:44 +08:00
} catch (e) {
2017-09-22 17:35:12 +08:00
console.log(e)
2017-08-24 12:13:44 +08:00
}
2017-09-22 17:35:12 +08:00
message.error(err || '请求异常')
2017-09-20 11:45:18 +08:00
that.setState({ res: err || '请求失败', resHeader: header, validRes: [], test_status: 'error' })
2017-09-07 20:39:58 +08:00
that.setState({ loading: false })
that.bindAceEditor()
2017-08-22 09:43:33 +08:00
}
})
}
2017-08-29 11:28:28 +08:00
// @autobind
// changeDomain(value) {
// this.setState({ currDomain: value });
// }
2017-08-22 09:43:33 +08:00
@autobind
selectDomain(value) {
2017-08-29 10:08:55 +08:00
this.setState({ caseEnv: value });
2017-08-22 09:43:33 +08:00
}
@autobind
2017-09-22 10:58:50 +08:00
changeHeader(v, index, isName) {
2017-09-07 20:39:58 +08:00
const headers = json_parse(JSON.stringify(this.state.headers));
2017-08-22 09:43:33 +08:00
if (isName) {
headers[index].name = v;
} else {
headers[index].value = v;
}
this.setState({ headers });
}
@autobind
addHeader() {
const { headers } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ headers: headers.concat([{ name: '', value: '' }]) })
2017-08-22 09:43:33 +08:00
}
@autobind
deleteHeader(index) {
const { headers } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ headers: headers.filter((item, i) => +index !== +i) });
2017-08-22 09:43:33 +08:00
}
@autobind
2017-09-08 15:21:41 +08:00
setContentType() {
// const headersObj = this.getHeadersObj(this.state.headers);
// headersObj['Content-Type'] = type;
// this.setState({ headers: this.objToArr(headersObj) })
2017-08-22 09:43:33 +08:00
}
@autobind
2017-09-22 10:58:50 +08:00
changeQuery(v, index, isKey) {
2017-09-07 20:39:58 +08:00
const query = json_parse(JSON.stringify(this.state.query));
2017-08-22 09:43:33 +08:00
if (isKey) {
query[index].name = v;
} else {
query[index].value = v;
}
this.setState({ query });
}
@autobind
addQuery() {
const { query } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ query: query.concat([{ name: '', value: '' }]) })
2017-08-22 09:43:33 +08:00
}
@autobind
deleteQuery(index) {
const { query } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ query: query.filter((item, i) => +index !== +i) });
2017-08-22 09:43:33 +08:00
}
@autobind
2017-09-22 10:58:50 +08:00
changePathParam(v, index, isKey) {
2017-08-22 09:43:33 +08:00
const pathParam = JSON.parse(JSON.stringify(this.state.pathParam));
const name = pathParam[index].name;
let newPathname = this.state.pathname;
if (isKey) {
if (!name && v) {
newPathname += `/:${v}`;
} else {
newPathname = newPathname.replace(`/:${name}`, v ? `/:${v}` : '')
}
pathParam[index].name = v;
} else {
pathParam[index].value = v;
}
this.setState({ pathParam, pathname: newPathname });
}
@autobind
addPathParam() {
const { pathParam } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ pathParam: pathParam.concat([{ name: '', value: '' }]) })
2017-08-22 09:43:33 +08:00
}
@autobind
deletePathParam(index) {
const { pathParam } = this.state;
const name = pathParam[index].name;
const newPathname = this.state.pathname.replace(`/:${name}`, '');
2017-09-07 20:39:58 +08:00
this.setState({ pathParam: pathParam.filter((item, i) => +index !== +i), pathname: newPathname });
2017-08-22 09:43:33 +08:00
}
@autobind
2017-09-22 10:58:50 +08:00
changeBody(v, index) {
2017-09-07 20:39:58 +08:00
const bodyForm = json_parse(JSON.stringify(this.state.bodyForm));
2017-09-22 10:58:50 +08:00
if (bodyForm[index].type === 'file') {
bodyForm[index].value = 'file_' + index
} else {
bodyForm[index].value = v
2017-08-22 09:43:33 +08:00
}
this.setState({ bodyForm });
}
@autobind
addBody() {
const { bodyForm } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ bodyForm: bodyForm.concat([{ name: '', value: '', type: 'text' }]) })
2017-08-22 09:43:33 +08:00
}
@autobind
deleteBody(index) {
const { bodyForm } = this.state;
2017-09-07 20:39:58 +08:00
this.setState({ bodyForm: bodyForm.filter((item, i) => +index !== +i) });
2017-08-22 09:43:33 +08:00
}
@autobind
changeMethod(value) {
this.setState({ method: value });
}
@autobind
changePath(e) {
const path = e.target.value;
const urlObj = URL.parse(path, true);
this.setState({
query: this.objToArr(urlObj.query),
pathname: urlObj.pathname
})
}
@autobind
changeBodyType(value) {
2017-09-07 20:39:58 +08:00
this.setState({ bodyType: value }, () => {
if (value !== 'file' && value !== 'form') {
2017-08-24 17:02:39 +08:00
this.loadBodyEditor()
}
})
2017-08-22 09:43:33 +08:00
}
// hasCrossRequestPlugin() {
// const dom = document.getElementById('y-request');
// return dom.getAttribute('key') === 'yapi';
// }
2017-08-22 09:43:33 +08:00
objToArr(obj, key, value) {
const keyName = key || 'name';
const valueName = value || 'value';
const arr = []
Object.keys(obj).forEach((_key) => {
if (_key) {
2017-09-07 20:39:58 +08:00
arr.push({ [keyName]: _key, [valueName]: obj[_key] });
2017-08-22 09:43:33 +08:00
}
})
return arr;
}
arrToObj(arr) {
const obj = {};
arr.forEach(item => {
if (item.name && item.type !== 'file') {
2017-09-19 20:35:59 +08:00
obj[item.name] = handleMockWord(item.value);
2017-08-22 09:43:33 +08:00
}
})
return obj;
}
2017-09-19 20:35:59 +08:00
2017-08-22 09:43:33 +08:00
getFiles(bodyForm) {
const files = {};
bodyForm.forEach(item => {
if (item.name && item.type === 'file') {
files[item.name] = item.value
}
})
return files;
}
getQueryObj(query) {
const queryObj = {};
query.forEach(item => {
if (item.name) {
2017-09-19 20:35:59 +08:00
queryObj[item.name] = handleMockWord(item.value);
2017-08-22 09:43:33 +08:00
}
})
return queryObj;
}
getHeadersObj(headers) {
const headersObj = {};
headers.forEach(item => {
if (item.name && item.value) {
headersObj[item.name] = item.value;
}
})
return headersObj;
}
2017-08-24 12:13:44 +08:00
bindAceEditor = () => {
mockEditor({
container: 'res-body-pretty',
2017-09-08 15:21:41 +08:00
data: this.state.res,
2017-09-07 20:39:58 +08:00
readOnly: true,
onChange: function () { }
2017-08-24 12:13:44 +08:00
})
2017-09-29 15:55:41 +08:00
2017-08-24 12:13:44 +08:00
mockEditor({
container: 'res-headers-pretty',
2017-09-08 15:21:41 +08:00
data: this.state.resHeader,
2017-09-07 20:39:58 +08:00
readOnly: true,
onChange: function () { }
2017-08-24 12:13:44 +08:00
})
}
2017-08-24 17:02:39 +08:00
loadBodyEditor = () => {
const that = this;
2017-09-07 20:39:58 +08:00
setTimeout(function () {
2017-08-24 17:02:39 +08:00
mockEditor({
container: 'body-other-edit',
data: that.state.bodyOther,
onChange: function (d) {
if (d.format !== true) return false;
that.setState({
bodyOther: d.text
})
}
})
}, 0);
}
2017-08-24 12:13:44 +08:00
2017-08-22 09:43:33 +08:00
@autobind
fileChange(e, index) {
console.log(e)
console.log(index)
}
@autobind
onTestSwitched(checked) {
this.setState({
2017-09-29 15:55:41 +08:00
resMockTest: checked
});
}
2017-09-07 20:39:58 +08:00
render() {
2017-09-20 20:21:58 +08:00
const { method, domains, pathParam, pathname, query, headers, bodyForm, caseEnv, bodyType, resHeader, loading, validRes } = this.state;
2017-09-08 11:09:17 +08:00
HTTP_METHOD[method] = HTTP_METHOD[method] || {}
const hasPlugin = this.state.hasPlugin;
2017-09-20 20:21:58 +08:00
let isResJson = isJsonData(resHeader);
2017-08-22 09:43:33 +08:00
let path = pathname;
pathParam.forEach(item => {
path = path.replace(`:${item.name}`, item.value || `:${item.name}`);
});
2017-09-07 20:39:58 +08:00
const search = decodeURIComponent(URL.format({ query: this.getQueryObj(query) }));
let validResView;
2017-09-20 11:45:18 +08:00
validResView = validRes.map((item, index) => {
return <div key={index}>{item}</div>
})
2017-09-07 20:39:58 +08:00
2017-08-22 09:43:33 +08:00
return (
2017-08-24 12:13:44 +08:00
<div className="interface-test postman">
2017-09-29 15:55:41 +08:00
<div className={hasPlugin ? null : 'has-plugin'} >
2017-09-07 20:39:58 +08:00
{hasPlugin ? '' : <Alert
message={
<div>
{/* 温馨提示:当前正在使用接口测试服务,请安装我们为您免费提供的测试增强插件&nbsp;(该插件可支持任何 chrome 内核的浏览器) */}
重要当前的接口测试服务需安装免费测试增强插件 支持所有 webkit 内核选择下面任意一种安装方式
2017-08-22 09:43:33 +08:00
<div>
2017-09-07 20:39:58 +08:00
<a
target="blank"
href="https://chrome.google.com/webstore/detail/cross-request/cmnlfmgbjmaciiopcgodlhpiklaghbok?hl=en-US"
> [Google 商店获取需翻墙]</a>
2017-08-22 09:43:33 +08:00
</div>
2017-09-07 20:39:58 +08:00
<div>
<a
target="blank"
2017-09-29 14:58:07 +08:00
href="/api/interface/download_crx"
2017-09-07 20:39:58 +08:00
> [手动下载] </a>
<span> zip 文件解压后将 crx 文件拖入到 chrome://extensions/ </span>
<a
target="blank"
href="http://www.jianshu.com/p/12ca04c61fc6"
> [详细安装教程] </a>
</div>
</div>
}
type="warning"
/>
2017-08-22 09:43:33 +08:00
}
</div>
2017-09-17 20:43:34 +08:00
2017-09-26 10:40:44 +08:00
<h2 className="interface-title" style={{ marginTop: 0 }}>请求部分&nbsp;
<Tooltip placement="top" title="在 '设置->环境配置' 配置 domain"><Icon type="question-circle-o" /></Tooltip>
</h2>
<div className="url">
2017-09-17 20:43:34 +08:00
2017-09-26 10:40:44 +08:00
<InputGroup compact style={{ display: 'flex' }}>
<Select disabled value={method} style={{ flexBasis: 60 }} onChange={this.changeMethod} >
<Option value="GET">GET</Option>
<Option value="POST">POST</Option>
</Select>
2017-09-17 20:43:34 +08:00
2017-09-26 10:40:44 +08:00
<Select value={caseEnv} style={{ flexBasis: 180, flexGrow: 1 }} onSelect={this.selectDomain}>
2017-08-22 09:43:33 +08:00
{
2017-09-26 10:40:44 +08:00
domains.map((item, index) => (<Option value={item.name} key={index}>{item.name + '' + item.domain}</Option>))
2017-08-22 09:43:33 +08:00
}
2017-09-26 10:40:44 +08:00
</Select>
<Input disabled value={path + search} onChange={this.changePath} spellCheck="false" style={{ flexBasis: 180, flexGrow: 1 }} />
</InputGroup>
<Tooltip placement="bottom" title={(() => {
if (hasPlugin) {
return '发送请求'
} else {
return '请安装cross-request插件'
}
})()}>
<Button
disabled={!hasPlugin}
onClick={this.reqRealInterface}
type="primary"
style={{ marginLeft: 10 }}
icon={loading ? 'loading' : ''}
>{loading ? '取消' : '发送'}</Button>
</Tooltip>
<Tooltip placement="bottom" title={this.props.saveTip}>
<Button
onClick={this.props.save}
type="primary"
style={{ marginLeft: 10 }}
>{this.props.type === 'inter' ? '保存' : '保存'}</Button>
</Tooltip>
</div>
2017-09-08 10:40:28 +08:00
2017-09-26 10:40:44 +08:00
<Collapse defaultActiveKey={['0', '1', '2', '3']} bordered={true}>
<Panel header="PATH PARAMETERS" key="0" className={pathParam.length === 0 ? 'hidden' : ''}>
{
pathParam.map((item, index) => {
return (
<div key={index} className="key-value-wrap">
<Input disabled value={item.name} onChange={e => this.changePathParam(e, index, true)} className="key" />
<span className="eq-symbol">=</span>
<AutoComplete
value={item.value}
onChange={e => this.changePathParam(e, index)}
className="value"
dataSource={mockDataSource}
placeholder="参数值"
optionLabelProp="value"
/>
<Icon style={{ display: 'none' }} type="delete" className="icon-btn" onClick={() => this.deletePathParam(index)} />
</div>
)
})
}
<Button style={{ display: 'none' }} type="primary" icon="plus" onClick={this.addPathParam}>添加Path参数</Button>
</Panel>
<Panel header="QUERY PARAMETERS" key="1" className={query.length === 0 ? 'hidden' : ''}>
{
query.map((item, index) => {
return (
<div key={index} className="key-value-wrap">
<Input disabled value={item.name} onChange={e => this.changeQuery(e, index, true)} className="key" />
<span className="eq-symbol">=</span>
<AutoComplete
value={item.value}
onChange={e => this.changeQuery(e, index)}
className="value"
dataSource={mockDataSource}
placeholder="参数值"
optionLabelProp="value"
/>
<Icon style={{ display: 'none' }} type="delete" className="icon-btn" onClick={() => this.deleteQuery(index)} />
</div>
)
})
}
<Button style={{ display: 'none' }} type="primary" icon="plus" onClick={this.addQuery}>添加Query参数</Button>
</Panel>
<Panel header="HEADERS" key="2" className={headers.length === 0 ? 'hidden' : ''}>
{
headers.map((item, index) => {
return (
<div key={index} className="key-value-wrap">
<Input disabled value={item.name} onChange={e => this.changeHeader(e, index, true)} className="key" />
<span className="eq-symbol">=</span>
<AutoComplete
value={item.value}
onChange={e => this.changeHeader(e, index)}
className="value"
dataSource={mockDataSource}
placeholder="参数值"
optionLabelProp="value"
/>
<Icon style={{ display: 'none' }} type="delete" className="icon-btn" onClick={() => this.deleteHeader(index)} />
</div>
)
})
}
<Button style={{ display: 'none' }} type="primary" icon="plus" onClick={this.addHeader}>添加Header</Button>
</Panel>
<Panel
header={
<div style={{ display: 'flex', justifyContent: 'space-between' }}>
<div>BODY</div>
2017-09-08 10:40:28 +08:00
</div>
2017-09-26 10:40:44 +08:00
}
key="3"
className={HTTP_METHOD[method].request_body ? 'POST' : 'hidden'}
>
2017-09-08 10:40:28 +08:00
2017-09-26 10:40:44 +08:00
<div style={{ display: HTTP_METHOD[method].request_body && bodyType !== 'form' && bodyType !== 'file' ? 'block' : 'none' }}>
<div id="body-other-edit" style={{ marginTop: 10, minHeight: 150 }} className="pretty-editor"></div>
</div>
{
HTTP_METHOD[method].request_body && bodyType === 'form' &&
<div>
{
bodyForm.map((item, index) => {
return (
<div key={index} className="key-value-wrap">
<Input disabled value={item.name} onChange={e => this.changeBody(e, index, 'key')} className="key" />
<span>[</span>
<Select disabled value={item.type} onChange={e => this.changeBody(e, index, 'type')}>
<Option value="file">File</Option>
<Option value="text">Text</Option>
</Select>
<span>]</span>
<span className="eq-symbol">=</span>
{item.type === 'file' ?
<Input type="file" id={'file_' + index} onChange={e => this.changeBody(e, index, 'value')} multiple className="value" /> :
<AutoComplete
value={item.value}
onChange={e => this.changeBody(e, index, 'value')}
className="value"
dataSource={mockDataSource}
placeholder="参数值"
optionLabelProp="value"
/>
}
<Icon style={{ display: 'none' }} type="delete" className="icon-btn" onClick={() => this.deleteBody(index)} />
</div>
)
})
}
<Button style={{ display: 'none' }} type="primary" icon="plus" onClick={this.addBody}>添加Form参数</Button>
</div>
}
{
HTTP_METHOD[method].request_body && bodyType === 'file' &&
<div>
<Input type="file" id="single-file"></Input>
</div>
}
{/*
method !== 'POST' &&
<div>GET 请求没有 BODY</div>
*/}
</Panel>
</Collapse>
<h2 className="interface-title">返回结果</h2>
2017-09-29 15:55:41 +08:00
<Spin spinning={this.state.loading}>
<h2 style={{ display: this.state.resStatusCode !== null ? '' : 'none' }} className={'res-code ' + ((this.state.resStatusCode >= 200 && this.state.resStatusCode < 400 && !this.state.loading) ? 'success' : 'fail')}>{this.state.resStatusCode + ' ' + this.state.resStatusText}</h2>
<div style={{ display: this.state.res ? '' : 'none' }} className="container-header-body">
<div className="header">
<div className="container-title">
<h4>Headers</h4>
</div>
2017-09-29 15:55:41 +08:00
<div id="res-headers-pretty" className="pretty-editor-header"></div>
</div>
<div className="resizer">
<div className="container-title">
<h4 style={{ visibility: 'hidden' }}>1</h4>
</div>
2017-09-29 15:55:41 +08:00
</div>
<div className="body">
<div className="container-title">
<h4>Body</h4>
</div>
2017-09-29 15:55:41 +08:00
<div id="res-body-pretty" className="pretty-editor-body" style={{ display: isResJson ? '' : 'none' }}></div>
<div
style={{ display: isResJson ? 'none' : '' }}
className="res-body-text"
>{this.state.res && this.state.res.toString()}</div>
</div>
2017-09-29 15:55:41 +08:00
</div>
</Spin>
2017-09-29 15:55:41 +08:00
<p style={{ display: this.state.resStatusCode===null ? '' : 'none' }}>发送请求后在这里查看返回结果</p>
<h2 className="interface-title">数据结构验证
2017-09-29 15:55:41 +08:00
<Switch style={{ verticalAlign: 'text-bottom', marginLeft: '8px' }} checked={this.state.resMockTest} onChange={this.onTestSwitched} />
</h2>
2017-09-29 15:55:41 +08:00
<div className={(isResJson && this.state.resMockTest) ? '' : 'none'}>
{(isResJson && this.state.resMockTest) ? validResView : <div><p>若开启此功能则发送请求后在这里查看验证结果</p><p>YApi Response body </p></div>}
</div>
2017-08-22 09:43:33 +08:00
</div>
)
}
}