import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; class ApiClient { private client: AxiosInstance; constructor() { this.client = axios.create({ withCredentials: true }); } private getCsrfToken(): string { const match = document.cookie.match(/(?:^|; )csrftoken=([^;]+)/); return match ? decodeURIComponent(match[1]) : ''; } private withCsrf(config?: AxiosRequestConfig): AxiosRequestConfig { const token = this.getCsrfToken(); const csrfHeader = token ? { 'X-CSRFToken': token } : {}; return { ...config, headers: { ...csrfHeader, ...(config?.headers || {}), }, }; } get( url: string, config?: AxiosRequestConfig ): Promise> { return this.client.get(url, this.withCsrf(config)); } post( url: string, data?: unknown, config?: AxiosRequestConfig ): Promise> { return this.client.post(url, data, this.withCsrf(config)); } put( url: string, data?: unknown, config?: AxiosRequestConfig ): Promise> { return this.client.put(url, data, this.withCsrf(config)); } patch( url: string, data?: unknown, config?: AxiosRequestConfig ): Promise> { return this.client.patch(url, data, this.withCsrf(config)); } delete( url: string, config?: AxiosRequestConfig ): Promise> { return this.client.delete(url, this.withCsrf(config)); } } export const apiClient = new ApiClient(); export { isAxiosError } from 'axios';