TooTallNate / superagent-proxy

`Request#proxy(uri)` superagent extension
154 stars 46 forks source link

Supports for conditional proxy? #18

Closed reyou closed 8 years ago

reyou commented 8 years ago

I have following code;

// if config is enabled return http://127.0.0.1:8888 // else return null var proxy = process.env.http_proxy || config.getProxyAddress();

request .get(url) .proxy(proxy) .set('Authorization', 'Bearer ' + authCode)

here if I pass proxy as null or empty string it throws error of: "TypeError: an HTTP(S) proxy server host and protocol must be specified!" but I would like to depend on my config settings.

How can I enable this?

In this case, if proxy is null or empty can we just bypass and do not use proxy?

TooTallNate commented 8 years ago

I would just not call .proxy() if your proxy var is falsey:

// if config is enabled return http://127.0.0.1:8888
// else return null
var proxy = process.env.http_proxy || config.getProxyAddress();

var req = request.get(url)
  .set('Authorization', 'Bearer ' + authCode);

if (proxy) req.proxy(proxy);
reyou commented 8 years ago

Actually this pull request does exactly what I am asking for;

https://github.com/gebrits/superagent-proxy/commit/a2f0a060d616a9452630c45c91bb1fd4dc459ba9

Is it possible to accept it?

For now thanks for your response, I will try that.

hughmp commented 7 years ago

This works for me:

request
  .get(url)
  .proxy(proxy || url)
  .set('Authorization', 'Bearer ' + authCode)

Does it work for others?

hughmp commented 7 years ago

realised this doesn't work as intended when url is using https :-1: