mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
feat: use cache: false to ignore caching
This commit is contained in:
parent
077787424c
commit
4ad803ec0b
@ -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<CacheProperties>;
|
||||
cache?: false | Partial<CacheProperties>;
|
||||
};
|
||||
|
||||
export default interface CacheInstance {
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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<CacheProperties> };
|
||||
|
||||
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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user