创建评查点分组的API文件
This commit is contained in:
+18
-9
@@ -58,17 +58,26 @@ export class ApiError extends Error {
|
||||
* 构建请求URL
|
||||
*/
|
||||
export function buildUrl(path: string, params?: Record<string, any>): string {
|
||||
const url = new URL(`${API_BASE_URL}${path}`);
|
||||
// 确保API_BASE_URL末尾没有斜杠,而path开头有斜杠
|
||||
const baseUrl = API_BASE_URL.endsWith('/') ? API_BASE_URL.slice(0, -1) : API_BASE_URL;
|
||||
const normalizedPath = path.startsWith('/') ? path : `/${path}`;
|
||||
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
url.searchParams.append(key, String(value));
|
||||
}
|
||||
});
|
||||
try {
|
||||
const url = new URL(`${baseUrl}${normalizedPath}`);
|
||||
|
||||
if (params) {
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null) {
|
||||
url.searchParams.append(key, String(value));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
} catch (error) {
|
||||
console.error('URL构建错误:', error);
|
||||
throw new Error(`无法构建URL: ${baseUrl}${normalizedPath}, 错误: ${error}`);
|
||||
}
|
||||
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user