Skip to content

Adding support for beforeRedirect config option#3852

Merged
jasonsaayman merged 11 commits intoaxios:masterfrom
zoran995:redirect-options
Mar 7, 2022
Merged

Adding support for beforeRedirect config option#3852
jasonsaayman merged 11 commits intoaxios:masterfrom
zoran995:redirect-options

Conversation

@zoran995
Copy link
Copy Markdown
Contributor

@zoran995 zoran995 commented Jun 24, 2021

Axios for long time supports following of the redirects using the follow-redirects package, but it didn't implemented support for beforeRedirect function which is useful to easily control the redirects.
This adds support for providing the beforeRedirect function in options.
Also updated the maxRedirects default value in readme, it is 21 for around 5 years.

// Handle errors
req.on('error', function handleRequestError(err) {
if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') return;
if (req.aborted && err.code !== 'ERR_FR_TOO_MANY_REDIRECTS') reject(err);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this request will never complete.

Copy link
Copy Markdown
Contributor Author

@zoran995 zoran995 Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to expand more on this change, I haven't used enhanceError since this should be an error thrown from client application so we return it as is.
It's a bit strange that no one before run into the issue of stuck request

@jasonsaayman
Copy link
Copy Markdown
Member

@zoran995 can you please fix the merge conflicts?

@zoran995
Copy link
Copy Markdown
Contributor Author

@jasonsaayman merge conflicts are resolved

@zoran995
Copy link
Copy Markdown
Contributor Author

zoran995 commented Jan 20, 2022

@jasonsaayman it seems that something weird was going on on my end, typescript never complained about those issues and all tests passed successfully (it started to complain yesterday). Those are totally valid ts errors, which should have been thrown from the start
Probably the best solution is to type beforeRedirect parameters as any and add details in the doc about correct types

beforeRedirect?: (options: any, responseDetails: {headers: any}) => void; 

any other ideas?

@zoran995
Copy link
Copy Markdown
Contributor Author

zoran995 commented Feb 7, 2022

@jasonsaayman I have resolved type issues, both are now typed as records(objects)

@jasonsaayman jasonsaayman merged commit 412d3bd into axios:master Mar 7, 2022
@zoran995 zoran995 deleted the redirect-options branch March 15, 2022 15:38
options.maxRedirects = config.maxRedirects;
}
if (config.beforeRedirect) {
options.beforeRedirect = config.beforeRedirect;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this break proxy support when redirection occurs?

The setProxy function, which is called before hitting this line, sets options.beforeRedirect to re-apply proxy configuration after a redirect. This line here happily stamps over it.

Either there should be something to harmonize the two features (such as registering a function that invokes both the original config beforeRedirect if needed, and the proxy re-configuration next) or the documentation should state that the config.beforeRedirect option is incompatible with proxy support (😿) and can introduce proxy redirect issues.

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

Successfully merging this pull request may close these issues.

4 participants