1 |
|
2 |
|
3 | const { create: createAxios } = require('axios').default;
|
4 | const { setupCache } = require('axios-cache-interceptor');
|
5 | const { log } = console;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | (async () => {
|
13 | const axios = setupCache(
|
14 |
|
15 | createAxios({ baseURL: 'https://registry.npmjs.org/' }),
|
16 |
|
17 | { ttl: 99999, interpretHeader: true }
|
18 | );
|
19 |
|
20 | const fetchedResponse = await axios.get('/axios-cache-interceptor');
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const cachedResponse = await axios.get('/axios-cache-interceptor');
|
28 |
|
29 |
|
30 | log(`First request was ${fetchedResponse.cached ? 'cached' : 'fetched'}`);
|
31 | log(`Second request was ${cachedResponse.cached ? 'cached' : 'fetched'}`);
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | log(`Fetched response Cache-Control: ${fetchedResponse.headers['cache-control']}`);
|
39 | log(`Fetched response Age: ${fetchedResponse.headers['age']}`);
|
40 |
|
41 | const cacheInformation = await axios.storage.get(fetchedResponse.id);
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | log(`Cache TTL info: ${cacheInformation.ttl}`);
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | await axios.storage.remove(fetchedResponse.id);
|
55 |
|
56 | const refetchedResponse = await axios.get('/axios-cache-interceptor', {
|
57 | cache: {
|
58 |
|
59 | interpretHeader: false
|
60 | }
|
61 | });
|
62 |
|
63 | const refetchedInformation = await axios.storage.get(refetchedResponse.id);
|
64 |
|
65 | log(`Third request TTL: ${refetchedInformation.ttl}`);
|
66 | })().catch(console.error);
|