| import axios from "axios"; | |
| const api_url = import.meta.env.VITE_API_URL; | |
| console.log(api_url) | |
| const apiClient = axios.create({ | |
| baseURL: api_url, | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| }); | |
| apiClient.interceptors.request.use( | |
| (config) => { | |
| const token = localStorage.getItem("authToken"); | |
| if (token) { | |
| config.headers.Authorization = `Bearer ${token}`; | |
| } | |
| return config; | |
| }, | |
| (error) => Promise.reject(error) | |
| ); | |
| apiClient.interceptors.response.use( | |
| (response) => response, | |
| (error) => { | |
| if (error.response?.status === 401) { | |
| localStorage.removeItem("authToken"); | |
| window.location.href = "/login"; | |
| } | |
| return Promise.reject(error); | |
| } | |
| ); | |
| const api = { | |
| get: async (endpoint, config = {}) => { | |
| const response = await apiClient.get(endpoint, { ...config }); | |
| return response.data; | |
| }, | |
| post: async (endpoint, data, config = {}) => { | |
| const response = await apiClient.post(endpoint, data, { ...config }); | |
| return response.data; | |
| }, | |
| put: async (endpoint, data, config = {}) => { | |
| const response = await apiClient.put(endpoint, data, { ...config }); | |
| return response.data; | |
| }, | |
| patch: async (endpoint, data, config = {}) => { | |
| const response = await apiClient.patch(endpoint, data, { ...config }); | |
| return response.data; | |
| }, | |
| delete: async (endpoint, config = {}) => { | |
| const response = await apiClient.delete(endpoint, { ...config }); | |
| return response.data; | |
| }, | |
| }; | |
| export default api; |