Skip to content

Commit

Permalink
refactor: better cache predicate function
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Dec 11, 2021
1 parent 3b40abd commit a77cde7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
17 changes: 2 additions & 15 deletions src/interceptors/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -50,7 +50,7 @@ export class CacheResponseInterceptor<R, D>
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;
Expand Down Expand Up @@ -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
* Not Modified by updating response properties.
Expand Down
15 changes: 14 additions & 1 deletion src/util/cache-predicate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import type { AxiosResponse } from 'axios';
import type { CacheProperties } from '..';
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>,
{ statusCheck, containsHeaders, responseMatch }: CachePredicateObject
): boolean {
Expand Down
30 changes: 15 additions & 15 deletions test/util/cache-predicate.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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
});

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

Expand All @@ -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' }
});

Expand All @@ -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' }
});

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

Expand Down

0 comments on commit a77cde7

Please sign in to comment.