From 132653673b1987fb1459a3d617cb115cdee7cce9 Mon Sep 17 00:00:00 2001 From: suxiaoxin Date: Fri, 15 Sep 2017 16:58:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0har=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/constants/variable.js | 2 +- .../Setting/ProjectData/ProjectData.js | 2 +- client/plugin.js | 2 +- exts/yapi-plugin-import-har/client.js | 210 ++++++++++++++++++ exts/yapi-plugin-import-postman/client.js | 185 +++++++++++++++ 5 files changed, 398 insertions(+), 3 deletions(-) create mode 100644 exts/yapi-plugin-import-har/client.js create mode 100644 exts/yapi-plugin-import-postman/client.js diff --git a/client/constants/variable.js b/client/constants/variable.js index 9209412d..08552184 100755 --- a/client/constants/variable.js +++ b/client/constants/variable.js @@ -1,6 +1,6 @@ export default { PAGE_LIMIT: 10, // 默认每页展示10条数据 - NAME_LIMIT: 20, // 限制名称的字符长度(中文算两个长度) + NAME_LIMIT: 30, // 限制名称的字符长度(中文算两个长度) HTTP_METHOD: { 'GET': { request_body: false diff --git a/client/containers/Project/Setting/ProjectData/ProjectData.js b/client/containers/Project/Setting/ProjectData/ProjectData.js index dc0fd245..21f7705d 100755 --- a/client/containers/Project/Setting/ProjectData/ProjectData.js +++ b/client/containers/Project/Setting/ProjectData/ProjectData.js @@ -9,7 +9,7 @@ const Option = Select.Option; const plugin = require('client/plugin.js'); -let importDataModule = {}; +const importDataModule = {}; @connect( state => { diff --git a/client/plugin.js b/client/plugin.js index a91a029e..13e50982 100755 --- a/client/plugin.js +++ b/client/plugin.js @@ -32,7 +32,7 @@ pluginModule = { emitHook: emitHook } -systemPlugins = ['import-postman'] +systemPlugins = ['import-postman', 'import-har'] config.plugins = config.plugins && Array.isArray(config.plugins) ? config.plugins: []; diff --git a/exts/yapi-plugin-import-har/client.js b/exts/yapi-plugin-import-har/client.js new file mode 100644 index 00000000..41f7115e --- /dev/null +++ b/exts/yapi-plugin-import-har/client.js @@ -0,0 +1,210 @@ + +import {message} from 'antd' + +function json_format(json){ + try{ + return JSON.stringify(JSON.parse(json), null, ' '); + }catch(e){ + return json; + } +} + +function postman(importDataModule){ + + function parseUrl(url){ + let parser = document.createElement('a'); + parser.href = url; + return { + protocol: parser.protocol, + hostname: parser.hostname, + port: parser.port, + pathname: parser.pathname, + search: parser.search, + host: parser.host + } + } + + function checkInterRepeat(interData){ + let obj = {}; + let arr = []; + for(let item in interData){ + // console.log(interData[item].url + "-" + interData[item].method); + let key = interData[item].request.url + "|" + interData[item].request.method; + if(!obj[key]){ + arr.push(interData[item]); + obj[key] = true; + } + } + return arr; + } + + function handleReq_query(query){ + let res = []; + if(query&&query.length){ + for(let item in query){ + res.push({ + name: query[item].key, + desc: query[item].description, + required: query[item].enable + }); + } + } + return res; + } + // function handleReq_headers(headers){ + // let res = []; + // if(headers&&headers.length){ + // for(let item in headers){ + // res.push({ + // name: headers[item].key, + // desc: headers[item].description, + // value: headers[item].value, + // required: headers[item].enable + // }); + // } + // } + // return res; + // } + + function handleReq_body_form(body_form){ + let res = []; + if(body_form && typeof body_form === 'object'){ + for(let item in body_form){ + res.push({ + name: body_form[item].name, + type: 'text' + }); + } + } + return res; + } + + function handlePath(path){ + path = path.replace(/{{(\w*)}}/,""); + path = parseUrl(path).pathname; + if(path.charAt(0) != "/"){ + path = "/" + path; + } + if(path.charAt(path.length-1) === "/"){ + path = path.substr(0,path.length-1); + } + return path; + } + + function run(res){ + try{ + res = JSON.parse(res); + res = res.log.entries; + res = res.filter(item=>{ + if(!item)return false; + return item.response.content.mimeType.indexOf('application/json') === 0; + }) + + + let interfaceData = {apis: []}; + res = checkInterRepeat.bind(this)(res); + if(res && res.length){ + for(let item in res){ + let data = importHar.bind(this)(res[item]); + interfaceData.apis.push(data); + } + } + return interfaceData; + + }catch(e){ + console.error(e); + message.error("数据格式有误"); + } + + } + + function importHar(data,key){ + let reflect = {//数据字段映射关系 + title: "url", + path: "url", + method: "method", + desc: "description", + req_query: "queryString", + req_body_form: "params", + req_body_other: "text" + }; + let allKey = ["title","path","method","req_query","req_body_type","req_body_form","req_body_other", "res_body_type", "res_body","req_headers"]; + key = key || allKey; + let res = {}; + + let reqType = 'json', header; + data.request.headers.forEach(item=>{ + if(!item ||item.name ||item.value) return null; + if(/content-type/i.test(item.name) && item.value.index('application/json') === 0){ + reqType = 'json'; + header = 'application/json'; + }else if(/content-type/i.test(item.name) && item.value.index('application/x-www-form-urlencoded') === 0){ + header = 'application/x-www-form-urlencoded' + reqType = 'form'; + }else if(/content-type/i.test(item.name) && item.value.index('multipart/form-data') === 0){ + header = 'multipart/form-data' + reqType = 'form'; + } + }) + + for(let item in key){ + item = key[item]; + if(item === "req_query"){ + res[item] = handleReq_query.bind(this)(data.request[reflect[item]]); + }else if(item === "req_body_form" && reqType === 'form' && data.request.postData){ + if(header === 'application/x-www-form-urlencoded'){ + res[item] = handleReq_body_form.bind(this)(data.request.postData[reflect[item]]); + }else if(header === 'multipart/form-data' ){ + res[item] = []; + } + + }else if(item === "req_headers"){ + res[item] = [{ + name: 'Content-Type', + value: header + }]; + }else if(item === "req_body_type"){ + res[item] = reqType; + }else if(item === "path"){ + res[item] = handlePath.bind(this)(data.request[reflect[item]]); + }else if(item === "title"){ + let path = handlePath.bind(this)(data.request[reflect["path"]]); + if(data.request[reflect[item]].indexOf(path) > -1){ + res[item] = path; + if(res[item] && res[item].indexOf("/:") > -1){ + res[item] = res[item].substr(0,res[item].indexOf("/:")); + } + }else{ + res[item] = data.request[reflect[item]]; + } + }else if(item === 'res_body_type'){ + res[item] = 'json'; + }else if(item === 'res_body'){ + res[item] = json_format(data.response.content.text); + } + else{ + res[item] = data.request[reflect[item]]; + } + } + return res; + } + + if(!importDataModule || typeof importDataModule !== 'object'){ + console.error('obj参数必需是一个对象'); + return null; + } + + importDataModule.har = { + name: 'HAR', + run: run, + desc: '使用chrome录制请求功能,具体使用请查看文档' + } +} + + + +module.exports = function(){ + + + this.bindHook('import_data', postman) +} \ No newline at end of file diff --git a/exts/yapi-plugin-import-postman/client.js b/exts/yapi-plugin-import-postman/client.js new file mode 100644 index 00000000..07dc1148 --- /dev/null +++ b/exts/yapi-plugin-import-postman/client.js @@ -0,0 +1,185 @@ + +import {message} from 'antd' + +function postman(importDataModule){ + + function parseUrl(url){ + let parser = document.createElement('a'); + parser.href = url; + return { + protocol: parser.protocol, + hostname: parser.hostname, + port: parser.port, + pathname: parser.pathname, + search: parser.search, + host: parser.host + } + } + + function checkInterRepeat(interData){ + let obj = {}; + let arr = []; + for(let item in interData){ + // console.log(interData[item].url + "-" + interData[item].method); + if(!obj[interData[item].url + "-" + interData[item].method+ "-"+interData[item].method]){ + arr.push(interData[item]); + obj[interData[item].url + "-" + interData[item].method+ "-"+interData[item].method] = true; + } + } + return arr; + } + + function handleReq_query(query){ + let res = []; + if(query&&query.length){ + for(let item in query){ + res.push({ + name: query[item].key, + desc: query[item].description, + required: query[item].enable + }); + } + } + return res; + } + function handleReq_headers(headers){ + let res = []; + if(headers&&headers.length){ + for(let item in headers){ + res.push({ + name: headers[item].key, + desc: headers[item].description, + value: headers[item].value, + required: headers[item].enable + }); + } + } + return res; + } + + function handleReq_body_form(body_form){ + let res = []; + if(body_form&&body_form.length){ + for(let item in body_form){ + res.push({ + name: body_form[item].key, + value: body_form[item].value, + type: body_form[item].type + }); + } + } + return res; + } + + function handlePath(path){ + path = path.replace(/{{(\w*)}}/,""); + path = parseUrl(path).pathname; + if(path.charAt(0) != "/"){ + path = "/" + path; + } + if(path.charAt(path.length-1) === "/"){ + path = path.substr(0,path.length-1); + } + return path; + } + + function run(res){ + try{ + res = JSON.parse(res); + let interData = res.requests; + let interfaceData = {apis: []}; + interData = checkInterRepeat.bind(this)(interData); + if(interData && interData.length){ + for(let item in interData){ + let data = importPostman.bind(this)(interData[item]); + interfaceData.apis.push(data); + } + } + return interfaceData; + + }catch(e){ + message.error("文件格式必须为JSON"); + } + + } + + function importPostman(data,key){ + let reflect = {//数据字段映射关系 + title: "name", + path: "url", + method: "method", + desc: "description", + req_query: "queryParams", + req_headers: "headerData", + req_params: "", + req_body_type: "dataMode", + req_body_form: "data", + req_body_other: "rawModeData" + }; + let allKey = ["title","path","method","desc","req_query","req_headers","req_body_type","req_body_form","req_body_other"]; + key = key || allKey; + let res = {}; + for(let item in key){ + item = key[item]; + if(item === "req_query"){ + res[item] = handleReq_query.bind(this)(data[reflect[item]]); + }else if(item === "req_headers"){ + res[item] = handleReq_headers.bind(this)(data[reflect[item]]); + }else if(item === "req_body_form"){ + res[item] = handleReq_body_form.bind(this)(data[reflect[item]]); + }else if(item === "req_body_type"){ + if(data.headers.indexOf('application/json')>-1){ + res[item] = "json"; + }else{ + res[item] = "raw"; + } + }else if(item === "path"){ + res[item] = handlePath.bind(this)(data[reflect[item]]); + if(res[item] && res[item].indexOf("/:") > -1){ + let params = res[item].substr(res[item].indexOf("/:")+2).split("/:"); + // res[item] = res[item].substr(0,res[item].indexOf("/:")); + let arr = []; + for(let i in params){ + arr.push({ + name: params[i], + desc: "" + }); + } + res["req_params"] = arr; + } + }else if(item === "title"){ + let path = handlePath.bind(this)(data[reflect["path"]]); + if(data[reflect[item]].indexOf(path) > -1){ + res[item] = path; + if(res[item] && res[item].indexOf("/:") > -1){ + res[item] = res[item].substr(0,res[item].indexOf("/:")); + } + }else{ + res[item] = data[reflect[item]]; + } + }else{ + res[item] = data[reflect[item]]; + } + } + return res; + } + + if(!importDataModule || typeof importDataModule !== 'object'){ + console.error('obj参数必需是一个对象'); + return null; + } + + importDataModule.postman = { + name: 'Postman', + run: run, + desc: '注意:只支持json格式数据' + } +} + + + +module.exports = function(){ + + + this.bindHook('import_data', postman) +} \ No newline at end of file