import axiosBaseInstance from "./axios-base-instance"; import axios from "axios"; import Cookies from "js-cookie"; import qs from "qs"; import { getCsrfToken } from "../auth"; // CSRF token cache let csrfTokenCache: string | null = null; let csrfTokenExpiry: number = 0; const CSRF_CACHE_DURATION = 5 * 60 * 1000; // 5 minutes // Helper function to append timestamp to URL to prevent caching function addTimestampToUrl(url: string): string { const separator = url.includes("?") ? "&" : "?"; const timestamp = Date.now(); // return `${url}${separator}timemilis=${timestamp}`; return `${url}${separator}`; } async function getCachedCsrfToken() { const now = Date.now(); // Return cached token if still valid if (csrfTokenCache && now < csrfTokenExpiry) { return csrfTokenCache; } // Fetch new token try { const resCsrf = await getCsrfToken(); const token = resCsrf?.data?.token; if (token) { csrfTokenCache = token; csrfTokenExpiry = now + CSRF_CACHE_DURATION; return token; } } catch (error) { console.error("Failed to fetch CSRF token:", error); } return null; } export async function httpPost(pathUrl: any, data?: any, headers?: any) { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; const authToken = Cookies.get("access_token"); const defaultHeaders = { "Content-Type": "application/json", }; const mergedHeaders = { ...defaultHeaders, ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), ...(authToken ? { Authorization: `Bearer ${authToken}` } : {}), ...headers, }; // Add timestamp to URL to prevent caching const urlWithTimestamp = addTimestampToUrl(pathUrl); const response = await axiosBaseInstance .post(urlWithTimestamp, data, { headers: mergedHeaders }) .catch(function (error: any) { console.log(error); return error.response; }); // Remove console.log for better performance if (response?.status == 200 || response?.status == 201) { return { error: false, message: "success", data: response?.data, }; } else { return { error: true, message: response?.data?.message || response?.data || null, data: null, }; } } // export async function httpGet(pathUrl: any, headers: any) { // const response = await axiosBaseInstance // .get(pathUrl, { headers }) // .catch(function (error: any) { // console.log(error); // return error.response; // }); // if (response?.status === 200 || response?.status === 201) { // return { // error: false, // message: "success", // data: response?.data, // }; // } // return { // error: true, // message: response?.data?.message || response?.data || null, // data: null, // }; // } export async function httpGet(pathUrl: any, headers: any) { // Add timestamp to URL to prevent caching const urlWithTimestamp = addTimestampToUrl(pathUrl); const response = await axiosBaseInstance .get(urlWithTimestamp, { headers }) .catch(function (error: any) { console.log(error); return error.response; }); // Remove console.log for better performance if (response?.status == 200 || response?.status == 201) { return { error: false, message: "success", data: response?.data, }; } else { return { error: true, message: response?.data?.message || response?.data || null, data: null, }; } } export async function httpPut(pathUrl: any, headers: any, data?: any) { const resCsrf = await getCsrfToken(); const csrfToken = resCsrf?.data?.token; const defaultHeaders = { "Content-Type": "application/json", }; const mergedHeaders = { ...defaultHeaders, ...headers, ...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}), }; // Add timestamp to URL to prevent caching const urlWithTimestamp = addTimestampToUrl(pathUrl); const response = await axiosBaseInstance .put(urlWithTimestamp, data, { headers: mergedHeaders }) .catch(function (error: any) { console.log(error); return error.response; }); // Remove console.log for better performance if (response?.status == 200 || response?.status == 201) { return { error: false, message: "success", data: response?.data, }; } else { return { error: true, message: response?.data?.message || response?.data || null, data: null, }; } }