Spaces:
Running
Running
| import axios from 'axios'; | |
| const API_URL = import.meta.env.VITE_API_URL;; | |
| // Create an axios instance | |
| const axiosInstance = axios.create({ | |
| baseURL: API_URL, | |
| }); | |
| // Add a request interceptor | |
| axiosInstance.interceptors.request.use( | |
| (config) => { | |
| // Get the token from your storage solution (e.g., localStorage) | |
| const token = localStorage.getItem('token'); | |
| if (token) { | |
| config.headers['Authorization'] = `Bearer ${token}`; | |
| } | |
| // Add any other custom headers here | |
| config.headers['xi-api-key'] = 'u2'; | |
| // Set the appropriate Content-Type based on the request data type | |
| if (config.data instanceof FormData) { | |
| config.headers['Content-Type'] = 'multipart/form-data'; | |
| } else if (typeof config.data === 'object') { | |
| config.headers['Content-Type'] = 'application/json'; | |
| } | |
| return config; | |
| }, | |
| (error) => { | |
| // Handle the error | |
| return Promise.reject(error); | |
| } | |
| ); | |
| // You can also add a response interceptor if you need to handle responses globally | |
| axiosInstance.interceptors.response.use( | |
| (response) => response, | |
| (error) => { | |
| // Handle the error | |
| return Promise.reject(error); | |
| } | |
| ); | |
| export default axiosInstance; | |