mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
38 lines
900 B
TypeScript
38 lines
900 B
TypeScript
import { parse } from '@tusbar/cache-control';
|
|
import type { HeaderInterpreter } from './types';
|
|
|
|
export const defaultHeaderInterpreter: HeaderInterpreter = (headers) => {
|
|
const cacheControl = headers?.['cache-control'];
|
|
|
|
if (!cacheControl) {
|
|
// Checks if Expires header is present
|
|
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Expires
|
|
const expires = headers?.['expires'];
|
|
|
|
if (expires) {
|
|
const milliseconds = Date.parse(expires) - Date.now();
|
|
|
|
if (milliseconds > 0) {
|
|
return milliseconds;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
const { noCache, noStore, mustRevalidate, maxAge } = parse(cacheControl);
|
|
|
|
// Header told that this response should not be cached.
|
|
if (noCache || noStore || mustRevalidate) {
|
|
return false;
|
|
}
|
|
|
|
if (!maxAge) {
|
|
return undefined;
|
|
}
|
|
|
|
return maxAge * 1000;
|
|
};
|