mirror of
https://github.com/arthurfiorette/axios-cache-interceptor.git
synced 2025-12-08 17:36:16 +00:00
docs: updated rukit example
This commit is contained in:
parent
1e8ca383ac
commit
0aed72f309
@ -71,22 +71,27 @@ const app = express();
|
|||||||
const Axios = require('axios');
|
const Axios = require('axios');
|
||||||
const { setupCache } = require('axios-cache-interceptor');
|
const { setupCache } = require('axios-cache-interceptor');
|
||||||
|
|
||||||
const api = setupCache(Axios.create(), {
|
const api = setupCache(
|
||||||
baseUrl: 'https://jsonplaceholder.typicode.com/',
|
Axios.create({ baseURL: 'https://jsonplaceholder.typicode.com/' }),
|
||||||
cache: {
|
{
|
||||||
interpretHeader: true, // Cache-Control, Expires, etc.
|
ttl: 5 * 1000 // 5 seconds
|
||||||
ttl: 5 * 60 * 1000, // 5 seconds
|
|
||||||
etag: true, // Enables ETag caching
|
|
||||||
ifModifiedSince: true // Enables If-Modified-Since caching
|
|
||||||
}
|
}
|
||||||
});
|
);
|
||||||
|
|
||||||
// Every time an api call reaches here, it will
|
// Every time an api call reaches here, it will
|
||||||
// make another internal request and forward the response.
|
// make another internal request and forward the response.
|
||||||
app.get('/', (req, res) => {
|
app.get('/', (req, res) => {
|
||||||
api.get('https://jsonplaceholder.typicode.com/users').then(
|
api.get('/users').then(
|
||||||
({ data, cached }) => {
|
({ data, cached, id }) => {
|
||||||
res.json({ cached, data });
|
res.json({
|
||||||
|
cached,
|
||||||
|
id: {
|
||||||
|
value: id,
|
||||||
|
deleteUrl: `/cache/${id}/delete`,
|
||||||
|
getUrl: `/cache/${id}/get`
|
||||||
|
},
|
||||||
|
data
|
||||||
|
});
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
res.json({ error });
|
res.json({ error });
|
||||||
@ -94,5 +99,18 @@ app.get('/', (req, res) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.get('/cache/:id/delete', async (req, res) => {
|
||||||
|
await api.storage.remove(req.params.id);
|
||||||
|
res.send({
|
||||||
|
status: 'Deleted!',
|
||||||
|
current: await api.storage.get(req.params.id)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/cache/:id/get', async (req, res) => {
|
||||||
|
const cache = await api.storage.get(req.params.id);
|
||||||
|
res.json(cache);
|
||||||
|
});
|
||||||
|
|
||||||
app.listen(3000);
|
app.listen(3000);
|
||||||
```
|
```
|
||||||
|
|||||||
@ -62,9 +62,9 @@ export function buildStorage({ set, find, remove }: BuildStorage): AxiosStorage
|
|||||||
Header.XAxiosCacheLastModified in value.data.headers)
|
Header.XAxiosCacheLastModified in value.data.headers)
|
||||||
) {
|
) {
|
||||||
const stale: StaleStorageValue = {
|
const stale: StaleStorageValue = {
|
||||||
data: value.data,
|
|
||||||
state: 'stale',
|
state: 'stale',
|
||||||
createdAt: value.createdAt
|
createdAt: value.createdAt,
|
||||||
|
data: value.data
|
||||||
};
|
};
|
||||||
await set(key, stale);
|
await set(key, stale);
|
||||||
return stale;
|
return stale;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user