All files / frontend/src/lib api.ts

42.59% Statements 23/54
100% Branches 0/0
0% Functions 0/1
42.59% Lines 23/54

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 641x 1x   1x     1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x     1x                     1x         1x 1x               1x                         1x   1x  
import axios from 'axios';
import { useAuthStore } from '../store';
 
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000';
 
// DEBUG: Log della configurazione API
console.log('🔧 API Configuration:', {
    VITE_API_URL: import.meta.env.VITE_API_URL,
    API_BASE_URL,
    environment: import.meta.env.MODE,
    isDev: import.meta.env.DEV,
});
 
const api = axios.create({
    baseURL: API_BASE_URL,
    timeout: 10000,
    headers: {
        'Content-Type': 'application/json',
    },
});
 
// Request interceptor
api.interceptors.request.use((config) => {
    console.log('📤 API Request:', {
        method: config.method?.toUpperCase(),
        url: config.url,
        fullURL: `${config.baseURL}${config.url}`,
    });
    const { token } = useAuthStore.getState();
    if (token) {
        config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
}, (error) => {
    return Promise.reject(error);
});
 
// Response interceptor
api.interceptors.response.use(
    (response) => {
        console.log('📥 API Response:', {
            status: response.status,
            url: response.config.url,
            dataSize: JSON.stringify(response.data).length,
        });
        return response;
    },
    (error) => {
        console.error('❌ API Error:', {
            status: error.response?.status,
            url: error.config?.url,
            message: error.message,
            data: error.response?.data,
        });
        if (error.response?.status === 401) {
            useAuthStore.getState().logout();
            window.location.href = '/login';
        }
        return Promise.reject(error);
    }
);
 
export default api;