Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions lib/wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,32 @@ const {
} = require('./config');
const fetch = require('isomorphic-fetch');
const log = require('./log');
const Url = require('url');
const HttpsProxyAgent = require('https-proxy-agent');

const learnMore = 'Learn more at https://wit.ai/docs/quickstart';

function getProxyAgent(witURL) {
const url = Url.parse(witURL);
const proxy = url.protocol === "http:" ?
process.env.http_proxy || process.env.HTTP_PROXY :
process.env.https_proxy || process.env.HTTPS_PROXY;
const noProxy = process.env.no_proxy || process.env.NO_PROXY;

const shouldIgnore = noProxy && noProxy.indexOf(url.hostname) > -1;
if (proxy && !shouldIgnore) {
return new HttpsProxyAgent(proxy);
}
if(!proxy) return null;
}

function Wit(opts) {
if (!(this instanceof Wit)) {
return new Wit(opts);
}

const {
accessToken, apiVersion, headers, logger, witURL
accessToken, apiVersion, headers, logger, witURL, proxy
} = this.config = Object.freeze(validate(opts));

this._sessions = {};
Expand All @@ -46,10 +62,11 @@ function Wit(opts) {
return fetch(fullURL, {
method,
headers,
proxy,
})
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
;
.then(response => Promise.all([response.json(), response.status]))
.then(handler)
;
};
}

Expand Down Expand Up @@ -93,6 +110,7 @@ const validate = (opts) => {
'Content-Type': 'application/json',
};
opts.logger = opts.logger || new log.Logger(log.INFO);
opts.proxy = getProxyAgent(opts.witURL);

return opts;
};
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"body-parser": "^1.18.2",
"express": "^4.16.2",
"https-proxy-agent": "^1.0.0",
"isomorphic-fetch": "^2.2.1",
"request": "^2.83.0",
"uuid": "^3.0.0"
Expand Down