diff --git a/src/interceptors/response.ts b/src/interceptors/response.ts index 3f03a18..37a0926 100644 --- a/src/interceptors/response.ts +++ b/src/interceptors/response.ts @@ -2,7 +2,7 @@ import type { AxiosResponse } from 'axios'; import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios'; import type { CacheProperties } from '../cache/cache'; import type { CachedResponse, CachedStorageValue } from '../storage/types'; -import { checkPredicateObject } from '../util/cache-predicate'; +import { shouldCacheResponse } from '../util/cache-predicate'; import { Header } from '../util/headers'; import { updateCache } from '../util/update-cache'; import type { AxiosInterceptor } from './types'; @@ -50,7 +50,7 @@ export class CacheResponseInterceptor if ( // For 'loading' values (post stale), this check was already run in the past. !cache.data && - !CacheResponseInterceptor.testCachePredicate(response, cacheConfig) + !shouldCacheResponse(response, cacheConfig) ) { await this.rejectResponse(response.id); return response; @@ -131,19 +131,6 @@ export class CacheResponseInterceptor }; }; - static readonly testCachePredicate = ( - response: AxiosResponse, - cache: CacheProperties - ): boolean => { - const cachePredicate = cache.cachePredicate; - - return ( - (typeof cachePredicate === 'function' && cachePredicate(response)) || - (typeof cachePredicate === 'object' && - checkPredicateObject(response, cachePredicate)) - ); - }; - /** * Creates the new date to the cache by the provided response. Also handles possible 304 * Not Modified by updating response properties. diff --git a/src/util/cache-predicate.ts b/src/util/cache-predicate.ts index 577adce..05dc209 100644 --- a/src/util/cache-predicate.ts +++ b/src/util/cache-predicate.ts @@ -1,7 +1,20 @@ import type { AxiosResponse } from 'axios'; +import type { CacheProperties } from '..'; import type { CachePredicateObject } from './types'; -export function checkPredicateObject( +/** Returns true if the response should be cached */ +export function shouldCacheResponse( + response: AxiosResponse, + { cachePredicate }: CacheProperties +) { + if (typeof cachePredicate === 'function') { + return cachePredicate(response); + } + + return isCachePredicateValid(response, cachePredicate); +} + +export function isCachePredicateValid( response: AxiosResponse, { statusCheck, containsHeaders, responseMatch }: CachePredicateObject ): boolean { diff --git a/test/util/cache-predicate.test.ts b/test/util/cache-predicate.test.ts index 4a403e4..1b807e3 100644 --- a/test/util/cache-predicate.test.ts +++ b/test/util/cache-predicate.test.ts @@ -1,12 +1,12 @@ -import { checkPredicateObject } from '../../src/util/cache-predicate'; +import { isCachePredicateValid } from '../../src/util/cache-predicate'; import { createResponse } from '../utils'; describe('tests cache predicate object', () => { it('tests statusCheck with tuples', () => { const response = createResponse({ status: 764 }); - const falsyTest = checkPredicateObject(response, { statusCheck: [200, 299] }); - const truthyTest = checkPredicateObject(response, { statusCheck: [760, 769] }); + const falsyTest = isCachePredicateValid(response, { statusCheck: [200, 299] }); + const truthyTest = isCachePredicateValid(response, { statusCheck: [760, 769] }); expect(falsyTest).toBeFalsy(); expect(truthyTest).toBeTruthy(); @@ -15,11 +15,11 @@ describe('tests cache predicate object', () => { it('tests statusCheck with a predicate', () => { const response = createResponse({ status: 764 }); - const falsyTest = checkPredicateObject(response, { + const falsyTest = isCachePredicateValid(response, { statusCheck: (status) => status >= 200 && status <= 299 }); - const truthyTest = checkPredicateObject(response, { + const truthyTest = isCachePredicateValid(response, { statusCheck: (status) => status >= 760 && status <= 769 }); @@ -32,11 +32,11 @@ describe('tests cache predicate object', () => { headers: { 'Content-Type': 'application/json' } }); - const hasContentTypeLowercase = checkPredicateObject(response, { + const hasContentTypeLowercase = isCachePredicateValid(response, { containsHeaders: { 'content-type': true } }); - const hasContentType = checkPredicateObject(response, { + const hasContentType = isCachePredicateValid(response, { containsHeaders: { 'Content-Type': true } }); @@ -49,15 +49,15 @@ describe('tests cache predicate object', () => { headers: { 'Content-Type': 'application/json' } }); - const headerExists = checkPredicateObject(response, { + const headerExists = isCachePredicateValid(response, { containsHeaders: { 'content-type': 'application/json' } }); - const isXmlContent = checkPredicateObject(response, { + const isXmlContent = isCachePredicateValid(response, { containsHeaders: { 'Content-Type': 'application/xml' } }); - const isJsonContent = checkPredicateObject(response, { + const isJsonContent = isCachePredicateValid(response, { containsHeaders: { 'Content-Type': 'application/json' } }); @@ -71,15 +71,15 @@ describe('tests cache predicate object', () => { headers: { 'Content-Type': 'application/json' } }); - const headerExists = checkPredicateObject(response, { + const headerExists = isCachePredicateValid(response, { containsHeaders: { 'content-type': (header) => header == 'application/json' } }); - const isXmlContent = checkPredicateObject(response, { + const isXmlContent = isCachePredicateValid(response, { containsHeaders: { 'Content-Type': (header) => header == 'application/xml' } }); - const isJsonContent = checkPredicateObject(response, { + const isJsonContent = isCachePredicateValid(response, { containsHeaders: { 'Content-Type': (header) => header == 'application/json' } }); @@ -93,11 +93,11 @@ describe('tests cache predicate object', () => { data: { a: true, b: 1 } }); - const testStrict = checkPredicateObject(response, { + const testStrict = isCachePredicateValid(response, { responseMatch: (data: any) => data && data.a === true && data.b === 1 }); - const testError = checkPredicateObject(response, { + const testError = isCachePredicateValid(response, { responseMatch: (data: any) => data && (data.a !== true || data.b !== 1) });