mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: better cache predicate function
This commit is contained in:
parent
3b40abd8de
commit
a77cde7e22
@ -2,7 +2,7 @@ import type { AxiosResponse } from 'axios';
|
|||||||
import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios';
|
import type { AxiosCacheInstance, CacheAxiosResponse } from '../cache/axios';
|
||||||
import type { CacheProperties } from '../cache/cache';
|
import type { CacheProperties } from '../cache/cache';
|
||||||
import type { CachedResponse, CachedStorageValue } from '../storage/types';
|
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 { Header } from '../util/headers';
|
||||||
import { updateCache } from '../util/update-cache';
|
import { updateCache } from '../util/update-cache';
|
||||||
import type { AxiosInterceptor } from './types';
|
import type { AxiosInterceptor } from './types';
|
||||||
@ -50,7 +50,7 @@ export class CacheResponseInterceptor<R, D>
|
|||||||
if (
|
if (
|
||||||
// For 'loading' values (post stale), this check was already run in the past.
|
// For 'loading' values (post stale), this check was already run in the past.
|
||||||
!cache.data &&
|
!cache.data &&
|
||||||
!CacheResponseInterceptor.testCachePredicate(response, cacheConfig)
|
!shouldCacheResponse(response, cacheConfig)
|
||||||
) {
|
) {
|
||||||
await this.rejectResponse(response.id);
|
await this.rejectResponse(response.id);
|
||||||
return response;
|
return response;
|
||||||
@ -131,19 +131,6 @@ export class CacheResponseInterceptor<R, D>
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
static readonly testCachePredicate = <R>(
|
|
||||||
response: AxiosResponse<R>,
|
|
||||||
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
|
* Creates the new date to the cache by the provided response. Also handles possible 304
|
||||||
* Not Modified by updating response properties.
|
* Not Modified by updating response properties.
|
||||||
|
|||||||
@ -1,7 +1,20 @@
|
|||||||
import type { AxiosResponse } from 'axios';
|
import type { AxiosResponse } from 'axios';
|
||||||
|
import type { CacheProperties } from '..';
|
||||||
import type { CachePredicateObject } from './types';
|
import type { CachePredicateObject } from './types';
|
||||||
|
|
||||||
export function checkPredicateObject<R>(
|
/** Returns true if the response should be cached */
|
||||||
|
export function shouldCacheResponse<R>(
|
||||||
|
response: AxiosResponse<R>,
|
||||||
|
{ cachePredicate }: CacheProperties
|
||||||
|
) {
|
||||||
|
if (typeof cachePredicate === 'function') {
|
||||||
|
return cachePredicate(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
return isCachePredicateValid(response, cachePredicate);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isCachePredicateValid<R>(
|
||||||
response: AxiosResponse<R>,
|
response: AxiosResponse<R>,
|
||||||
{ statusCheck, containsHeaders, responseMatch }: CachePredicateObject
|
{ statusCheck, containsHeaders, responseMatch }: CachePredicateObject
|
||||||
): boolean {
|
): boolean {
|
||||||
|
|||||||
@ -1,12 +1,12 @@
|
|||||||
import { checkPredicateObject } from '../../src/util/cache-predicate';
|
import { isCachePredicateValid } from '../../src/util/cache-predicate';
|
||||||
import { createResponse } from '../utils';
|
import { createResponse } from '../utils';
|
||||||
|
|
||||||
describe('tests cache predicate object', () => {
|
describe('tests cache predicate object', () => {
|
||||||
it('tests statusCheck with tuples', () => {
|
it('tests statusCheck with tuples', () => {
|
||||||
const response = createResponse({ status: 764 });
|
const response = createResponse({ status: 764 });
|
||||||
|
|
||||||
const falsyTest = checkPredicateObject(response, { statusCheck: [200, 299] });
|
const falsyTest = isCachePredicateValid(response, { statusCheck: [200, 299] });
|
||||||
const truthyTest = checkPredicateObject(response, { statusCheck: [760, 769] });
|
const truthyTest = isCachePredicateValid(response, { statusCheck: [760, 769] });
|
||||||
|
|
||||||
expect(falsyTest).toBeFalsy();
|
expect(falsyTest).toBeFalsy();
|
||||||
expect(truthyTest).toBeTruthy();
|
expect(truthyTest).toBeTruthy();
|
||||||
@ -15,11 +15,11 @@ describe('tests cache predicate object', () => {
|
|||||||
it('tests statusCheck with a predicate', () => {
|
it('tests statusCheck with a predicate', () => {
|
||||||
const response = createResponse({ status: 764 });
|
const response = createResponse({ status: 764 });
|
||||||
|
|
||||||
const falsyTest = checkPredicateObject(response, {
|
const falsyTest = isCachePredicateValid(response, {
|
||||||
statusCheck: (status) => status >= 200 && status <= 299
|
statusCheck: (status) => status >= 200 && status <= 299
|
||||||
});
|
});
|
||||||
|
|
||||||
const truthyTest = checkPredicateObject(response, {
|
const truthyTest = isCachePredicateValid(response, {
|
||||||
statusCheck: (status) => status >= 760 && status <= 769
|
statusCheck: (status) => status >= 760 && status <= 769
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -32,11 +32,11 @@ describe('tests cache predicate object', () => {
|
|||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasContentTypeLowercase = checkPredicateObject(response, {
|
const hasContentTypeLowercase = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'content-type': true }
|
containsHeaders: { 'content-type': true }
|
||||||
});
|
});
|
||||||
|
|
||||||
const hasContentType = checkPredicateObject(response, {
|
const hasContentType = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'Content-Type': true }
|
containsHeaders: { 'Content-Type': true }
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -49,15 +49,15 @@ describe('tests cache predicate object', () => {
|
|||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const headerExists = checkPredicateObject(response, {
|
const headerExists = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'content-type': 'application/json' }
|
containsHeaders: { 'content-type': 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const isXmlContent = checkPredicateObject(response, {
|
const isXmlContent = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'Content-Type': 'application/xml' }
|
containsHeaders: { 'Content-Type': 'application/xml' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const isJsonContent = checkPredicateObject(response, {
|
const isJsonContent = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'Content-Type': 'application/json' }
|
containsHeaders: { 'Content-Type': 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -71,15 +71,15 @@ describe('tests cache predicate object', () => {
|
|||||||
headers: { 'Content-Type': 'application/json' }
|
headers: { 'Content-Type': 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const headerExists = checkPredicateObject(response, {
|
const headerExists = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'content-type': (header) => header == 'application/json' }
|
containsHeaders: { 'content-type': (header) => header == 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const isXmlContent = checkPredicateObject(response, {
|
const isXmlContent = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'Content-Type': (header) => header == 'application/xml' }
|
containsHeaders: { 'Content-Type': (header) => header == 'application/xml' }
|
||||||
});
|
});
|
||||||
|
|
||||||
const isJsonContent = checkPredicateObject(response, {
|
const isJsonContent = isCachePredicateValid(response, {
|
||||||
containsHeaders: { 'Content-Type': (header) => header == 'application/json' }
|
containsHeaders: { 'Content-Type': (header) => header == 'application/json' }
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -93,11 +93,11 @@ describe('tests cache predicate object', () => {
|
|||||||
data: { a: true, b: 1 }
|
data: { a: true, b: 1 }
|
||||||
});
|
});
|
||||||
|
|
||||||
const testStrict = checkPredicateObject(response, {
|
const testStrict = isCachePredicateValid(response, {
|
||||||
responseMatch: (data: any) => data && data.a === true && data.b === 1
|
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)
|
responseMatch: (data: any) => data && (data.a !== true || data.b !== 1)
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user