fetch-intercept
fetch-intercept copied to clipboard
Request interceptor containing an async call
Hi, Is it possible to perform asynchronous calls in a "request" interceptor? I have an interceptor that gets the access token from the localforage library but all methods are asynchronous. If so could you provide a code example please? Thanks
You might have realized this already but it already works this way:
this.unregisterFetch = fetchIntercept.register({
request: async (url, config = {}) => {
const fetchOptions = {
...config,
};
try {
const token = await this.Authenticator.getIdToken();
if (!fetchOptions.headers) {
fetchOptions.headers = new Headers();
}
fetchOptions.headers.append('Authorization', `Bearer ${token}`);
} catch (error) {
// Should we re-login or inspect error here?
}
return [url, fetchOptions];
},
});
If you're using something like adal/angular-adal note that my getIdToken is just a wrapper around their ridiculous synchronous/callback call that promisifies it.
You might have realized this already but it already works this way:
this.unregisterFetch = fetchIntercept.register({ request: async (url, config = {}) => { const fetchOptions = { ...config, }; try { const token = await this.Authenticator.getIdToken(); if (!fetchOptions.headers) { fetchOptions.headers = new Headers(); } fetchOptions.headers.append('Authorization', `Bearer ${token}`); } catch (error) { // Should we re-login or inspect error here? } return [url, fetchOptions]; }, });If you're using something like adal/angular-adal note that my getIdToken is just a wrapper around their ridiculous synchronous/callback call that promisifies it.
This way does not take effect
Hi, Is it possible to perform asynchronous calls in a "request" interceptor? I have an interceptor that gets the access token from the localforage library but all methods are asynchronous. If so could you provide a code example please? Thanks
I have the same issue