Spaces:
Sleeping
Sleeping
| import axios from 'axios'; | |
| // Create axios instance with base configuration | |
| // FORCE REBUILD: Fixed double /api issue by using environment variable | |
| const api = axios.create({ | |
| baseURL: process.env.REACT_APP_API_URL || 'https://linguabot-transcreation-backend.hf.space/api', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| timeout: 10000, // 10 second timeout | |
| }); | |
| // Debug: Log the API URL being used | |
| console.log('π§ API CONFIGURATION DEBUG - FIXED DOUBLE /API ISSUE:'); | |
| console.log('API Base URL:', process.env.REACT_APP_API_URL || 'https://linguabot-transcreation-backend.hf.space/api'); | |
| console.log('Environment variables:', { | |
| REACT_APP_API_URL: process.env.REACT_APP_API_URL, | |
| NODE_ENV: process.env.NODE_ENV | |
| }); | |
| console.log('Build timestamp:', new Date().toISOString()); // Force rebuild - Fixed double /api issue | |
| // Request interceptor to add auth token and user role | |
| api.interceptors.request.use( | |
| (config) => { | |
| const token = localStorage.getItem('token'); | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| // Add user role to headers | |
| const user = localStorage.getItem('user'); | |
| if (user) { | |
| try { | |
| const userData = JSON.parse(user); | |
| config.headers['user-role'] = userData.role || 'visitor'; | |
| } catch (error) { | |
| config.headers['user-role'] = 'visitor'; | |
| } | |
| } | |
| // Debug: Log the actual request URL | |
| console.log('π Making API request to:', (config.baseURL || '') + (config.url || '')); | |
| console.log('π Auth token:', token ? 'Present' : 'Missing'); | |
| return config; | |
| }, | |
| (error) => { | |
| return Promise.reject(error); | |
| } | |
| ); | |
| // Response interceptor to handle errors | |
| api.interceptors.response.use( | |
| (response) => { | |
| console.log('β API response received:', response.config.url); | |
| return response; | |
| }, | |
| (error) => { | |
| console.error('β API request failed:', error.config?.url, error.message); | |
| if (error.response?.status === 401) { | |
| // Token expired or invalid | |
| localStorage.removeItem('token'); | |
| localStorage.removeItem('user'); | |
| window.location.href = '/login'; | |
| } else if (error.response?.status === 429) { | |
| // Rate limit exceeded - retry after delay | |
| console.warn('Rate limit exceeded, retrying after delay...'); | |
| return new Promise(resolve => { | |
| setTimeout(() => { | |
| resolve(api.request(error.config)); | |
| }, 2000); // Wait 2 seconds before retry | |
| }); | |
| } else if (error.response?.status === 500) { | |
| console.error('Server error:', error.response.data); | |
| } else if (error.code === 'ECONNABORTED') { | |
| console.error('Request timeout'); | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export { api }; |