mock-convert
Version:
convert interface data to mock data
273 lines (257 loc) • 7.88 kB
JavaScript
const { URL } = require('url');
const ISPACE = '';
function removeEnter(strSrc) {
if (strSrc) {
return strSrc.replace(/[\n|\r]/g, ' ').trim();
}
return strSrc;
}
function adjustRemark(remark, space) {
const result = removeEnter(remark);
if (result && space) {
return result.replace(/[\n]/g, space).trim();
}
return result.trim();
}
function adjustByOpt(parseOpt, params) {
if (!parseOpt || !params) {
return null;
}
for (let i = 0, len = parseOpt.length; i < len; i++) {
const {
dataType,
identifier,
ignoreCase,
fullMatch,
target,
master,
} = parseOpt[i];
if (dataType.indexOf(params.dataType) >= 0) {
let optVar = identifier;
let paramVar = params.identifier;
if (master) {
optVar = parseOpt[i][master];
paramVar = params[master];
}
if (ignoreCase) {
paramVar = paramVar.toLowerCase();
}
if (fullMatch) {
if (optVar === paramVar) {
return target;
}
} else if (paramVar.indexOf(optVar) >= 0) {
return target;
}
}
}
return null;
}
function parseRequestList(requestJson) {
let requestResult = '';
const requestSpace = `${ISPACE} `;
if (requestJson && requestJson.length > 0) {
requestResult += (`${requestSpace}const reqParams = req.body || {};\n`);
requestResult += (`${requestSpace}console.log(reqParams);\n`);
for (let i = 0, len = requestJson.length; i < len; i++) {
const {
identifier,
isNecessary,
name,
remark,
} = requestJson[i];
const n = removeEnter(name);
if (n) {
requestResult += `${requestSpace}// ${n}, necessary: ${isNecessary}\n`;
}
if (remark) {
const r = adjustRemark(remark, `\n${requestSpace}`);
requestResult += `${requestSpace}/* ${r} */\n`;
}
requestResult += `${requestSpace}// const ${identifier} = reqParams.${identifier};\n`;
}
}
return requestResult;
}
function parseParameterList(paramJson, parseOpt, space) {
let result = '';
if (paramJson) {
const {
dataType,
identifier,
name,
remark,
parameterList,
mock,
} = paramJson;
// let target = adjustByOpt(parseOpt, dataType, identifier);
const target = adjustByOpt(parseOpt, paramJson);
if (name) {
const n = removeEnter(name);
result += `${space}// ${n}\n`;
}
if (remark) {
// if (remark.indexOf('0-下架') !== -1) {
// console.log('====');
// }
const r = adjustRemark(remark, `\n${space}`);
result += `${space}/* ${r} */\n`;
}
result += space;
if (mock) {
if (mock.indexOf(identifier) !== -1) {
result += `${mock},`;
} else {
result += `${identifier}: '${mock}',`;
}
} else if (target) {
result += target.replace(/\$identifier\$/, identifier);
result += ',';
} else if (dataType === 'string') {
result += `${identifier}: '@word(3,5)',`;
} else if (dataType === 'int' || dataType === 'long') {
result += `'${identifier}|1-1000': 100,`;
} else if (dataType === 'bigdecimal') {
result += `${identifier}: '@float(-1000, 1000, 2, 2)',`;
} else if (dataType === 'object') {
result += `${identifier}: {\n`;
if (parameterList && parameterList.length > 0) {
for (let j = 0, len = parameterList.length; j < len; j++) {
result += parseParameterList(parameterList[j], parseOpt, `${space} `);
}
}
result += space;
result += '},';
} else if (dataType === 'array<object>') {
result += `'${identifier}|2-10': [{\n`;
if (parameterList && parameterList.length > 0) {
for (let j = 0, len = parameterList.length; j < len; j++) {
result += parseParameterList(parameterList[j], parseOpt, `${space} `);
}
}
result += space;
result += '}],';
} else if (dataType === 'array<string>') {
result += `'${identifier}|2-10':[ '@word(3,5)' ],`;
}
result += '\n';
}
return result;
}
function parseResponseList(responseJson, parseOption, space) {
let responseResult = '';
if (responseJson && responseJson.length > 0) {
for (let i = 0, len = responseJson.length; i < len; i++) {
responseResult += parseParameterList(responseJson[i], parseOption, space);
}
}
return responseResult;
}
function parseActionList(actionJson, basePath, parseOption) {
let actionResult = '';
const actionSpace = `${ISPACE} `;
if (actionJson) {
for (let i = 0, len = actionJson.length; i < len; i++) {
const {
name,
description,
requestParameterList,
requestUrl,
responseParameterList,
} = actionJson[i];
if (name) {
const n = removeEnter(name);
actionResult += `${actionSpace}// ${n}\n`;
}
if (description) {
const desc = description.replace(/ /g, '').trim();
actionResult += `${actionSpace}/* ${desc} */\n`;
}
const reqUrl = new URL(requestUrl);
let urlPath = reqUrl.pathname;
if (urlPath[0] === '/' && urlPath.length > 1) {
urlPath = urlPath.substring(1);
}
if (basePath) {
const index = urlPath.indexOf(basePath);
if (index > -1) {
urlPath = urlPath.substring(index + basePath.length);
}
}
actionResult += `${actionSpace}'${urlPath}': {\n`;
if (requestParameterList && requestParameterList.length > 0) {
// 带参数的接口
const requestResult = parseRequestList(requestParameterList);
actionResult += (`${actionSpace} response: (req) => {\n`);
actionResult += requestResult;
actionResult += (`${actionSpace} return {\n`);
actionResult += parseResponseList(responseParameterList, parseOption, `${ISPACE} `);
actionResult += (`${actionSpace} };\n`);
actionResult += (`${actionSpace} },\n`);
} else {
// 不带参数的接口
actionResult += parseResponseList(responseParameterList, parseOption, `${ISPACE} `);
}
actionResult += (`${actionSpace}},\n`);
}
}
return actionResult;
}
function parsePageList(pageJson, basePath, parseOption) {
let pageResult = '';
if (pageJson) {
for (let i = 0, len = pageJson.length; i < len; i++) {
const {
name,
introduction,
actionList,
} = pageJson[i];
if (name) {
const n = removeEnter(name);
pageResult += ` // ${n}\n`;
}
if (introduction) {
const intro = introduction.replace(/ /g, '');
pageResult += ` /* ${intro} */\n`;
}
pageResult += parseActionList(actionList, basePath, parseOption);
}
}
return pageResult;
}
module.exports = function parseProject(jsonData, basePath, parseOpt, callback) {
const { name, moduleList, introduction } = jsonData;
const projectObj = {};
if (name) {
const n = removeEnter(name);
projectObj.name = `// ${n}\n`;
}
if (introduction) {
const intro = introduction.replace(/ /g, '');
if (introduction) {
projectObj.introduction = `/* ${intro} */\n`;
}
}
if (moduleList) {
projectObj.moduleList = [];
for (let i = 0, moduleLen = moduleList.length; i < moduleLen; i++) {
const {
name: moduleName,
pageList,
} = moduleList[i];
const module = {};
let moduleResult = '';
if (moduleName) {
const n = removeEnter(moduleName);
moduleResult += `// ### ${n}\n`;
}
moduleResult += parsePageList(pageList, basePath, parseOpt);
module.name = moduleName;
module.parse = moduleResult;
projectObj.moduleList.push(module);
}
}
if (callback) {
callback(null, projectObj);
}
};