Skip to content

Commit

Permalink
docs: updated rukit example
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 17, 2022
1 parent 1e8ca38 commit 0aed72f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
40 changes: 29 additions & 11 deletions docs/pages/usage-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,28 +71,46 @@ 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 });
}
);
});

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);
```
4 changes: 2 additions & 2 deletions src/storage/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0aed72f

Please sign in to comment.