133 lines
3.2 KiB
TypeScript
133 lines
3.2 KiB
TypeScript
|
|
import axiosBaseInstance from "./axios-base-instance";
|
||
|
|
import axios from "axios";
|
||
|
|
import Cookies from "js-cookie";
|
||
|
|
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
|
||
|
|
|
||
|
|
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,
|
||
|
|
};
|
||
|
|
|
||
|
|
const response = await axiosBaseInstance
|
||
|
|
.post(pathUrl, 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;
|
||
|
|
});
|
||
|
|
// 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 } : {}),
|
||
|
|
};
|
||
|
|
|
||
|
|
const response = await axiosBaseInstance
|
||
|
|
.put(pathUrl, 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,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|