fix: update pathCombine function to handle multiple URL segments for improved URL construction

This commit is contained in:
Tudor Stanciu 2025-10-05 02:04:50 +03:00
parent 358373adfd
commit 6b84544f9a
2 changed files with 26 additions and 10 deletions

View File

@ -12,8 +12,8 @@ import { pathCombine } from '@/utils/paths';
const isDevelopment = config.MODE === 'development'; const isDevelopment = config.MODE === 'development';
const BASE_URL = isDevelopment const BASE_URL = isDevelopment
? pathCombine(config.VITE_API_URL, '/api') ? pathCombine(config.VITE_API_URL, config.BASE_PATH, '/api')
: '/api'; : pathCombine(config.BASE_PATH, '/api');
const apiClient = axios.create({ const apiClient = axios.create({
baseURL: BASE_URL, baseURL: BASE_URL,

View File

@ -1,13 +1,29 @@
/** /**
* Combines base URL and path, ensuring proper slash handling * Combines multiple URL segments, ensuring proper slash handling
* @param baseUrl - Base URL (e.g., 'http://localhost:5172' or 'http://localhost:5172/') * @param segments - URL segments to combine (e.g., 'http://localhost:5172', '/api', 'health')
* @param path - Path to append (e.g., '/api' or 'api') * @returns Combined URL path with proper slash separators
* @returns Combined URL path * @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 pathCombine = (...segments: string[]): string => {
const trimmedBase = baseUrl.replace(/\/+$/, ''); // Remove trailing slashes if (segments.length === 0) return '';
const trimmedPath = path.replace(/^\/+/, ''); // Remove leading slashes if (segments.length === 1) return segments[0];
return `${trimmedBase}/${trimmedPath}`;
// 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 }; export { pathCombine };