diff --git a/src/axios/types.ts b/src/axios/types.ts index d25df269..d34b99cf 100644 --- a/src/axios/types.ts +++ b/src/axios/types.ts @@ -88,9 +88,11 @@ export type CacheRequestConfig = AxiosRequestConfig & { id?: string | number; /** - * All cache options for the request + * All cache options for the request. + * + * False means ignore everything about cache, for this request. */ - cache?: Partial; + cache?: false | Partial; }; export default interface CacheInstance { diff --git a/src/interceptors/request.ts b/src/interceptors/request.ts index 6298d2a7..bf382197 100644 --- a/src/interceptors/request.ts +++ b/src/interceptors/request.ts @@ -5,6 +5,11 @@ import { CACHED_RESPONSE_STATUS, CACHED_RESPONSE_STATUS_TEXT } from '../util/sta export function applyRequestInterceptor(axios: AxiosCacheInstance): void { axios.interceptors.request.use(async (config) => { + // Ignore caching + if (config.cache === false) { + return config; + } + // Only cache specified methods const allowedMethods = config.cache?.methods || axios.defaults.cache.methods; diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index db64ba78..b3be70b7 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -1,11 +1,13 @@ import { AxiosResponse } from 'axios'; -import { AxiosCacheInstance, CacheRequestConfig } from '../axios/types'; +import { AxiosCacheInstance, CacheProperties, CacheRequestConfig } from '../axios/types'; import { CachedStorageValue } from '../storage/types'; import { checkPredicateObject } from '../util/cache-predicate'; import { updateCache } from '../util/update-cache'; +type CacheConfig = CacheRequestConfig & { cache?: Partial }; + export function applyResponseInterceptor(axios: AxiosCacheInstance): void { - const testCachePredicate = (response: AxiosResponse, config: CacheRequestConfig): boolean => { + const testCachePredicate = (response: AxiosResponse, config: CacheConfig): boolean => { const cachePredicate = config.cache?.cachePredicate || axios.defaults.cache.cachePredicate; return ( @@ -15,6 +17,11 @@ export function applyResponseInterceptor(axios: AxiosCacheInstance): void { }; axios.interceptors.response.use(async (response) => { + // Ignore caching + if (response.config.cache === false) { + return response; + } + const key = axios.generateKey(response.config); const cache = await axios.storage.get(key); @@ -24,7 +31,7 @@ export function applyResponseInterceptor(axios: AxiosCacheInstance): void { } // Config told that this response should be cached. - if (!testCachePredicate(response, response.config)) { + if (!testCachePredicate(response, response.config as CacheConfig)) { // Update the cache to empty to prevent infinite loading state await axios.storage.remove(key); return response;