This commit is contained in:
Tomas Dvorak
2025-10-31 18:22:04 +01:00
parent 16e4533202
commit ac886502e0
65 changed files with 3211 additions and 553 deletions
+8 -9
View File
@@ -1,24 +1,23 @@
import axios, { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from 'axios';
import { getToken } from '../utils/auth';
// Resolve API URL. Some code uses REACT_APP_API_URL (full api path including /api/v1),
// others set REACT_APP_API_BASE_URL (backend origin). Normalize so baseURL always points to API root.
const envApiUrl = process.env.REACT_APP_API_URL || process.env.REACT_APP_API_BASE_URL;
let API_URL = envApiUrl || '/api/v1';
function readStored(key: string): string | null {
try { return localStorage.getItem(key); } catch { return null; }
}
const storedApi = typeof window !== 'undefined' ? (readStored('fc_api_base_url') || readStored('api_base_url')) : null;
const envApiUrl = process.env.REACT_APP_API_URL || process.env.REACT_APP_API_BASE_URL;
let API_URL = storedApi || envApiUrl || '/api/v1';
// If the provided base looks like a backend origin (no /api/), append /api/v1
try {
const maybe = new URL(API_URL, typeof window !== 'undefined' ? window.location.origin : undefined);
if (!/\/api\//.test(maybe.pathname)) {
// ensure single trailing slash then append api/v1
maybe.pathname = maybe.pathname.replace(/\/$/, '') + '/api/v1';
API_URL = maybe.toString();
} else {
API_URL = maybe.toString();
}
} catch {
// If URL parsing fails, keep API_URL as-is
}
} catch {}
export const api: AxiosInstance = axios.create({
baseURL: API_URL,