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 { setupCache } = require('axios-cache-interceptor');
|
||||
|
||||
const api = setupCache(Axios.create(), {
|
||||
baseUrl: 'https://jsonplaceholder.typicode.com/',
|
||||
cache: {
|
||||
interpretHeader: true, // Cache-Control, Expires, etc.
|
||||
ttl: 5 * 60 * 1000, // 5 seconds
|
||||
etag: true, // Enables ETag caching
|
||||
ifModifiedSince: true // Enables If-Modified-Since caching
|
||||
const api = setupCache(
|
||||
Axios.create({ baseURL: 'https://jsonplaceholder.typicode.com/' }),
|
||||
{
|
||||
ttl: 5 * 1000 // 5 seconds
|
||||
}
|
||||
});
|
||||
);
|
||||
|
||||
// Every time an api call reaches here, it will
|
||||
// make another internal request and forward the response.
|
||||
app.get('/', (req, res) => {
|
||||
api.get('https://jsonplaceholder.typicode.com/users').then(
|
||||
({ data, cached }) => {
|
||||
res.json({ cached, data });
|
||||
api.get('/users').then(
|
||||
({ data, cached, id }) => {
|
||||
res.json({
|
||||
cached,
|
||||
id: {
|
||||
value: id,
|
||||
deleteUrl: `/cache/${id}/delete`,
|
||||
getUrl: `/cache/${id}/get`
|
||||
},
|
||||
data
|
||||
});
|
||||
},
|
||||
(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);
|
||||
```
|
||||
|
||||
@ -62,9 +62,9 @@ export function buildStorage({ set, find, remove }: BuildStorage): AxiosStorage
|
||||
Header.XAxiosCacheLastModified in value.data.headers)
|
||||
) {
|
||||
const stale: StaleStorageValue = {
|
||||
data: value.data,
|
||||
state: 'stale',
|
||||
createdAt: value.createdAt
|
||||
createdAt: value.createdAt,
|
||||
data: value.data
|
||||
};
|
||||
await set(key, stale);
|
||||
return stale;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user