Files
cunkebao_v3/Cunkebao/src/utils/apiUrl.ts
超级老白兔 9c3bc5200a 同步代码
2025-08-12 09:27:50 +08:00

74 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* API URL工具函数
* 用于统一处理API地址的拼接逻辑
*
* URL结构: {VITE_API_BASE_URL}/v1/api/scenarios/{path}
*
* 示例:
* - 开发环境: http://localhost:3000/api/v1/api/scenarios/webhook/123
* - 生产环境: https://api.example.com/v1/api/scenarios/webhook/123
*/
/**
* 获取完整的API基础路径
* @returns 完整的API基础路径包含 /v1/api/scenarios
*
* 示例:
* - 开发环境: http://localhost:3000/api/v1/api/scenarios
* - 生产环境: https://api.example.com/v1/api/scenarios
*/
export const getFullApiPath = (): string => {
const apiBaseUrl = (import.meta as any).env?.VITE_API_BASE_URL || "/api";
return `${apiBaseUrl}/v1/api/scenarios`;
};
/**
* 构建完整的API URL
* @param path 相对路径或完整URL
* @returns 完整的API URL
*
* 示例:
* - buildApiUrl('/webhook/123') → 'http://localhost:3000/api/v1/api/scenarios/webhook/123'
* - buildApiUrl('webhook/123') → 'http://localhost:3000/api/v1/api/scenarios/webhook/123'
* - buildApiUrl('https://api.example.com/webhook/123') → 'https://api.example.com/webhook/123'
*/
export const buildApiUrl = (path: string): string => {
if (!path) return "";
// 如果已经是完整的URL包含http或https直接返回
if (path.startsWith("http://") || path.startsWith("https://")) {
return path;
}
const fullApiPath = getFullApiPath();
// 如果是相对路径拼接完整API路径
if (path.startsWith("/")) {
return `${fullApiPath}${path}`;
}
// 其他情况拼接完整API路径和路径
return `${fullApiPath}?${path}`;
};
/**
* 构建webhook URL
* @param taskId 任务ID
* @param path 可选的相对路径
* @returns 完整的webhook URL
*
* 示例:
* - buildWebhookUrl('123') → 'http://localhost:3000/api/v1/api/scenarios/webhook/123'
* - buildWebhookUrl('123', '/custom/path') → 'http://localhost:3000/api/v1/api/scenarios/custom/path'
*/
export const buildWebhookUrl = (taskId: string, path?: string): string => {
const fullApiPath = getFullApiPath();
const webhookPath = path || `/webhook/${taskId}`;
if (webhookPath.startsWith("/")) {
return `${fullApiPath}${webhookPath}`;
}
return `${fullApiPath}/${webhookPath}`;
};