forked from arthurfiorette/axios-cache-interceptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.ts
37 lines (28 loc) · 900 Bytes
/
interpreter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
};