Spaces:
Running
Running
File size: 1,193 Bytes
4fb0c68 |
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 |
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;
|