2025-09-19 17:20:05 -06:00
|
|
|
import axios from "axios";
|
|
|
|
|
import Cookies from "js-cookie";
|
|
|
|
|
import { envConfig } from "./config";
|
|
|
|
|
|
2025-10-01 15:57:38 -06:00
|
|
|
if (!envConfig.apiUrl) {
|
|
|
|
|
throw new Error("API URL is not defined in envConfig");
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-19 17:20:05 -06:00
|
|
|
const apiClient = axios.create({
|
|
|
|
|
baseURL: envConfig.apiUrl,
|
2025-10-01 15:57:38 -06:00
|
|
|
timeout: 10000,
|
2025-09-19 17:20:05 -06:00
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-01 15:57:38 -06:00
|
|
|
apiClient.interceptors.request.use(
|
|
|
|
|
(config) => {
|
|
|
|
|
const token = Cookies.get("token");
|
|
|
|
|
if (token) {
|
|
|
|
|
config.headers.Authorization = `Bearer ${token}`;
|
2025-09-19 17:20:05 -06:00
|
|
|
}
|
2025-10-01 15:57:38 -06:00
|
|
|
return config;
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
return Promise.reject(error);
|
2025-09-19 17:20:05 -06:00
|
|
|
}
|
2025-10-01 15:57:38 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
apiClient.interceptors.response.use(
|
|
|
|
|
(response) => response,
|
|
|
|
|
(error) => {
|
|
|
|
|
if (error.response) {
|
|
|
|
|
const status = error.response.status;
|
|
|
|
|
|
|
|
|
|
if (status === 401) {
|
|
|
|
|
Cookies.remove("token");
|
|
|
|
|
Cookies.remove("user");
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
typeof window !== "undefined" &&
|
|
|
|
|
!window.location.pathname.includes("/")
|
|
|
|
|
) {
|
|
|
|
|
window.location.href = "/";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status === 403) {
|
2025-11-28 19:49:48 -06:00
|
|
|
if (typeof window !== "undefined") {
|
2025-10-01 15:57:38 -06:00
|
|
|
window.location.href = "/Impresiones";
|
2025-11-28 19:49:48 -06:00
|
|
|
}
|
2025-10-01 15:57:38 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
2025-09-19 17:20:05 -06:00
|
|
|
|
|
|
|
|
export default apiClient;
|
2025-09-22 14:53:19 -06:00
|
|
|
//IO
|
2025-10-01 15:57:38 -06:00
|
|
|
//Mike
|