From 866447423ab6ff7523ff61c96ccd779be4e9be68 Mon Sep 17 00:00:00 2001 From: Chase Roossin Date: Sun, 28 Jan 2018 09:20:44 -0800 Subject: [PATCH 1/2] Add functionality to edit device info --- README.md | 14 +++++++++++++- lib/client.js | 27 +++++++++++++++++++++++++++ test/index.js | 39 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4362a31..891129e 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,13 @@ const client = new OneSignalClient([YOUR APP ID], [YOUR REST API KEY]); client.sendNotification('test notification', { included_segments: 'all' }); + +// update device info +client.sendNotification('deviceId12345', { + 'tags': { + 'hasUpdated': 'true' + } +}); ``` ## API @@ -26,7 +33,12 @@ client.sendNotification('test notification', { `sendNotification(message, options)` * `message`_(string/object, required)_ - the content of your message. **Note:** when passing an object, please see the [OneSignal documentation](https://documentation.onesignal.com/docs/notifications-create-notification) for details on the format. -* `options`_(object)_ - OneSignal options. Please see the [OneSignal documentation](https://documentation.onesignal.com/docs/notifications-create-notification). +* `options`_(object)_ - OneSignal options. Please see the [OneSignal documentation](https://documentation.onesignal.com/reference#create-notification). + +`editDeviceInfo(deviceId, options)` +* `deviceId`_(string, required)_ - the device's OneSignal ID. + +* `options`_(object, required)_ - OneSignal options. Please see the [OneSignal documentation](https://documentation.onesignal.com/reference#edit-device). As you can see, this SDK does not implement all of the methods available through the OneSignal REST API. If there are other methods you require, please open an issue or feel free to create a PR (with tests!). diff --git a/lib/client.js b/lib/client.js index b78c8ee..19173cc 100644 --- a/lib/client.js +++ b/lib/client.js @@ -64,4 +64,31 @@ export default class Client { throw new Error(err.response.error.text); } } + + /** + * Updates a particular device. + * @param {string|object} deviceId - the device's OneSignal ID + * @param {object} options - a hash of options to pass to the API + * @return {object} the response + */ + async editDeviceInfo(deviceId, options) { + + // Perform some basic validation + Joi.assert(deviceId, Joi.alternatives().try(Joi.string(), Joi.object()).required(), new Error('`deviceId` is required')); + Joi.assert(options, Joi.object().required(), new Error('`options` is required')); + Joi.assert(options, Joi.object().keys().min(1), new Error('`options` must have at least one key/value')); + + // Make the request + try { + + return await Request + .put(`${API_URL}/players/${deviceId}`) + .set('Authorization', `Basic ${this.restApiKey}`) + .send(options); + } + catch(err) { + + throw new Error(err.response.error.text); + } + } }; diff --git a/test/index.js b/test/index.js index e5b85bd..c147201 100644 --- a/test/index.js +++ b/test/index.js @@ -23,6 +23,8 @@ test.before(t => { // Tests +// Send Notification Tests + test('throws an error if no app id is specified', t => { t.throws(() => { @@ -79,7 +81,6 @@ test('throws an error if no message is provided', async t => { t.throws(response, '`message` is required'); }); - test('handles API error', async t => { const client = new Client(process.env.APP_ID, process.env.REST_API_KEY); @@ -91,3 +92,39 @@ test('handles API error', async t => { t.throws(response); }); + +// Edit Device Info Tests + +test('throws an error if no options is provided for edit device', async t => { + + const deviceId = '12345'; + const options = undefined; + + const response = client.editDeviceInfo(deviceId, options); + + t.throws(response, '`options` is required'); +}); + +test('throws an error if options does not contain at least one key/value pair for edit device', async t => { + + const deviceId = '12345'; + const options = {}; + + const response = client.editDeviceInfo(deviceId, options); + + t.throws(response, '`options` must have at least one key/value'); +}); + +test('throws an error if no deviceId is provided for edit device', async t => { + + const deviceId = ''; + const options = { + "tags": { + "testing": "true" + } + }; + + const response = client.editDeviceInfo(deviceId, options); + + t.throws(response, '`deviceId` is required'); +}); From abf309aef57e171990e4541b442fbcc2569b28ff Mon Sep 17 00:00:00 2001 From: Chase Roossin Date: Sun, 28 Jan 2018 09:26:50 -0800 Subject: [PATCH 2/2] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 891129e..87b2c78 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ client.sendNotification('test notification', { }); // update device info -client.sendNotification('deviceId12345', { +client.editDeviceInfo('deviceId12345', { 'tags': { 'hasUpdated': 'true' }