Skip to content

Commit

Permalink
feat: use cache: false to ignore caching
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Sep 15, 2021
1 parent 0777874 commit 4ad803e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/axios/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions src/interceptors/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 10 additions & 3 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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);

Expand All @@ -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;
Expand Down

0 comments on commit 4ad803e

Please sign in to comment.