axios-cache-interceptor/src/util/cache-predicate.ts
2021-09-21 12:01:47 -03:00

56 lines
1.2 KiB
TypeScript

import { AxiosResponse } from 'axios';
import { CachePredicateObject } from './types';
export function checkPredicateObject(
response: AxiosResponse,
{ statusCheck, containsHeaders: containsHeader, responseMatch }: CachePredicateObject
): boolean {
if (statusCheck) {
if (typeof statusCheck === 'function') {
if (!statusCheck(response.status)) {
return false;
}
} else {
const [start, end] = statusCheck;
if (
response.status < start || //
response.status > end
) {
return false;
}
}
}
if (containsHeader) {
for (const [headerName, value] of Object.entries(containsHeader)) {
const header = response.headers[headerName];
// At any case, if the header is not found, the predicate fails.
if (!header) {
return false;
}
switch (typeof value) {
case 'string':
if (header != value) {
return false;
}
break;
case 'function':
if (!value(header)) {
return false;
}
break;
}
}
}
if (responseMatch) {
if (!responseMatch(response.data)) {
return false;
}
}
return true;
}