Skip to content

Commit

Permalink
v6.1.0 - Bump API version + kaizens
Browse files Browse the repository at this point in the history
Summary:
* bump API version to latest `20210928`
* moved API version from `Accept` header to `v` parameter
* kaizens
* update headers in touched files

Reviewed By: ChrisyShine

Differential Revision: D34868508

fbshipit-source-id: d4f56e7e3f726fde2473d67a01f600d776ec117e
  • Loading branch information
patapizza authored and facebook-github-bot committed Mar 14, 2022
1 parent f320b81 commit 8782944
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 62 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v6.1.0
Bumped API version to `20210928`.
Moved API version from `Accept` header to `v` HTTP parameter.
Kaizens.

## v6.0.1
Removed unused `request` dependency
Updated various dependencies.
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
* Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved.
*/

'use strict';
Expand Down
6 changes: 3 additions & 3 deletions lib/config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
* Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved.
*/

module.exports = {
DEFAULT_API_VERSION: '20200513',
DEFAULT_WIT_URL: 'https://api.wit.ai'
DEFAULT_API_VERSION: '20210928',
DEFAULT_WIT_URL: 'https://api.wit.ai',
};
26 changes: 13 additions & 13 deletions lib/interactive.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
* Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved.
*/

'use strict';

const logger = require('./log.js');
const readline = require('readline');

module.exports = (wit, handleMessage, context) => {
Expand All @@ -18,20 +17,21 @@ module.exports = (wit, handleMessage, context) => {
rl.write(null, {ctrl: true, name: 'e'});
};
prompt();
rl.on('line', (line) => {
rl.on('line', line => {
line = line.trim();
if (!line) {
return prompt();
}
wit.message(line, context)
.then((rsp) => {
if (handleMessage) {
handleMessage(rsp);
} else {
console.log(JSON.stringify(rsp));
}
prompt();
})
.catch(err => console.error(err))
wit
.message(line, context)
.then(rsp => {
if (handleMessage) {
handleMessage(rsp);
} else {
console.log(JSON.stringify(rsp));
}
prompt();
})
.catch(err => console.error(err));
});
};
92 changes: 48 additions & 44 deletions lib/wit.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,54 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
* Copyright (c) Meta Platforms, Inc. and its affiliates. All Rights Reserved.
*/

'use strict';

const {
DEFAULT_API_VERSION,
DEFAULT_WIT_URL
} = require('./config');
const {DEFAULT_API_VERSION, DEFAULT_WIT_URL} = 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, proxy
} = this.config = Object.freeze(validate(opts));
const {accessToken, apiVersion, headers, logger, witURL, proxy} =
(this.config = Object.freeze(validate(opts)));

this._sessions = {};

this.message = (message, context, n, verbose, junk) => {
let qs = 'q=' + encodeURIComponent(message);
this.message = (message, context, n) => {
const params = {
v: apiVersion,
q: message,
};

if (context) {
qs += '&context=' + encodeURIComponent(JSON.stringify(context));
params.context = JSON.stringify(context);
}

if (typeof n === 'number') {
qs += '&n=' + encodeURIComponent(JSON.stringify(n));
}
if (verbose != null) {
qs += '&verbose=' + encodeURIComponent(JSON.stringify(verbose));
}
if (junk != null) {
qs += '&junk=true';
params.n = JSON.stringify(n);
}

const method = 'GET';
const fullURL = witURL + '/message?' + qs;
const handler = makeWitResponseHandler(logger, 'message');
const fullURL =
witURL +
'/message?' +
Object.entries(params)
.map(([key, value]) => key + '=' + encodeURIComponent(value))
.join('&');
logger.debug(method, fullURL);
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(makeWitResponseHandler(logger, 'message'));
};
}

Expand All @@ -86,26 +69,47 @@ const makeWitResponseHandler = (logger, endpoint) => {
return error(json);
}

const err = json.error || status !== 200 && json.body + ' (' + status + ')';
const err =
json.error || (status !== 200 && json.body + ' (' + status + ')');

if (err) {
return error(err);
}

logger.debug('[' + endpoint + '] Response: ' + JSON.stringify(json));
return json;
};
};

const 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;
}
};

const validate = (opts) => {
const validate = opts => {
if (!opts.accessToken) {
throw new Error('Could not find access token, learn more at https://wit.ai/docs');
throw new Error(
'Could not find access token, learn more at https://wit.ai/docs',
);
}

opts.witURL = opts.witURL || DEFAULT_WIT_URL;
opts.apiVersion = opts.apiVersion || DEFAULT_API_VERSION;
opts.headers = opts.headers || {
'Authorization': 'Bearer ' + opts.accessToken,
'Accept': 'application/vnd.wit.' + opts.apiVersion + '+json',
Authorization: 'Bearer ' + opts.accessToken,
'Content-Type': 'application/json',
};
opts.logger = opts.logger || new log.Logger(log.INFO);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-wit",
"version": "6.0.1",
"version": "6.1.0",
"description": "Wit.ai Node.js SDK",
"keywords": [
"wit",
Expand Down

0 comments on commit 8782944

Please sign in to comment.