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 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,

View File

@ -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 };