Skip to content

Commit

Permalink
Implement promise support (#154)
Browse files Browse the repository at this point in the history
* added promise support to base object

* updated sendingDomains for promise support

* updated inboundDomains for promise support

* updated messageEvents for promise support

* updated recipientLists for promise support

* updated relayWebhooks for promise support

* updated subaccounts for promise support

* updated suppressionList for promise support

* updated templates for promise support

* updated transmissions for promise support

* updated webhooks for promise support

* Replaced bluebird with native promise support for PR #154

* removed dependency for bluebird

* replaced bluebird on spec tests
  • Loading branch information
aydrian authored Aug 22, 2016
1 parent 0564e25 commit 23fdf26
Show file tree
Hide file tree
Showing 25 changed files with 273 additions and 249 deletions.
1 change: 1 addition & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"node" : true,
"es3" : false,
"esnext" : true,
"strict" : true,
"curly" : true,
"eqeqeq" : true,
Expand Down
31 changes: 31 additions & 0 deletions lib/Promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

/**
* asCallback method mimics bluebird and allows
* promise object to handle an optional nodeback
*
* @param cb {Function}
* @return Promise
*
* @example
* function eitherOr(options, callback) {
* return promiseThingy(options).asCallback(callback);
* }
*/
Promise.prototype.asCallback = function(cb) {
if (typeof cb !== 'function') {
cb = noop;
}

return this.then((result) => {
cb(null, result);
return this;
}).catch((err) => {
cb(err);
return this;
});
};

function noop() {}

module.exports = Promise;
21 changes: 10 additions & 11 deletions lib/inboundDomains.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
'use strict';

var api = 'inbound-domains';
var api = 'inbound-domains'
/* global -Promise */
, Promise = require('./Promise');

module.exports = function(client) {
var inboundDomains = {
all: function(callback) {
var options = {
uri: api
};
client.get(options, callback);
return client.get(options).asCallback(callback);
},
find: function(domain, callback) {
if(typeof domain === 'function') {
Expand All @@ -17,14 +19,13 @@ module.exports = function(client) {
}

if(!domain) {
callback(new Error('domain is required'));
return;
return Promise.reject(new Error('domain is required')).asCallback(callback);
}

var options = {
uri: api + '/' + domain
};
client.get(options, callback);
return client.get(options).asCallback(callback);
},
create: function(domain, callback) {
if(typeof domain === 'function') {
Expand All @@ -33,8 +34,7 @@ module.exports = function(client) {
}

if(!domain) {
callback(new Error('domain is required'));
return;
return Promise.reject(new Error('domain is required')).asCallback(callback);
}

var options = {
Expand All @@ -43,7 +43,7 @@ module.exports = function(client) {
domain: domain
}
};
client.post(options, callback);
return client.post(options, callback).asCallback(callback);
},
delete: function(domain, callback) {
if (typeof domain === 'function') {
Expand All @@ -52,14 +52,13 @@ module.exports = function(client) {
}

if (!domain) {
callback(new Error('domain is required'));
return;
return Promise.reject(new Error('domain is required')).asCallback(callback);
}

var options = {
uri: api + '/' + domain
};
client.delete(options, callback);
return client.delete(options).asCallback(callback);
}
};

Expand Down
2 changes: 1 addition & 1 deletion lib/messageEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = function (client) {
uri: api
, qs: parameters
};
client.get(options, callback);
return client.get(options).asCallback(callback);
}
};
};
Expand Down
47 changes: 22 additions & 25 deletions lib/recipientLists.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var api = 'recipient-lists'
/* global -Promise */
, Promise = require('./Promise')
, toApiFormat = require('./toApiFormat');

module.exports = function(client) {
Expand All @@ -9,14 +11,13 @@ module.exports = function(client) {
var reqOpts = {
uri: api
};
client.get(reqOpts, callback);
return client.get(reqOpts).asCallback(callback);
},
find: function(options, callback) {
options = options || {};

if(!options.id) {
callback(new Error('id is required'));
return;
return Promise.reject(new Error('id is required')).asCallback(callback);
}

var reqOpts = {
Expand All @@ -28,14 +29,13 @@ module.exports = function(client) {
reqOpts.qs.show_recipients = options.show_recipients;
}

client.get(reqOpts, callback);
return client.get(reqOpts).asCallback(callback);
},
create: function(options, callback) {
options = options || {};

if(!options.recipients) {
callback(new Error('recipients list is required'));
return;
return Promise.reject(new Error('recipients list is required')).asCallback(callback);
}

var reqOpts = {
Expand All @@ -50,14 +50,13 @@ module.exports = function(client) {

reqOpts.json = toApiFormat(options);

client.post(reqOpts, callback);
return client.post(reqOpts).asCallback(callback);
},
update: function(options, callback) {
options = options || {};

if(!options.id) {
callback(new Error('recipients list id is required'));
return;
return Promise.reject(new Error('recipients list id is required')).asCallback(callback);
}

var reqOpts = {
Expand All @@ -72,25 +71,23 @@ module.exports = function(client) {

reqOpts.json = toApiFormat(options);

client.put(reqOpts, callback);
}
};
return client.put(reqOpts, callback).asCallback(callback);
},
delete: function(id, callback) {
if (typeof id === 'function') {
callback = id;
id = null;
}

recipientLists['delete'] = function(id, callback) {
if (typeof id === 'function') {
callback = id;
id = null;
}
if (!id) {
return Promise.reject(new Error('id is required')).asCallback(callback);
}

if (!id) {
callback(new Error('id is required'));
return;
var reqOpts = {
uri: api + '/' + id
};
return client.delete(reqOpts).asCallback(callback);
}

var reqOpts = {
uri: api + '/' + id
};
client['delete'](reqOpts, callback);
};

return recipientLists;
Expand Down
35 changes: 15 additions & 20 deletions lib/relayWebhooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

var api = 'relay-webhooks';
var api = 'relay-webhooks'
/* global -Promise */
, Promise = require('./Promise');

var toApiFormat = function(input) {
var model = {
Expand All @@ -23,7 +25,7 @@ module.exports = function(client) {
var reqOpts = {
uri: api
};
client.get(reqOpts, callback);
return client.get(reqOpts).asCallback(callback);
},
find: function(relayWebhookId, callback) {
if(typeof relayWebhookId === 'function') {
Expand All @@ -32,14 +34,13 @@ module.exports = function(client) {
}

if(!relayWebhookId) {
callback(new Error('relayWebhookId is required'));
return;
return Promise.reject(new Error('relayWebhookId is required')).asCallback(callback);
}

var options = {
uri: api + '/' + relayWebhookId
};
client.get(options, callback);
return client.get(options).asCallback(callback);
},
create: function(options, callback) {
if(typeof options === 'function') {
Expand All @@ -48,25 +49,22 @@ module.exports = function(client) {
}

if(!options) {
callback(new Error('options are required'));
return;
return Promise.reject(new Error('options are required')).asCallback(callback);
}

if(!options.target) {
callback(new Error('target is required in options'));
return;
return Promise.reject(new Error('target is required in options')).asCallback(callback);
}

if(!options.domain) {
callback(new Error('domain is required in options'));
return;
return Promise.reject(new Error('domain is required in options')).asCallback(callback);
}

var reqOpts = {
uri: api
, json: toApiFormat(options)
};
client.post(reqOpts, callback);
return client.post(reqOpts).asCallback(callback);
},
update: function(options, callback) {
if(typeof options === 'function') {
Expand All @@ -75,21 +73,19 @@ module.exports = function(client) {
}

if(!options) {
callback(new Error('options are required'));
return;
return Promise.reject(new Error('options are required')).asCallback(callback);
}

if(!options.relayWebhookId) {
callback(new Error('relayWebhookId is required in options'));
return;
return Promise.reject(new Error('relayWebhookId is required in options')).asCallback(callback);
}

var relayWebhookId = options.relayWebhookId;
var reqOpts = {
uri: api + '/' + relayWebhookId
, json: toApiFormat(options)
};
client.put(reqOpts, callback);
return client.put(reqOpts).asCallback(callback);
},
delete: function(relayWebhookId, callback) {
if (typeof relayWebhookId === 'function') {
Expand All @@ -98,15 +94,14 @@ module.exports = function(client) {
}

if (!relayWebhookId) {
callback(new Error('relayWebhookId is required'));
return;
return Promise.reject(new Error('relayWebhookId is required')).asCallback(callback);
}

var options = {
uri: api + '/' + relayWebhookId
};

client.delete(options, callback);
client.delete(options, callback).asCallback(callback);
}
};

Expand Down
Loading

0 comments on commit 23fdf26

Please sign in to comment.