import axios from "axios";
import baseUrl from "../array/base/config";
/**
* Axios instance with custom interceptors for handling requests and responses.
*/
const Axios = axios.create({
timeout: 60000,
baseURL: `${baseUrl}/api/`,
});
/**
* Request interceptor that adds an Authorization header to the request configuration if a token is present in localStorage.
*
* @param {Object} configuration - The request configuration.
* @returns {Object} The updated request configuration.
*/
Axios.interceptors.request.use((configuration) => {
!configuration?.headers?.Authorization
? (configuration.headers = {
Authorization: localStorage.getItem("token")
? `Token ${localStorage.getItem("token")}`
: "",
})
: {};
return configuration;
});
/**
* Response interceptor that handles errors and rejects the promise with the error.
*
* @param {Object} err - The response error.
* @returns {Promise} A rejected promise with the error.
*/
Axios.interceptors.response.use(
(configuration) => configuration,
(err) => {
return Promise.reject(err);
}
);
export default Axios;
Source