Skip to content
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

Adds promises support to the client. #160

Merged
1 commit merged into from
Mar 5, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v2.0.7 ()

* Adds promises to the Slack clients. If no callback is passed to an API call, a promise will be created and returned instead.

### v2.0.6 (2016-03-01)

* Fixes a crash introduce in `2.0.5` if you try and instantiate a `WebClient` without passing in any options
Expand Down
36 changes: 31 additions & 5 deletions lib/clients/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
*/

var EventEmitter = require('eventemitter3');
var Promise = require('bluebird');
var async = require('async');
var bind = require('lodash').bind;
var inherits = require('inherits');
var partial = require('lodash').partial;
var retry = require('retry');
var urlJoin = require('url-join');

var SlackAPIError = require('./errors').SlackAPIError;
var getLogger = require('../helpers').getLogger;
var helpers = require('./helpers');
var requestsTransport = require('./transports/request').requestTransport;
Expand Down Expand Up @@ -136,20 +138,44 @@ BaseAPIClient.prototype._callTransport = function _callTransport(task, queueCb)
* @param {function=} optCb The callback to run on completion.
*/
BaseAPIClient.prototype.makeAPICall = function makeAPICall(endpoint, optData, optCb) {
var promise;
var args;
var _this = this;
var apiCallArgs = helpers.getAPICallArgs(this._token, optData, optCb);

var args = {
args = {
url: urlJoin(this.slackAPIUrl, endpoint),
data: apiCallArgs.data,
headers: {
'User-Agent': this.userAgent
}
};

this.requestQueue.push({
args: args,
cb: apiCallArgs.cb
});
if (!apiCallArgs.cb) {
promise = new Promise(function makeAPICallPromiseResolver(resolve, reject) {
_this.requestQueue.push({
args: args,
cb: function makeAPICallPromiseResolverInner(err, res) {
if (err) {
reject(err);
} else {
if (!res.ok) {
reject(new SlackAPIError(res.error));
} else {
resolve(res);
}
}
}
});
});
} else {
this.requestQueue.push({
args: args,
cb: apiCallArgs.cb
});
}

return promise;
};


Expand Down
14 changes: 14 additions & 0 deletions lib/clients/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

var inherits = require('inherits');


var SlackAPIError = function SlackAPIError(error) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = error;
};

inherits(SlackAPIError, Error);


module.exports.SlackAPIError = SlackAPIError;
6 changes: 1 addition & 5 deletions lib/clients/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var isFunction = require('lodash').isFunction;
var isUndefined = require('lodash').isUndefined;
var isString = require('lodash').isString;
var forEach = require('lodash').forEach;
var noop = require('lodash').noop;


/**
Expand Down Expand Up @@ -48,19 +47,16 @@ var getAPICallArgs = function getAPICallArgs(token, optData, optCb) {
var cb;

if (arguments.length === 1) {
// Pass in a no-op function here to avoid adding more conditionals in the _callTransport fn
cb = noop;
data = getData({}, token);
} else if (arguments.length === 2) {
if (isFunction(arguments[1])) {
cb = arguments[1];
data = getData({}, token);
} else {
cb = noop;
data = getData(optData, token);
}
} else if (arguments.length === 3) {
cb = optCb || noop;
cb = optCb;
data = getData(optData, token);
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"async": "^1.5.0",
"bluebird": "^3.3.3",
"eventemitter3": "^1.1.1",
"https-proxy-agent": "^1.0.0",
"inherits": "^2.0.1",
Expand Down
7 changes: 2 additions & 5 deletions test/clients/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var expect = require('chai').expect;
var lodash = require('lodash');

var helpers = require('../../lib/clients/helpers');

Expand Down Expand Up @@ -65,21 +64,19 @@ describe('Client Helpers', function () {
});

describe('#getAPICallArgs()', function () {
it('returns an empty object and noop fn when called with no data or cb', function () {
it('returns an object with the token when called with no data or cb', function () {
var callArgs = helpers.getAPICallArgs('test');
expect(callArgs.data).to.deep.equal({
token: 'test'
});
expect(callArgs.cb).to.deep.equal(lodash.noop);
});

it('returns the supplied object and noop fn when called with an object and no cb', function () {
it('returns the supplied object when called with a data object and no cb', function () {
var callArgs = helpers.getAPICallArgs('test', { test: 1 });
expect(callArgs.data).to.deep.equal({
test: 1,
token: 'test'
});
expect(callArgs.cb).to.deep.equal(lodash.noop);
});

it('returns the supplied cb and empty object when called with a cb and no object', function () {
Expand Down