Skip to content

Commit

Permalink
docs: fixed cache.update example
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurfiorette committed Jan 5, 2022
1 parent 8bbddf1 commit 766b016
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -614,23 +614,30 @@ Once the request is resolved, this specifies what other responses should change
cache. Can be used to update the request or delete other caches. It is a simple `Record`
with the request id.

Example:
Here's an example with some basic login:

```js
// Retrieved together with their responses
let otherResponseId;
let userInfoResponseId;
```ts
// Some requests id's
let profileInfoId;
let userInfoId;

axios.get('url', {
axios.post<{ auth: { user: User } }>('login', { username, password }, {
cache: {
update: {
// Evict the otherRequestId cache when this response arrives
[otherResponseId]: 'delete',
// Evicts the profile info cache, because now he is authenticated and the response needs to be re-fetched
[profileInfoId]: 'delete',

// An example that update the "user info response cache" when doing a login.
// Imagine this request is a login one.
[userInfoResponseId]: (cachedValue, thisResponse) => {
return { ...cachedValue, user: thisResponse.user.info };
[userInfoResponseId]: (cachedValue, response) => {
if(cachedValue.state !== 'cached') {
// Only needs to update if the response is cached
return 'ignore';
}

cachedValue.data = response.auth.user;

return cachedValue;
}
}
}
Expand Down

0 comments on commit 766b016

Please sign in to comment.