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

Refactored adapters loader; #5277

Merged
merged 3 commits into from Nov 22, 2022
Merged

Conversation

DigitalBrainJS
Copy link
Collaborator

Now we can set the adapter by name:

axios.get(url, {
  adapter: 'http' // 'xhr'
});

We can use auto-select from the list (If the adapter is not supported its handle is set to false and Axios tries to set the next one):

axios.get(url, {
  adapter:  ['xhr', 'http', function myCustomAdapter(config){}]
});

So now the default adapter config is adapter: ['xhr', 'http'];

In the future, when we have more than 2 adapters (fetch, http2, other frameworks/OS), we will be able to make separate builds with the selected adapters, and this adapter loader will deal with this by selecting the next available adapter or throwing an error like:

Error: Adapter 'fetch' is not available in the build

Closes #5264;
Closes #5263;

@kranzhoff
Copy link

Does this change apply to defaults as well, like:

axios.defaults.adapter = "http"; ?

@DigitalBrainJS
Copy link
Collaborator Author

Yes, the adapter is resolved right before its call.

Copy link
Member

@jasonsaayman jasonsaayman left a comment

Choose a reason for hiding this comment

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

Awesome!

@jasonsaayman jasonsaayman merged commit d032edd into axios:v1.x Nov 22, 2022
@geekact
Copy link
Contributor

geekact commented Nov 23, 2022

Is that possible to export method getAdapter for people who wants to enhance the adapter?

import axios, { getAdapter } from 'axios';

const defaultAdapters = axios.defaults.adapter;
axios.defaults.adapter = (config) => {
  // do something
  const adapter = getAdapter(defaultAdapters);
  adapter(config);
  // do something
}

depoulo added a commit to depoulo/nock that referenced this pull request Apr 17, 2023
This was added with nock#1711 but [has changed with axios v1](axios/axios#5277)
mastermatt pushed a commit to nock/nock that referenced this pull request Apr 26, 2023
* fix(README): Update axios example

This was added with #1711 but [has changed with axios v1](axios/axios#5277)
@yxw007
Copy link

yxw007 commented Jun 15, 2023

Is that possible to export method getAdapter for people who wants to enhance the adapter?

import axios, { getAdapter } from 'axios';

const defaultAdapters = axios.defaults.adapter;
axios.defaults.adapter = (config) => {
  // do something
  const adapter = getAdapter(defaultAdapters);
  adapter(config);
  // do something
}

I want to do adapter enhancement, why not expose the adapter

bmulholland pushed a commit to gitify-app/gitify that referenced this pull request Oct 2, 2023
bmulholland pushed a commit to gitify-app/gitify that referenced this pull request Oct 3, 2023
bmulholland pushed a commit to gitify-app/gitify that referenced this pull request Oct 4, 2023
* fix(deps): update dependency axios to v1

* Use new adapter setting

See axios/axios#5277

* Fix tests

* Remove unnecessary transform

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Brendan Mulholland <brendan@recital.software>
afonsojramos added a commit to gitify-app/gitify that referenced this pull request Oct 24, 2023
* fix(deps): update dependency axios to v1

* Use new adapter setting

See axios/axios#5277

* Fix tests

* Remove unnecessary transform

* chore(deps): Remove history dependency

* chore: update tests to remove `history`

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Afonso Jorge Ramos <afonsojorgeramos@gmail.com>
MrChuckomo added a commit to MrChuckomo/poddycast that referenced this pull request Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants