import axios from 'axios'; function getCookie(name: string): string | null { let cookieValue = null; if (document.cookie && document.cookie !== '') { const cookies = document.cookie.split(';'); for (let i = 0; i < cookies.length; i++) { const cookie = cookies[i].trim(); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } const api = axios.create({ baseURL: import.meta.env.VITE_API_URL || 'http://localhost:8000/api/', }); api.interceptors.request.use((config) => { // Set DRF Auth Token if available from localStorage const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Token ${token}`; } // Set CSRF token for state-changing methods const csrfToken = getCookie('csrftoken'); const method = config.method?.toUpperCase(); if (csrfToken && method && ['POST', 'PUT', 'PATCH', 'DELETE'].includes(method)) { if (config.headers) { config.headers['X-CSRFToken'] = csrfToken; } } return config; }); export default api;