fix: 修复 dynamicURL 错误匹配端口号

pull/41/head
zjx0905 2023-04-05 12:21:26 +08:00
parent 30c3503b22
commit 7e29e91f0a
2 changed files with 15 additions and 2 deletions

View File

@ -1,4 +1,4 @@
const dynamicRE = /:([^/]+)/g;
const dynamicRE = /\/:([^/]+)/g;
export function dynamicURL(url: string, data: AnyObject = {}): string {
return url.replace(dynamicRE, (_, $2) => data[$2]);
return url.replace(dynamicRE, (_, $2) => `/${data[$2]}`);
}

View File

@ -19,4 +19,17 @@ describe('src/helpers/dynamicURL.ts', () => {
}),
).toBe('http://api.com/tests/name/axios/type/0/list');
});
test('应该忽略端口号', () => {
expect(
dynamicURL(':8080/:id', {
id: 0,
}),
).toBe(':8080/0');
expect(
dynamicURL('http://api.com:8080/:id', {
id: 0,
}),
).toBe('http://api.com:8080/0');
});
});