From 766b016866ec2e031740409cc6760e198b92efac Mon Sep 17 00:00:00 2001 From: arthurfiorette Date: Wed, 5 Jan 2022 18:14:18 -0300 Subject: [PATCH] docs: fixed `cache.update` example --- README.md | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e955e96..6ce6ec1 100644 --- a/README.md +++ b/README.md @@ -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; } } }