axios-cache-interceptor/src/util/update-cache.ts

25 lines
597 B
TypeScript

import { AxiosCacheInstance, CacheProperties } from '../axios';
export async function updateCache(
axios: AxiosCacheInstance,
data: any,
entries: CacheProperties['update']
) {
for (const [cacheKey, value] of Object.entries(entries)) {
if (value == 'delete') {
await axios.storage.remove(cacheKey);
continue;
}
const oldValue = await axios.storage.get(cacheKey);
const newValue = value(oldValue, data);
if (newValue === undefined) {
await axios.storage.remove(cacheKey);
continue;
}
await axios.storage.set(cacheKey, newValue);
}
}