Spaces:
Sleeping
Sleeping
| /** | |
| * Axios API client for EVG Ultimate Team. | |
| * | |
| * Configured with base URL, interceptors, and error handling. | |
| */ | |
| import axios, { AxiosError, InternalAxiosRequestConfig } from 'axios'; | |
| // Get API URL from environment variables | |
| const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000'; | |
| /** | |
| * Axios instance with pre-configured base URL and interceptors. | |
| */ | |
| const apiClient = axios.create({ | |
| baseURL: API_URL.endsWith('/api') ? API_URL : `${API_URL}/api`, | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| timeout: 10000, // 10 seconds | |
| }); | |
| /** | |
| * Request interceptor to add authentication token to requests. | |
| */ | |
| apiClient.interceptors.request.use( | |
| (config: InternalAxiosRequestConfig) => { | |
| // Get token from localStorage | |
| const token = localStorage.getItem('auth_token'); | |
| if (token && config.headers) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| return config; | |
| }, | |
| (error) => { | |
| return Promise.reject(error); | |
| } | |
| ); | |
| /** | |
| * Response interceptor to handle errors globally. | |
| */ | |
| apiClient.interceptors.response.use( | |
| (response) => { | |
| // Return the response data directly | |
| return response; | |
| }, | |
| (error: AxiosError) => { | |
| // Handle different error scenarios | |
| if (error.response) { | |
| // Server responded with error status | |
| const status = error.response.status; | |
| if (status === 401) { | |
| // Unauthorized - clear token and redirect to login | |
| localStorage.removeItem('auth_token'); | |
| localStorage.removeItem('current_user'); | |
| window.location.href = '/login'; | |
| } else if (status === 403) { | |
| // Forbidden - user doesn't have permission | |
| console.error('Access forbidden:', error.response.data); | |
| } else if (status === 404) { | |
| // Not found | |
| console.error('Resource not found:', error.response.data); | |
| } else if (status >= 500) { | |
| // Server error | |
| console.error('Server error:', error.response.data); | |
| } | |
| } else if (error.request) { | |
| // Request was made but no response received | |
| console.error('No response from server:', error.request); | |
| } else { | |
| // Something else happened | |
| console.error('Request error:', error.message); | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export default apiClient; | |