feat: update http service

This commit is contained in:
hanif salafi 2025-07-20 02:00:02 +07:00
parent 3ed1327707
commit ac332b7099
2 changed files with 32 additions and 3 deletions

View File

@ -6,6 +6,13 @@ const baseURL = "https://mediahub.polri.go.id/api/v2/";
const refreshToken = Cookies.get("refresh_token");
// 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}`;
}
const axiosInterceptorInstance = axios.create({
baseURL,
headers: {
@ -22,6 +29,12 @@ axiosInterceptorInstance.interceptors.request.use(
if (config.headers)
config.headers.Authorization = "Bearer " + accessToken;
}
// Add timestamp to URL to prevent caching
if (config.url) {
config.url = addTimestampToUrl(config.url);
}
return config;
},
(error) => {

View File

@ -9,6 +9,13 @@ 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}`;
}
async function getCachedCsrfToken() {
const now = Date.now();
@ -51,8 +58,11 @@ export async function httpPost(pathUrl: any, data?: any, headers?: any,) {
...headers,
};
// Add timestamp to URL to prevent caching
const urlWithTimestamp = addTimestampToUrl(pathUrl);
const response = await axiosBaseInstance
.post(pathUrl, data, { headers: mergedHeaders })
.post(urlWithTimestamp, data, { headers: mergedHeaders })
.catch(function (error: any) {
console.log(error);
return error.response;
@ -74,8 +84,11 @@ export async function httpPost(pathUrl: any, data?: any, headers?: any,) {
}
export async function httpGet(pathUrl: any, headers: any) {
// Add timestamp to URL to prevent caching
const urlWithTimestamp = addTimestampToUrl(pathUrl);
const response = await axiosBaseInstance
.get(pathUrl, { headers })
.get(urlWithTimestamp, { headers })
.catch(function (error: any) {
console.log(error);
return error.response;
@ -110,8 +123,11 @@ export async function httpPut(pathUrl: any, headers: any, data?: any) {
...(csrfToken ? { "X-XSRF-TOKEN": csrfToken } : {}),
};
// Add timestamp to URL to prevent caching
const urlWithTimestamp = addTimestampToUrl(pathUrl);
const response = await axiosBaseInstance
.put(pathUrl, data, { headers: mergedHeaders })
.put(urlWithTimestamp, data, { headers: mergedHeaders })
.catch(function (error: any) {
console.log(error);
return error.response;