mock平台

diff-view.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // const json5_parse = require('../client/common.js').json5_parse;
  2. const json5 = require('json5');
  3. module.exports = function(jsondiffpatch, formattersHtml, curDiffData) {
  4. const json5_parse = json => {
  5. if (typeof json === 'object' && json) return json;
  6. try {
  7. return json5.parse(json);
  8. } catch (err) {
  9. return json;
  10. }
  11. };
  12. const diffText = (left, right) => {
  13. left = left || '';
  14. right = right || '';
  15. if (left == right) {
  16. return null;
  17. }
  18. var delta = jsondiffpatch.diff(left, right);
  19. let result = formattersHtml.format(delta, left);
  20. return result;
  21. };
  22. const diffJson = (left, right) => {
  23. left = json5_parse(left);
  24. right = json5_parse(right);
  25. let delta = jsondiffpatch.diff(left, right);
  26. return formattersHtml.format(delta, left);
  27. // return '';
  28. };
  29. const valueMaps = {
  30. '1': '必需',
  31. '0': '非必需',
  32. text: '文本',
  33. file: '文件',
  34. undone: '未完成',
  35. done: '已完成'
  36. };
  37. const handleParams = item => {
  38. let newItem = Object.assign({}, item);
  39. newItem._id = undefined;
  40. Object.keys(newItem).forEach(key => {
  41. switch (key) {
  42. case 'required':
  43. newItem[key] = valueMaps[newItem[key]];
  44. break;
  45. case 'type':
  46. newItem[key] = valueMaps[newItem[key]];
  47. break;
  48. }
  49. });
  50. return newItem;
  51. };
  52. const diffArray = (arr1, arr2) => {
  53. arr1 = arr1 || [];
  54. arr2 = arr2 || [];
  55. arr1 = arr1.map(handleParams);
  56. arr2 = arr2.map(handleParams);
  57. return diffJson(arr1, arr2);
  58. };
  59. let diffView = [];
  60. if (curDiffData && typeof curDiffData === 'object' && curDiffData.current) {
  61. const { current, old, type } = curDiffData;
  62. // wiki 信息的diff 输出
  63. if (type === 'wiki') {
  64. if (current != old) {
  65. diffView.push({
  66. title: 'wiki更新',
  67. content: diffText(old, current)
  68. });
  69. }
  70. return (diffView = diffView.filter(item => item.content));
  71. }
  72. if (current.path != old.path) {
  73. diffView.push({
  74. title: 'Api 路径',
  75. content: diffText(old.path, current.path)
  76. });
  77. }
  78. if (current.title != old.title) {
  79. diffView.push({
  80. title: 'Api 名称',
  81. content: diffText(old.title, current.title)
  82. });
  83. }
  84. if (current.method != old.method) {
  85. diffView.push({
  86. title: 'Method',
  87. content: diffText(old.method, current.method)
  88. });
  89. }
  90. if (current.catid != old.catid) {
  91. diffView.push({
  92. title: '分类 id',
  93. content: diffText(old.catid, current.catid)
  94. });
  95. }
  96. if (current.status != old.status) {
  97. diffView.push({
  98. title: '接口状态',
  99. content: diffText(valueMaps[old.status], valueMaps[current.status])
  100. });
  101. }
  102. if (current.tag !== old.tag) {
  103. diffView.push({
  104. title: '接口tag',
  105. content: diffText(old.tag, current.tag)
  106. });
  107. }
  108. diffView.push({
  109. title: 'Request Path Params',
  110. content: diffArray(old.req_params, current.req_params)
  111. });
  112. diffView.push({
  113. title: 'Request Query',
  114. content: diffArray(old.req_query, current.req_query)
  115. });
  116. diffView.push({
  117. title: 'Request Header',
  118. content: diffArray(old.req_headers, current.req_headers)
  119. });
  120. let oldValue = current.req_body_type === 'form' ? old.req_body_form : old.req_body_other;
  121. if (current.req_body_type !== old.req_body_type) {
  122. diffView.push({
  123. title: 'Request Type',
  124. content: diffText(old.req_body_type, current.req_body_type)
  125. });
  126. oldValue = null;
  127. }
  128. if (current.req_body_type === 'json') {
  129. diffView.push({
  130. title: 'Request Body',
  131. content: diffJson(oldValue, current.req_body_other)
  132. });
  133. } else if (current.req_body_type === 'form') {
  134. diffView.push({
  135. title: 'Request Form Body',
  136. content: diffArray(oldValue, current.req_body_form)
  137. });
  138. } else {
  139. diffView.push({
  140. title: 'Request Raw Body',
  141. content: diffText(oldValue, current.req_body_other)
  142. });
  143. }
  144. let oldResValue = old.res_body;
  145. if (current.res_body_type !== old.res_body_type) {
  146. diffView.push({
  147. title: 'Response Type',
  148. content: diffText(old.res_body_type, current.res_body_type)
  149. });
  150. oldResValue = '';
  151. }
  152. if (current.res_body_type === 'json') {
  153. diffView.push({
  154. title: 'Response Body',
  155. content: diffJson(oldResValue, current.res_body)
  156. });
  157. } else {
  158. diffView.push({
  159. title: 'Response Body',
  160. content: diffText(oldResValue, current.res_body)
  161. });
  162. }
  163. }
  164. return (diffView = diffView.filter(item => item.content));
  165. };