From 6b84544f9a12a298a5f980e903c6c53c0d5152bd Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Sun, 5 Oct 2025 02:04:50 +0300 Subject: [PATCH] fix: update pathCombine function to handle multiple URL segments for improved URL construction --- src/frontend/src/services/api.ts | 4 ++-- src/frontend/src/utils/paths.ts | 32 ++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/frontend/src/services/api.ts b/src/frontend/src/services/api.ts index 506a4d2..55dfaa0 100644 --- a/src/frontend/src/services/api.ts +++ b/src/frontend/src/services/api.ts @@ -12,8 +12,8 @@ import { pathCombine } from '@/utils/paths'; const isDevelopment = config.MODE === 'development'; const BASE_URL = isDevelopment - ? pathCombine(config.VITE_API_URL, '/api') - : '/api'; + ? pathCombine(config.VITE_API_URL, config.BASE_PATH, '/api') + : pathCombine(config.BASE_PATH, '/api'); const apiClient = axios.create({ baseURL: BASE_URL, diff --git a/src/frontend/src/utils/paths.ts b/src/frontend/src/utils/paths.ts index 67e5727..6362eb7 100644 --- a/src/frontend/src/utils/paths.ts +++ b/src/frontend/src/utils/paths.ts @@ -1,13 +1,29 @@ /** - * Combines base URL and path, ensuring proper slash handling - * @param baseUrl - Base URL (e.g., 'http://localhost:5172' or 'http://localhost:5172/') - * @param path - Path to append (e.g., '/api' or 'api') - * @returns Combined URL path + * Combines multiple URL segments, ensuring proper slash handling + * @param segments - URL segments to combine (e.g., 'http://localhost:5172', '/api', 'health') + * @returns Combined URL path with proper slash separators + * @example + * pathCombine('http://localhost:5172', '/api', 'health') // 'http://localhost:5172/api/health' + * pathCombine('/base', 'api/', '/endpoint') // '/base/api/endpoint' + * pathCombine('/', '/api') // '/api' */ -const pathCombine = (baseUrl: string, path: string): string => { - const trimmedBase = baseUrl.replace(/\/+$/, ''); // Remove trailing slashes - const trimmedPath = path.replace(/^\/+/, ''); // Remove leading slashes - return `${trimmedBase}/${trimmedPath}`; +const pathCombine = (...segments: string[]): string => { + if (segments.length === 0) return ''; + if (segments.length === 1) return segments[0]; + + // Process first segment (keep leading slash or protocol) + let result = segments[0].replace(/\/+$/, ''); + + // Process middle and last segments + for (let i = 1; i < segments.length; i++) { + const segment = segments[i].replace(/^\/+|\/+$/g, ''); // Remove leading and trailing slashes + if (segment) { + // Only add non-empty segments + result += `/${segment}`; + } + } + + return result; }; export { pathCombine };