yapi/common/diff-view.js

191 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2018-01-04 15:06:14 +08:00
// const json5_parse = require('../client/common.js').json5_parse;
const json5 = require('json5');
2018-07-10 20:38:37 +08:00
module.exports = function(jsondiffpatch, formattersHtml, curDiffData) {
const json5_parse = json => {
2018-01-04 15:06:14 +08:00
if (typeof json === 'object' && json) return json;
try {
return json5.parse(json);
} catch (err) {
return json;
}
2018-07-10 20:38:37 +08:00
};
2018-01-04 15:06:14 +08:00
const diffText = (left, right) => {
left = left || '';
right = right || '';
if (left == right) {
return null;
}
2018-07-10 20:38:37 +08:00
2018-01-04 15:06:14 +08:00
var delta = jsondiffpatch.diff(left, right);
2018-07-10 20:38:37 +08:00
let result = formattersHtml.format(delta, left);
return result;
};
2018-01-04 15:06:14 +08:00
const diffJson = (left, right) => {
left = json5_parse(left);
right = json5_parse(right);
let delta = jsondiffpatch.diff(left, right);
2018-07-10 20:38:37 +08:00
return formattersHtml.format(delta, left);
2018-01-04 15:06:14 +08:00
// return '';
2018-07-10 20:38:37 +08:00
};
2018-01-04 15:06:14 +08:00
const valueMaps = {
'1': '必需',
'0': '非必需',
2018-07-10 20:38:37 +08:00
text: '文本',
file: '文件',
undone: '未完成',
done: '已完成'
};
2018-01-04 15:06:14 +08:00
2018-07-10 20:38:37 +08:00
const handleParams = item => {
2018-06-07 11:16:27 +08:00
let newItem = Object.assign({}, item);
newItem._id = undefined;
2018-07-10 20:38:37 +08:00
2018-06-07 11:16:27 +08:00
Object.keys(newItem).forEach(key => {
2018-01-04 15:06:14 +08:00
switch (key) {
2018-07-10 20:38:37 +08:00
case 'required':
newItem[key] = valueMaps[newItem[key]];
break;
case 'type':
newItem[key] = valueMaps[newItem[key]];
break;
2018-01-04 15:06:14 +08:00
}
2018-07-10 20:38:37 +08:00
});
2018-06-07 11:16:27 +08:00
return newItem;
2018-07-10 20:38:37 +08:00
};
2018-01-04 15:06:14 +08:00
const diffArray = (arr1, arr2) => {
arr1 = arr1 || [];
arr2 = arr2 || [];
arr1 = arr1.map(handleParams);
arr2 = arr2.map(handleParams);
return diffJson(arr1, arr2);
2018-07-10 20:38:37 +08:00
};
2018-01-04 15:06:14 +08:00
let diffView = [];
if (curDiffData && typeof curDiffData === 'object' && curDiffData.current) {
2018-07-02 17:13:34 +08:00
const { current, old, type } = curDiffData;
// wiki 信息的diff 输出
2018-07-10 20:38:37 +08:00
if (type === 'wiki') {
2018-07-02 17:13:34 +08:00
if (current != old) {
diffView.push({
title: 'wiki更新',
content: diffText(old, current)
2018-07-10 20:38:37 +08:00
});
2018-07-02 17:13:34 +08:00
}
2018-07-10 20:38:37 +08:00
return (diffView = diffView.filter(item => item.content));
2018-07-02 17:13:34 +08:00
}
2018-01-04 15:06:14 +08:00
if (current.path != old.path) {
diffView.push({
title: 'Api 路径',
content: diffText(old.path, current.path)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
if (current.title != old.title) {
diffView.push({
title: 'Api 名称',
content: diffText(old.title, current.title)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
if (current.method != old.method) {
diffView.push({
title: 'Method',
content: diffText(old.method, current.method)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
if (current.catid != old.catid) {
diffView.push({
title: '分类 id',
content: diffText(old.catid, current.catid)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
if (current.status != old.status) {
diffView.push({
title: '接口状态',
content: diffText(valueMaps[old.status], valueMaps[current.status])
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
2018-08-16 15:31:34 +08:00
if (current.tag !== old.tag) {
2018-08-16 15:31:34 +08:00
diffView.push({
title: '接口tag',
content: diffText(old.tag, current.tag)
});
}
2018-01-04 15:06:14 +08:00
diffView.push({
title: 'Request Path Params',
content: diffArray(old.req_params, current.req_params)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
diffView.push({
title: 'Request Query',
content: diffArray(old.req_query, current.req_query)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
diffView.push({
title: 'Request Header',
content: diffArray(old.req_headers, current.req_headers)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
let oldValue = current.req_body_type === 'form' ? old.req_body_form : old.req_body_other;
if (current.req_body_type !== old.req_body_type) {
diffView.push({
title: 'Request Type',
content: diffText(old.req_body_type, current.req_body_type)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
oldValue = null;
}
if (current.req_body_type === 'json') {
diffView.push({
title: 'Request Body',
2018-01-04 15:06:14 +08:00
content: diffJson(oldValue, current.req_body_other)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
} else if (current.req_body_type === 'form') {
diffView.push({
title: 'Request Form Body',
2018-01-04 15:06:14 +08:00
content: diffArray(oldValue, current.req_body_form)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
} else {
diffView.push({
title: 'Request Raw Body',
2018-01-04 15:06:14 +08:00
content: diffText(oldValue, current.req_body_other)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
let oldResValue = old.res_body;
if (current.res_body_type !== old.res_body_type) {
diffView.push({
title: 'Response Type',
content: diffText(old.res_body_type, current.res_body_type)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
oldResValue = '';
}
if (current.res_body_type === 'json') {
diffView.push({
title: 'Response Body',
content: diffJson(oldResValue, current.res_body)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
} else {
diffView.push({
title: 'Response Body',
content: diffText(oldResValue, current.res_body)
2018-07-10 20:38:37 +08:00
});
2018-01-04 15:06:14 +08:00
}
}
2018-07-10 20:38:37 +08:00
return (diffView = diffView.filter(item => item.content));
};