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,
|
||||
"commitLimit": false,
|
||||
"sortCommits": "subject",
|
||||
"template": "keepachangelog"
|
||||
}
|
||||
{
|
||||
"unreleased": true,
|
||||
"commitLimit": false,
|
||||
"sortCommits": "subject",
|
||||
"template": "keepachangelog"
|
||||
}
|
||||
|
||||
@ -65,6 +65,11 @@ export function defaultResponseInterceptor(
|
||||
const config = response.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 the request interceptor had a problem or it wasn't cached
|
||||
cache.state !== 'loading'
|
||||
@ -72,7 +77,7 @@ export function defaultResponseInterceptor(
|
||||
if (__ACI_DEV__) {
|
||||
axios.debug?.({
|
||||
id,
|
||||
msg: 'Response not cached but storage is not loading',
|
||||
msg: "Response not cached and storage isn't loading",
|
||||
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 = {
|
||||
state: 'cached',
|
||||
ttl,
|
||||
@ -171,6 +171,7 @@ export function defaultResponseInterceptor(
|
||||
|
||||
// Resolve all other requests waiting for this response
|
||||
const waiting = axios.waiting[id];
|
||||
|
||||
if (waiting) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
waiting.resolve(newCache.data);
|
||||
|
||||
@ -120,4 +120,24 @@ describe('Tests update-cache', () => {
|
||||
const cacheValue = await axios.storage.get(cacheKey);
|
||||
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