File size: 990 Bytes
5e44686
 
 
35454f8
 
 
 
 
 
 
 
 
 
 
 
5e44686
35454f8
5e44686
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import axios from "axios";
import { toast } from "react-toastify";

// Use runtime configuration with fallback to environment variable
// Handle SSR by checking if window is available
const getBaseURL = () => {
    if (typeof window !== 'undefined' && (window as any).APP_CONFIG?.API_BASE_URL) {
        return (window as any).APP_CONFIG.API_BASE_URL;
    }
    // Fallback to environment variable during build time
    return import.meta.env.VITE_APP_API_URL || undefined;
};

const baseURL = getBaseURL();

export const HTTPManager = axios.create({
    baseURL: baseURL,
    headers: {
        "Content-Type": "application/json",
        "Accept": "application/json",
    },
});

HTTPManager.interceptors.request.use((config) => {
    const token = localStorage.getItem("token");
    if (token) {
        config.headers["Authorization"] = `Bearer ${token}`;
    }
    return config;
}, (error) => {
    toast.error("Request error: " + error.message);
    return Promise.reject(error);
});