Skip to content

Commit

Permalink
Replace request with node-fetch
Browse files Browse the repository at this point in the history
Moves to async first. Callbacks can still be used by utilising
`util.callbackify()`.
  • Loading branch information
alexwhitman committed Feb 2, 2022
1 parent 0f18e80 commit 6a83ef9
Show file tree
Hide file tree
Showing 9 changed files with 2,599 additions and 626 deletions.
41 changes: 12 additions & 29 deletions lib/internal/chats.js
Expand Up @@ -4,92 +4,75 @@ import PushBullet from '../pushbullet.js';
* Get a list of current chats.
*
* @param {Object} options Optional options object.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.chats = function chats(options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = {
active: true
};
}

PushBullet.prototype.chats = async function chats(options) {
options = options ? options : {};

if (options.active === undefined) {
options.active = true;
}

return this.getList(PushBullet.CHATS_END_POINT, options, callback);
return this.getList(PushBullet.CHATS_END_POINT, options);
};

/**
* Create a new chat.
*
* @param {String} channelTag Email of the person to create the chat with.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.createChat = function createChat(email, callback) {
PushBullet.prototype.createChat = async function createChat(email) {
var options = {
json: {
email: email
}
};

return this.makeRequest('post', PushBullet.CHATS_END_POINT, options, callback);
return this.makeRequest('post', PushBullet.CHATS_END_POINT, options);
};

/**
* Mute a chat.
*
* @param {String} chatIden The iden of the chat to mute.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.muteChat = function muteChat(chatIden, callback) {
return this.updateChat(chatIden, { muted: true }, callback);
PushBullet.prototype.muteChat = async function muteChat(chatIden) {
return this.updateChat(chatIden, { muted: true });
};

/**
* Unmute chat.
*
* @param {String} chatIden The iden of the chat to unmute.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.unmuteChat = function unmuteChat(chatIden, callback) {
return this.updateChat(chatIden, { muted: false }, callback);
PushBullet.prototype.unmuteChat = async function unmuteChat(chatIden) {
return this.updateChat(chatIden, { muted: false });
};

/**
* Update a chat.
*
* @param {String} chatIden The iden of the chat to ubsubscribe from.
* @param {Object} updates Updates to make to chat.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.updateChat = function updateChat(chatIden, updates, callback) {
if (!callback) {
callback = updates;
}

PushBullet.prototype.updateChat = async function updateChat(chatIden, updates) {
var options = {
json: updates
};

return this.makeRequest('post', PushBullet.CHATS_END_POINT + '/' + chatIden, options, callback);
return this.makeRequest('post', PushBullet.CHATS_END_POINT + '/' + chatIden, options);
};

/**
* Delete a chat.
*
* @param {String} chatIden The iden of the chat to delete.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.deleteChat = function deleteChat(chatIden, callback) {
return this.makeRequest('delete', PushBullet.CHATS_END_POINT + '/' + chatIden, null, callback);
PushBullet.prototype.deleteChat = async function deleteChat(chatIden) {
return this.makeRequest('delete', PushBullet.CHATS_END_POINT + '/' + chatIden, null);
};
27 changes: 8 additions & 19 deletions lib/internal/devices.js
Expand Up @@ -11,63 +11,52 @@ import PushBullet from '../pushbullet.js';
* - `limit` is used to limit the number of objects in the reponse.
*
* @param {Object} options Optional options object.
* @param {Function} callback Callback for when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.devices = function devices(options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = {
active: true
};
}

PushBullet.prototype.devices = async function devices(options) {
options = options ? options : {};

if (options.active === undefined) {
options.active = true;
}

return this.getList(PushBullet.DEVICES_END_POINT, options, callback);
return this.getList(PushBullet.DEVICES_END_POINT, options);
};

/**
* Create a new device.
*
* @param {Object} deviceOptions Object of device options.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.createDevice = function createDevice(deviceOptions, callback) {
PushBullet.prototype.createDevice = async function createDevice(deviceOptions) {
var options = {
json: deviceOptions
};

return this.makeRequest('post', PushBullet.DEVICES_END_POINT, options, callback);
return this.makeRequest('post', PushBullet.DEVICES_END_POINT, options);
};

/**
* Update new device.
*
* @param {Object} deviceOptions Object of device options.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.updateDevice = function updateDevice(deviceIden, deviceOptions, callback) {
PushBullet.prototype.updateDevice = async function updateDevice(deviceIden, deviceOptions) {
var options = {
json: deviceOptions
};

return this.makeRequest('post', PushBullet.DEVICES_END_POINT + '/' + deviceIden, options, callback);
return this.makeRequest('post', PushBullet.DEVICES_END_POINT + '/' + deviceIden, options);
};

/**
* Delete a device.
*
* @param {String} deviceIden Device IDEN of the device to delete.
* @param {Function} callback Called when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.deleteDevice = function deleteDevice(deviceIden, callback) {
return this.makeRequest('delete', PushBullet.DEVICES_END_POINT + '/' + deviceIden, {}, callback);
PushBullet.prototype.deleteDevice = async function deleteDevice(deviceIden) {
return this.makeRequest('delete', PushBullet.DEVICES_END_POINT + '/' + deviceIden, {});
};
20 changes: 8 additions & 12 deletions lib/internal/ephemerals.js
Expand Up @@ -9,16 +9,15 @@ import PushBullet from '../pushbullet.js';
* See https://docs.pushbullet.com/#send-sms for details.
*
* @param {Object} smsOptions SMS options.
* @param {Function} callback Callback for when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.sendSMS = function sendSMS(smsOptions, callback) {
PushBullet.prototype.sendSMS = async function sendSMS(smsOptions) {
var options = clone(smsOptions);

options.package_name = 'com.pushbullet.android';
options.type = 'messaging_extension_reply';

return this.sendEphemeral(options, callback);
return this.sendEphemeral(options);
};

/**
Expand All @@ -28,15 +27,14 @@ PushBullet.prototype.sendSMS = function sendSMS(smsOptions, callback) {
* See https://docs.pushbullet.com/#universal-copypaste for details.
*
* @param {Object} clipOptions Clipboard options.
* @param {Function} callback Callback for when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.sendClipboard = function sendClipboard(clipOptions, callback) {
PushBullet.prototype.sendClipboard = async function sendClipboard(clipOptions) {
var options = clone(clipOptions);

options.type = 'clip';

return this.sendEphemeral(options, callback);
return this.sendEphemeral(options);
};

/**
Expand All @@ -46,25 +44,23 @@ PushBullet.prototype.sendClipboard = function sendClipboard(clipOptions, callbac
* See https://docs.pushbullet.com/#dismissal-ephemeral for details.
*
* @param {Object} ephemeralOptions Ephemeral dismissal options.
* @param {Function} callback Callback for when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.dismissEphemeral = function dismissEphemeral(ephemeralOptions, callback) {
PushBullet.prototype.dismissEphemeral = async function dismissEphemeral(ephemeralOptions) {
var options = clone(ephemeralOptions);

options.type = 'dismissal';

return this.sendEphemeral(options, callback);
return this.sendEphemeral(options);
};

/**
* Send an ephemeral.
*
* @param {Object} ephemeralOptions Ephemeral options.
* @param {Function} callback Callback for when the request is complete.
* @returns {Promise}
*/
PushBullet.prototype.sendEphemeral = function sendEphemeral(ephemeralOptions, callback) {
PushBullet.prototype.sendEphemeral = async function sendEphemeral(ephemeralOptions) {
if (this.encryption) {
var encryptedOptions = this.encryption.encrypt(JSON.stringify(ephemeralOptions));
ephemeralOptions = {
Expand All @@ -80,5 +76,5 @@ PushBullet.prototype.sendEphemeral = function sendEphemeral(ephemeralOptions, ca
}
};

return this.makeRequest('post', PushBullet.EPHEMERALS_END_POINT, options, callback);
return this.makeRequest('post', PushBullet.EPHEMERALS_END_POINT, options);
};

0 comments on commit 6a83ef9

Please sign in to comment.