mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
refactor: simplified header interpreter
This commit is contained in:
parent
bafe1de98c
commit
e776f01c98
@ -1,53 +1,49 @@
|
||||
import { parse } from 'cache-parser';
|
||||
import { Header } from '../util/headers';
|
||||
import type { HeaderInterpreter, HeadersInterpreter } from './types';
|
||||
import type { HeadersInterpreter } from './types';
|
||||
|
||||
export const defaultHeaderInterpreter: HeadersInterpreter = (headers = {}) => {
|
||||
if (Header.CacheControl in headers) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
return interpretCacheControl(headers[Header.CacheControl]!, headers);
|
||||
}
|
||||
export const defaultHeaderInterpreter: HeadersInterpreter = (headers) => {
|
||||
if (!headers) return undefined;
|
||||
|
||||
if (Header.Expires in headers) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
return interpretExpires(headers[Header.Expires]!, headers);
|
||||
}
|
||||
const cacheControl = headers[Header.CacheControl];
|
||||
|
||||
return undefined;
|
||||
};
|
||||
if (cacheControl) {
|
||||
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(cacheControl);
|
||||
|
||||
const interpretExpires: HeaderInterpreter = (expires) => {
|
||||
const milliseconds = Date.parse(expires) - Date.now();
|
||||
return milliseconds >= 0 ? milliseconds : false;
|
||||
};
|
||||
|
||||
const interpretCacheControl: HeaderInterpreter = (cacheControl, headers) => {
|
||||
const { noCache, noStore, mustRevalidate, maxAge, immutable } = parse(cacheControl);
|
||||
|
||||
// Header told that this response should not be cached.
|
||||
if (noCache || noStore) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (immutable) {
|
||||
// 1 year is sufficient, as Infinity may cause more problems.
|
||||
// It might not be the best way, but a year is better than none.
|
||||
return 1000 * 60 * 60 * 24 * 365;
|
||||
}
|
||||
|
||||
// Already out of date, for cache can be saved, but must be requested again
|
||||
if (mustRevalidate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (maxAge) {
|
||||
const age = headers[Header.Age];
|
||||
|
||||
if (!age) {
|
||||
return maxAge * 1000;
|
||||
// Header told that this response should not be cached.
|
||||
if (noCache || noStore) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (maxAge - Number(age)) * 1000;
|
||||
if (immutable) {
|
||||
// 1 year is sufficient, as Infinity may cause more problems.
|
||||
// It might not be the best way, but a year is better than none.
|
||||
return 1000 * 60 * 60 * 24 * 365;
|
||||
}
|
||||
|
||||
// Already out of date, for cache can be saved, but must be requested again
|
||||
if (mustRevalidate) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (maxAge) {
|
||||
const age = headers[Header.Age];
|
||||
|
||||
if (!age) {
|
||||
return maxAge * 1000;
|
||||
}
|
||||
|
||||
return (maxAge - Number(age)) * 1000;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const expires = headers[Header.Expires];
|
||||
|
||||
if (expires) {
|
||||
const milliseconds = Date.parse(expires) - Date.now();
|
||||
return milliseconds >= 0 ? milliseconds : false;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user