mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
fix: cache update not being executed correctly (#283)
* test: added flaky test * fix: fixed flaky test * style: formatted code
This commit is contained in:
parent
03cc615e67
commit
cf97535d33
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"unreleased": true,
|
"unreleased": true,
|
||||||
"commitLimit": false,
|
"commitLimit": false,
|
||||||
"sortCommits": "subject",
|
"sortCommits": "subject",
|
||||||
"template": "keepachangelog"
|
"template": "keepachangelog"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -65,6 +65,11 @@ export function defaultResponseInterceptor(
|
|||||||
const config = response.config;
|
const config = response.config;
|
||||||
const cache = await axios.storage.get(id, config);
|
const cache = await axios.storage.get(id, config);
|
||||||
|
|
||||||
|
// Update other entries before updating himself
|
||||||
|
if (cacheConfig?.update) {
|
||||||
|
await updateCache(axios.storage, response, cacheConfig.update);
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
// If the request interceptor had a problem or it wasn't cached
|
// If the request interceptor had a problem or it wasn't cached
|
||||||
cache.state !== 'loading'
|
cache.state !== 'loading'
|
||||||
@ -72,7 +77,7 @@ export function defaultResponseInterceptor(
|
|||||||
if (__ACI_DEV__) {
|
if (__ACI_DEV__) {
|
||||||
axios.debug?.({
|
axios.debug?.({
|
||||||
id,
|
id,
|
||||||
msg: 'Response not cached but storage is not loading',
|
msg: "Response not cached and storage isn't loading",
|
||||||
data: { cache, response }
|
data: { cache, response }
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -157,11 +162,6 @@ export function defaultResponseInterceptor(
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update other entries before updating himself
|
|
||||||
if (cacheConfig?.update) {
|
|
||||||
await updateCache(axios.storage, response, cacheConfig.update);
|
|
||||||
}
|
|
||||||
|
|
||||||
const newCache: CachedStorageValue = {
|
const newCache: CachedStorageValue = {
|
||||||
state: 'cached',
|
state: 'cached',
|
||||||
ttl,
|
ttl,
|
||||||
@ -171,6 +171,7 @@ export function defaultResponseInterceptor(
|
|||||||
|
|
||||||
// Resolve all other requests waiting for this response
|
// Resolve all other requests waiting for this response
|
||||||
const waiting = axios.waiting[id];
|
const waiting = axios.waiting[id];
|
||||||
|
|
||||||
if (waiting) {
|
if (waiting) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||||
waiting.resolve(newCache.data);
|
waiting.resolve(newCache.data);
|
||||||
|
|||||||
@ -120,4 +120,24 @@ describe('Tests update-cache', () => {
|
|||||||
const cacheValue = await axios.storage.get(cacheKey);
|
const cacheValue = await axios.storage.get(cacheKey);
|
||||||
expect(cacheValue.state).toBe('loading');
|
expect(cacheValue.state).toBe('loading');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('tests updateCache with non cached updater', async () => {
|
||||||
|
const ID = 'cache-id';
|
||||||
|
|
||||||
|
const axios = mockAxios({ methods: ['get'] });
|
||||||
|
|
||||||
|
await axios.get('url', { id: ID });
|
||||||
|
|
||||||
|
// post isn't inside `methods`
|
||||||
|
await axios.post('url', true, {
|
||||||
|
cache: {
|
||||||
|
update: { [ID]: 'delete' }
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// if the update did not get executed, the cache shouldn't be empty
|
||||||
|
const cacheValue = await axios.storage.get(ID);
|
||||||
|
|
||||||
|
expect(cacheValue.state).toBe('empty');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user