es-fetcher
Version:
Enhance your frontend application's API implementation and calls with es-fetcher. Simplify and accelerate the process like never before.
38 lines (37 loc) • 1.18 kB
JavaScript
import { createAbsoluteUrl } from './index';
import { isAbsoluteUrl } from './utils';
/**
* In-memory cache for storing fetched data.
* @type {Object.<string, any>}
*/
let cacheData = {};
/**
* Delete cached data based on a URL pattern.
*
* @param {string} urlPattern - The URL pattern for which you want to clear cached data. This pattern can include wildcards (*) to match multiple URLs.
*/
const deleteMemoryCache = (urlPattern) => {
urlPattern = new URLPattern(isAbsoluteUrl(urlPattern) ? urlPattern : createAbsoluteUrl(urlPattern));
for (const url of Object.keys(cacheData)) {
if (urlPattern.test(url)) {
delete cacheData[url];
}
}
};
/**
* Delete cached data based on an array of URL patterns.
*
* @param {string[]} urlPatterns - An array of URL patterns to match for cache deletion.
*/
const deleteMemoryCaches = (urlPatterns) => {
for (const urlPattern of urlPatterns) {
deleteMemoryCache(urlPattern);
}
};
/**
* Clear the entire in-memory cache.
*/
const clearMemoryCache = () => {
cacheData = {};
};
export { cacheData as memoryCacheData, deleteMemoryCache, deleteMemoryCaches, clearMemoryCache };