Access Operation inside isTokenValidOrUndefined and handleFetch/handleError
See original GitHub issueWhen using this library we ran into issue of not being able to access Redux state or dispatch actions inside the TokenRefreshLink
because Operation
was not accessible inside of it. We had to tweek it a bit so that these functions would pass Operation
object.
I believe this use case is quite common because if we store accessToken
inside Redux and after refreshing it want to change the state, there’s no possible way to that without having access to context.
This requires minimal changes to the existing code though helps a lot, so it would be nice to have this functionality as part of this library.
export type HandleResponse = (operation: Operation, accessTokenField: string) => void;
export type HandleError = (operation: Operation, err: Error) => void;
export type IsTokenValidOrUndefined = (operation: Operation, ...args: any[]) => boolean;
if (!this.fetching) {
this.fetching = true;
this.fetchAccessToken()
.then(this.handleResponse(operation, this.accessTokenField))
.then(body => {
const token = this.extractToken(body);
if (!token) {
throw new Error('[Token Refresh Link]: Unable to retrieve new access token');
}
return token;
})
.then(payload => this.handleFetch(operation, payload))
.catch(error => this.handleError(operation, error))
.finally(() => {
this.fetching = false;
this.queue.consumeQueue();
});
}
Issue Analytics
- State:
- Created 2 years ago
- Comments:12 (3 by maintainers)
Top Results From Across the Web
React API Handle Error in Fetch Call : TypeError-Cannot read ...
On submitting wrong city name is am getting an error as: TypeError: Cannot read property 'temp' of undefined 22 | .then ...
Read more >Handling Failed HTTP Responses With fetch() - TJ VanToll
The good is news is fetch provides a simple ok flag that indicates whether an HTTP response's status code is in the successful...
Read more >How to Correctly Fetch Data in React (fix cannot read property ...
In this video, I give you the four-step process to correctly and safely fetch data asynchronously in React without running into errors.
Read more >Full React Tutorial #19 - Handling Fetch Errors - YouTube
Hey gang, in this React tutorial we'll handle any errors from the fetch request we made earlier & output any erors in the...
Read more >Error handling while using native fetch API in JavaScript
We will see, how to handle fetch API errors using promises and async await syntax in JavaScript.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Update* my configuration of Redux was incorrect - accessing the store directly instead of through
operation
does work as intended. Tonight, I will still put together a code sandbox to demo what I have.Original message:
Right, but I can confirm that this setup isn’t working.
Seems the dispatch isn’t getting called correctly from here.
I might be able to put together an example on code sandbox to demo
I think I’ve already updated README. thanks)