manage markdowns by path
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
const ongoingRequests = new Map();
|
||||
|
||||
export async function fetchWithCache(url, cacheKey = url, cacheExpiry = 60) {
|
||||
if (ongoingRequests.has(url)) {
|
||||
return ongoingRequests.get(url);
|
||||
}
|
||||
|
||||
const cachedData = localStorage.getItem(cacheKey);
|
||||
const now = Date.now();
|
||||
|
||||
if (cachedData) {
|
||||
const { data, timestamp } = JSON.parse(cachedData);
|
||||
if (now - timestamp < cacheExpiry * 1000) {
|
||||
console.log("Cache hit for:", url);
|
||||
return data;
|
||||
} else {
|
||||
console.log("Cache expired for:", url);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const token = localStorage.getItem("accessToken");
|
||||
const headers = {
|
||||
...(token? {Authorization: `Bearer ${token}`} : {}),
|
||||
}
|
||||
const fetchPromise = fetch(url, {headers})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
localStorage.setItem(cacheKey, JSON.stringify({ data, timestamp: now }));
|
||||
ongoingRequests.delete(url);
|
||||
return data;
|
||||
});
|
||||
ongoingRequests.set(url, fetchPromise);
|
||||
return await fetchPromise;
|
||||
} catch (error) {
|
||||
ongoingRequests.delete(url);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
88
src/utils/requestUtils.js
Normal file
88
src/utils/requestUtils.js
Normal file
@@ -0,0 +1,88 @@
|
||||
const ongoingRequests = new Map();
|
||||
|
||||
|
||||
function _default_hash_function(url, method, body){
|
||||
const url_obj = new URL(url, window.location.origin);
|
||||
|
||||
const query_params = [...url_obj.searchParams.entries()]
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([key, value]) => `${key}=${value}`)
|
||||
.join("&");
|
||||
const normalized_url = `${url_obj.origin}${url_obj.pathname}${query_params ? "?" + query_params : ""}`;
|
||||
const normalized_body = body
|
||||
? JSON.stringify(
|
||||
Object.keys(body)
|
||||
.sort()
|
||||
.reduce((acc, key) => {
|
||||
acc[key] = body[key];
|
||||
return acc;
|
||||
}, {})
|
||||
)
|
||||
: "";
|
||||
return `${method.toUpperCase()}:${normalized_url}:${normalized_body}`;
|
||||
}
|
||||
|
||||
|
||||
export async function fetch_(url, init = {}, init_options = {}){
|
||||
const default_options = {
|
||||
use_cache: true,
|
||||
cache_key: _default_hash_function(url, init.method || "GET", init.body || null),
|
||||
cache_expires: 60,
|
||||
use_token: true,
|
||||
};
|
||||
const options = { ...default_options, ...init_options };
|
||||
|
||||
|
||||
const token = options.use_token ? localStorage.getItem("accessToken") : null;
|
||||
|
||||
const request_options = {
|
||||
...init,
|
||||
headers: {
|
||||
...(init.headers || {}),
|
||||
...(token ? {Authorization: `Bearer ${token}`} : {}),
|
||||
}
|
||||
};
|
||||
|
||||
const now = Date.now();
|
||||
const cached_data = localStorage.getItem(options.cache_key);
|
||||
if(options.use_cache && cached_data){
|
||||
const {data, timestamp} = JSON.parse(cached_data);
|
||||
if(now - timestamp < options.cache_expires * 1000)
|
||||
return data;
|
||||
}
|
||||
|
||||
try {
|
||||
const fetchPromise = fetch(url, request_options)
|
||||
.then((response) => {
|
||||
if(!response.ok)
|
||||
throw new Error(`RESPONSE_ERROR: ${response.status}`);
|
||||
return response.json();
|
||||
})
|
||||
.then((data) => {
|
||||
if (options.use_cache)
|
||||
{
|
||||
localStorage.setItem(
|
||||
options.cache_key,
|
||||
JSON.stringify({ data, timestamp: now })
|
||||
);
|
||||
ongoingRequests.delete(options.cache_key);
|
||||
}
|
||||
return data;
|
||||
});
|
||||
if(options.use_cache){
|
||||
ongoingRequests.set(options.cache_key, fetchPromise);
|
||||
}
|
||||
return await fetchPromise;
|
||||
}catch(error){
|
||||
if(options.use_cache){
|
||||
ongoingRequests.delete(options.cache_key);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user