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
More flexible ways to customize adapters #2808
Comments
@chinesedfan, I don't really have a suggestion, but I also need #2233 (or an alternative to set the local address). |
What about having something like beforeSend and transportOptions to avoid creating new and different options for each library? Something like this usage: /**
* @param {import("http").ClientRequest | XMLHttpRequest} req
*/
let beforeSend = (req) => {
if (req instanceof XMLHttpRequest) {
req.setRequestHeader("foo", "bar");
req.onprogress = () => {
// ... I know about axios has onUploadProgress but this is just an
// example of how it can be used
};
} else {
req.setNoDelay(true);
req.setHeader("foo", "bar");
}
};
/**
* @type {import("http").RequestOptions}
*/
let transportOptions = {
localAddress: "127.0.0.1",
};
let instance = axios.create({
// ...
beforeSend,
transportOptions, // Node only
}); beforeSend will be called before these lines: Lines 177 to 178 in 885ada6
or this Lines 275 to 282 in 885ada6
Since transportOptions can only be applied on Node side, it can be destructed in here Lines 83 to 90 in 885ada6
Wdyt @chinesedfan? |
@seahindeniz Yes, you shared most of idea with me. Maybe we can learn something from other libraries first, like |
so right now we can't use localAddress with axios ? o.o |
Hello! 👋 \n\nThis issue is being automatically marked as stale because it has not been updated in a while. Please confirm that the issue is still present and reproducible. If no updates or new comments are received the issue will be closed in a few days. \n\nThanks |
As a workaround for options, waiting for #2808 to be implemented you may do something like :
|
@Poyoman39 Poyoman39 I have tried your sample code to set up the localAddress but it does not work. |
@Poyoman39 Sorry I forgot this feature is not launched yet. |
@carlchan1994 my sample should work with an IP. I tried it on a local server, and it seemed to work well. Be sure your outbound IP address is available => |
@Poyoman39 I tried again and success this time. Thanks a lot. Here is my code:
|
In case of accessing webpage with HTTPS, you may need to use "wrappedHttps" instead of "wrappedHttp"
|
Any solution for IPv6 addresses? The above workaround gives me |
Here is my solution working both with https and IPV6
Use it like that :
|
Closing this issue due to it's age, stale state or due to a loss of momentum surrounding the issue. Please open a new issue should this problem still be relevant. |
Is your feature request related to a problem? Please describe.
Users may want to set different options for native
http
module, or do something with the request object. axios only supports some of them, but doesn't want to add more and more options. For example,req.setNoDelay
for http adapterrequest.open(...., synchronous)
for xhr adapterreq.on('socket', cb)
options.localAddress
options.checkServerIdentity
options.insecureHTTPParser
options.dns
highWaterMark
in Node.js #3845, to setoptions.createConnection
Describe the solution you'd like
Abstract the adapter as more detailed lifecycles, like beforeSend, afterSend. I just have the basic idea, but no detailed solution.
Describe alternatives you've considered
If only consider the options, we can add an extra config called
adapterOptions
. Otherwise, users must implement their own adapters.Additional context
Should consider both xhr and http adapters to form a fixed pattern.
The text was updated successfully, but these errors were encountered: