All files / src/lib http-client.ts

62.5% Statements 20/32
20% Branches 2/10
78.57% Functions 11/14
55.55% Lines 15/27

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107          26x     26x                 26x     38x 38x         38x 38x           38x                 26x                                                                   26x   163x             12x             8x             6x     6x        
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import axios from 'axios';
import { useAuthStore } from '@/store/auth-store';
 
// API Configuration
const API_BASE_URL = import.meta.env.VITE_API_URL;
 
// Create Axios instance with default configuration
const httpClient: AxiosInstance = axios.create({
  baseURL: API_BASE_URL,
  timeout: 10000, // 10 seconds
  headers: {
    'Content-Type': 'application/json',
  },
});
 
// Request interceptor for authentication and logging
httpClient.interceptors.request.use(
  config => {
    // Add auth token if available from Zustand store
    const { token } = useAuthStore.getState();
    Iif (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
 
    // Log request in development
    Eif (import.meta.env.DEV) {
      console.log(
        `🚀 Request: ${config.method?.toUpperCase()} ${config.url}`,
        config
      );
    }
 
    return config;
  },
  error => {
    console.error('Request interceptor error:', error);
    return Promise.reject(error);
  }
);
 
// Response interceptor for error handling and logging
httpClient.interceptors.response.use(
  (response: AxiosResponse) => {
    // Log response in development
    if (import.meta.env.DEV) {
      console.log(
        `✅ Response: ${response.config.method?.toUpperCase()} ${response.config.url}`,
        response
      );
    }
 
    return response;
  },
  error => {
    // Log error in development
    if (import.meta.env.DEV) {
      console.error(
        `❌ Error: ${error.config?.method?.toUpperCase()} ${error.config?.url}`,
        error
      );
    }
 
    // Handle common error scenarios
    if (error.response?.status === 401) {
      // Handle unauthorized - clear auth state from Zustand store
      const { logout } = useAuthStore.getState();
      logout();
      // You can add redirect logic here
    }
 
    return Promise.reject(error);
  }
);
 
// Generic API methods
export const apiClient = {
  get: <T = any>(url: string, config?: AxiosRequestConfig): Promise<T> =>
    httpClient.get(url, config).then(response => response.data),
 
  post: <T = any>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig
  ): Promise<T> =>
    httpClient.post(url, data, config).then(response => response.data),
 
  put: <T = any>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig
  ): Promise<T> =>
    httpClient.put(url, data, config).then(response => response.data),
 
  patch: <T = any>(
    url: string,
    data?: any,
    config?: AxiosRequestConfig
  ): Promise<T> =>
    httpClient.patch(url, data, config).then(response => response.data),
 
  delete: <T = any>(url: string, config?: AxiosRequestConfig): Promise<T> =>
    httpClient.delete(url, config).then(response => response.data),
};
 
export default httpClient;