Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeSend global support async/await #2

Closed
alvins82 opened this issue Oct 15, 2021 · 3 comments
Closed

beforeSend global support async/await #2

alvins82 opened this issue Oct 15, 2021 · 3 comments

Comments

@alvins82
Copy link

alvins82 commented Oct 15, 2021

I am trying to use auth0 spa plugin and one of its methods to get the access-token to put in authorisation header is asynchronous (getTokenSilently). I would like to use that in beforeSend to setup global Authorisation header but far-fetch doesn't support async beforeSend. Any chance that this can be added?

Axious has something similiar - see below.

apiClient.interceptors.request.use(async (config) => {
    const token = await myApp.$auth.getTokenSilently();
    config.headers['Authorization'] = `Bearer ${token}`;
    return config;
}, (error) => {
    return Promise.reject(error);
});
@D-Marc1
Copy link
Owner

D-Marc1 commented Oct 15, 2021

Hey @alvins82, thanks for the suggestion. I'm always happy to improve FarFetch.

This should be no problem. I can change the code to simply detect if the beforeSend() function returns a promise or is async and then await it if so.

@D-Marc1
Copy link
Owner

D-Marc1 commented Oct 18, 2021

Ok, I just added the ability to create an async beforeSend(). I haven't written tests yet for this, along with a few other features and fixes I made previously, but try installing the new alpha release, 1.2.0-1 with npm i @websitebeaver/far-fetch@next. I will add testing in the near future as well, but this should work right now. Please let me know if it doesn't.

@D-Marc1
Copy link
Owner

D-Marc1 commented Jan 16, 2022

Version 2.0.0 is now released. You can use async with beforeSend(), afterSend(), dynamicOptions and errorHandler().

@D-Marc1 D-Marc1 closed this as completed Jan 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants