diff --git a/.auto-changelog b/.auto-changelog index 4560f7e..9ec7ef5 100644 --- a/.auto-changelog +++ b/.auto-changelog @@ -1,6 +1,6 @@ -{ - "unreleased": true, - "commitLimit": false, - "sortCommits": "subject", - "template": "keepachangelog" -} +{ + "unreleased": true, + "commitLimit": false, + "sortCommits": "subject", + "template": "keepachangelog" +} diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 2e05da2..2ea16a7 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -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); diff --git a/test/util/update-cache.test.ts b/test/util/update-cache.test.ts index 0e85817..d28ee9d 100644 --- a/test/util/update-cache.test.ts +++ b/test/util/update-cache.test.ts @@ -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'); + }); });