mock平台

client.js 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. import { message } from 'antd';
  2. import URL from 'url';
  3. const GenerateSchema = require('generate-schema/src/schemas/json.js');
  4. import { json_parse, unbase64 } from '../../common/utils.js';
  5. const transformJsonToSchema = json => {
  6. json = json || {};
  7. let jsonData = json_parse(json);
  8. jsonData = GenerateSchema(jsonData);
  9. let schemaData = JSON.stringify(jsonData);
  10. return schemaData;
  11. };
  12. function postman(importDataModule) {
  13. function parseUrl(url) {
  14. return URL.parse(url);
  15. }
  16. function checkInterRepeat(interData) {
  17. let obj = {};
  18. let arr = [];
  19. for (let item in interData) {
  20. // console.log(interData[item].url + "-" + interData[item].method);
  21. let key = interData[item].request.url + '|' + interData[item].request.method;
  22. if (!obj[key]) {
  23. arr.push(interData[item]);
  24. obj[key] = true;
  25. }
  26. }
  27. return arr;
  28. }
  29. function handleReq_query(query) {
  30. let res = [];
  31. if (query && query.length) {
  32. for (let item in query) {
  33. res.push({
  34. name: query[item].name,
  35. value: query[item].value
  36. });
  37. }
  38. }
  39. return res;
  40. }
  41. // function handleReq_headers(headers){
  42. // let res = [];
  43. // if(headers&&headers.length){
  44. // for(let item in headers){
  45. // res.push({
  46. // name: headers[item].key,
  47. // desc: headers[item].description,
  48. // value: headers[item].value,
  49. // required: headers[item].enable
  50. // });
  51. // }
  52. // }
  53. // return res;
  54. // }
  55. function handleReq_body_form(body_form) {
  56. let res = [];
  57. if (body_form && typeof body_form === 'object') {
  58. for (let item in body_form) {
  59. res.push({
  60. name: body_form[item].name,
  61. value: body_form[item].value,
  62. type: 'text'
  63. });
  64. }
  65. }
  66. return res;
  67. }
  68. function handlePath(path) {
  69. path = parseUrl(path).pathname;
  70. path = decodeURIComponent(path);
  71. if (!path) return '';
  72. path = path.replace(/{{\w*}}/g, '');
  73. if (path[0] != '/') {
  74. path = '/' + path;
  75. }
  76. return path;
  77. }
  78. function run(res) {
  79. try {
  80. res = JSON.parse(res);
  81. res = res.log.entries;
  82. res = res.filter(item => {
  83. if (!item) return false;
  84. return item.response.content.mimeType.indexOf('application/json') === 0;
  85. });
  86. let interfaceData = { apis: [] };
  87. res = checkInterRepeat.bind(this)(res);
  88. if (res && res.length) {
  89. for (let item in res) {
  90. let data = importHar.bind(this)(res[item]);
  91. interfaceData.apis.push(data);
  92. }
  93. }
  94. return interfaceData;
  95. } catch (e) {
  96. console.error(e);
  97. message.error('数据格式有误');
  98. }
  99. }
  100. function importHar(data, key) {
  101. let reflect = {
  102. //数据字段映射关系
  103. title: 'url',
  104. path: 'url',
  105. method: 'method',
  106. desc: 'description',
  107. req_query: 'queryString',
  108. req_body_form: 'params',
  109. req_body_other: 'text'
  110. };
  111. let allKey = [
  112. 'title',
  113. 'path',
  114. 'method',
  115. 'req_query',
  116. 'req_body_type',
  117. 'req_body_form',
  118. 'req_body_other',
  119. 'res_body_type',
  120. 'res_body',
  121. 'req_headers'
  122. ];
  123. key = key || allKey;
  124. let res = {};
  125. let reqType = 'json',
  126. header;
  127. data.request.headers.forEach(item => {
  128. if (!item || !item.name || !item.value) return null;
  129. if (/content-type/i.test(item.name) && item.value.indexOf('application/json') === 0) {
  130. reqType = 'json';
  131. header = 'application/json';
  132. } else if (
  133. /content-type/i.test(item.name) &&
  134. item.value.indexOf('application/x-www-form-urlencoded') === 0
  135. ) {
  136. header = 'application/x-www-form-urlencoded';
  137. reqType = 'form';
  138. } else if (
  139. /content-type/i.test(item.name) &&
  140. item.value.indexOf('multipart/form-data') === 0
  141. ) {
  142. header = 'multipart/form-data';
  143. reqType = 'form';
  144. }
  145. });
  146. for (let item in key) {
  147. item = key[item];
  148. if (item === 'req_query') {
  149. res[item] = handleReq_query.bind(this)(data.request[reflect[item]]);
  150. } else if (item === 'req_body_form' && reqType === 'form' && data.request.postData) {
  151. if (header === 'application/x-www-form-urlencoded') {
  152. res[item] = handleReq_body_form.bind(this)(data.request.postData[reflect[item]]);
  153. } else if (header === 'multipart/form-data') {
  154. res[item] = [];
  155. }
  156. } else if (item === 'req_body_other' && reqType === 'json' && data.request.postData) {
  157. res.req_body_is_json_schema = true;
  158. res[item] = transformJsonToSchema(data.request.postData.text);
  159. } else if (item === 'req_headers') {
  160. res[item] = [
  161. {
  162. name: 'Content-Type',
  163. value: header
  164. }
  165. ];
  166. } else if (item === 'req_body_type') {
  167. res[item] = reqType;
  168. } else if (item === 'path') {
  169. res[item] = handlePath.bind(this)(data.request[reflect[item]]);
  170. } else if (item === 'title') {
  171. let path = handlePath.bind(this)(data.request[reflect['path']]);
  172. if (data.request[reflect[item]].indexOf(path) > -1) {
  173. res[item] = path;
  174. if (res[item] && res[item].indexOf('/:') > -1) {
  175. res[item] = res[item].substr(0, res[item].indexOf('/:'));
  176. }
  177. } else {
  178. res[item] = data.request[reflect[item]];
  179. }
  180. } else if (item === 'res_body_type') {
  181. res[item] = 'json';
  182. } else if (item === 'res_body') {
  183. res.res_body_is_json_schema = true;
  184. if (data.response.content.encoding && data.response.content.encoding == 'base64') {
  185. //base64
  186. res[item] = transformJsonToSchema(unbase64(data.response.content.text));
  187. } else {
  188. res[item] = transformJsonToSchema(data.response.content.text);
  189. }
  190. } else {
  191. res[item] = data.request[reflect[item]];
  192. }
  193. }
  194. return res;
  195. }
  196. if (!importDataModule || typeof importDataModule !== 'object') {
  197. console.error('obj参数必需是一个对象');
  198. return null;
  199. }
  200. importDataModule.har = {
  201. name: 'HAR',
  202. run: run,
  203. desc: '使用chrome录制请求功能,具体使用请查看文档'
  204. };
  205. }
  206. module.exports = function() {
  207. this.bindHook('import_data', postman);
  208. };