Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upMore 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! |
|
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:
|

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Is your feature request related to a problem? Please describe.
Users may want to set different options for native
httpmodule, 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.setNoDelayfor http adapteroptions.localAddressoptions.checkServerIdentityoptions.insecureHTTPParseroptions.dnsDescribe 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.