mediahub-fe/service/http-config/http-base-service.ts

174 lines
4.4 KiB
TypeScript
Raw Normal View History

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
2025-07-19 19:00:02 +00:00
// Helper function to append timestamp to URL to prevent caching
2026-01-21 09:31:27 +00:00
function addTimestampToUrl(url: string): string {
const separator = url.includes("?") ? "&" : "?";
const timestamp = Date.now();
// return `${url}${separator}timemilis=${timestamp}`;
return `${url}${separator}`;
}
2025-07-19 19:00:02 +00:00
async function getCachedCsrfToken() {
const now = Date.now();
2026-01-21 03:24:55 +00:00
// Return cached token if still valid
if (csrfTokenCache && now < csrfTokenExpiry) {
return csrfTokenCache;
}
2026-01-21 03:24:55 +00:00
// Fetch new token
try {
const resCsrf = await getCsrfToken();
const token = resCsrf?.data?.token;
2026-01-21 03:24:55 +00:00
if (token) {
csrfTokenCache = token;
csrfTokenExpiry = now + CSRF_CACHE_DURATION;
return token;
}
} catch (error) {
console.error("Failed to fetch CSRF token:", error);
}
2026-01-21 03:24:55 +00:00
return null;
}
2026-01-21 03:24:55 +00:00
export async function httpPost(pathUrl: any, data?: any, headers?: any) {
2025-07-14 15:55:51 +00:00
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.token;
2025-04-24 08:43:49 +00:00
const authToken = Cookies.get("access_token");
const defaultHeaders = {
"Content-Type": "application/json",
};
const mergedHeaders = {
...defaultHeaders,
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
2026-01-21 03:24:55 +00:00
...(authToken ? { Authorization: `Bearer ${authToken}` } : {}),
2025-07-08 04:41:50 +00:00
...headers,
};
2025-07-19 19:00:02 +00:00
// Add timestamp to URL to prevent caching
2026-01-21 09:31:27 +00:00
const urlWithTimestamp = addTimestampToUrl(pathUrl);
2026-01-21 03:24:55 +00:00
const response = await axiosBaseInstance
2026-01-21 09:31:27 +00:00
.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,
};
}
}
2026-01-21 03:24:55 +00:00
// export async function httpGet(pathUrl: any, headers: any) {
// const response = await axiosBaseInstance
2026-01-21 09:31:27 +00:00
// .get(pathUrl, { headers })
2026-01-21 03:24:55 +00:00
// .catch(function (error: any) {
// console.log(error);
// return error.response;
// });
2026-01-21 09:31:27 +00:00
// if (response?.status === 200 || response?.status === 201) {
2026-01-21 03:24:55 +00:00
// return {
// error: false,
// message: "success",
// data: response?.data,
// };
// }
2026-01-21 09:31:27 +00:00
// return {
// error: true,
// message: response?.data?.message || response?.data || null,
// data: null,
// };
2026-01-21 03:24:55 +00:00
// }
2026-01-21 09:31:27 +00:00
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) {
2025-07-14 15:55:51 +00:00
const resCsrf = await getCsrfToken();
const csrfToken = resCsrf?.data?.token;
const defaultHeaders = {
"Content-Type": "application/json",
};
2026-01-21 03:24:55 +00:00
const mergedHeaders = {
...defaultHeaders,
...headers,
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
};
2025-07-19 19:00:02 +00:00
// Add timestamp to URL to prevent caching
2026-01-21 09:31:27 +00:00
const urlWithTimestamp = addTimestampToUrl(pathUrl);
2025-07-19 19:00:02 +00:00
const response = await axiosBaseInstance
2026-01-21 09:31:27 +00:00
.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,
};
}
}