1.添加url正则对象获取方法

2.调整地址匹配多个时逻辑处理为:
> 优先使用地址中动态数据较少的地址
> 出现动态数据数量相同时,抛出异常
pull/484/head
irving.zhao 6 years ago
parent 3f6c6d9fa5
commit 197e192fdb

@ -219,22 +219,40 @@ router.all('/app/mock/:repositoryId(\\d+)/:url(.+)', async (ctx) => {
}
})
let listMatched = []
let listMatched = [];
let relativeUrl = urlUtils.getRelative(url);
for (let item of list) {
if (urlUtils.urlMatchesPattern(url, item.url)) {
listMatched.push(item)
let regExp = urlUtils.getUrlPattern(item.url); // 获取地址匹配正则
if (regExp.test(relativeUrl)) { // 检查地址是否匹配
let regMatchLength = regExp.exec(relativeUrl).length; // 执行地址匹配
if (listMatched[regMatchLength]) { // 检查匹配地址中是否具有同group数量的数据
ctx.body = {
isOk: false,
errMsg: "匹配到多个同级别接口,请修改规则确保接口规则唯一性。"
};
return;
}
listMatched[regMatchLength] = item; // 写入数据
}
}
let loadDataId = 0;
if (listMatched.length > 1) {
ctx.body = { isOk: false, errMsg: '匹配到多个接口,请修改规则确保接口规则唯一性。 Matched multiple interfaces, please ensure pattern to be unique.' }
return
for (let matchedItem of listMatched) { // 循环匹配内的数据
if (matchedItem) { // 忽略为空的数据
loadDataId = matchedItem.id; // 设置需查询的id
break;
}
}
} else if (listMatched.length === 0) {
ctx.body = { isOk: false, errMsg: '未匹配到任何接口 No matched interface' }
ctx.body = {isOk: false, errMsg: '未匹配到任何接口 No matched interface'}
return
} else {
itf = await Interface.findById(listMatched[0].id)
loadDataId = listMatched[0].id;
}
itf = itf = await Interface.findById(loadDataId);
}
let interfaceId = itf.id

@ -29,5 +29,9 @@ export default class UrlUtils {
let re = pathToRegexp(pattern)
return re.test(url)
}
public static getUrlPattern = (pattern: string) => {
pattern = UrlUtils.getRelative(pattern);
return pathToRegexp(pattern);
}
}

Loading…
Cancel
Save