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

Ability to edit device info #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.editDeviceInfo('deviceId12345', {
'tags': {
'hasUpdated': 'true'
}
});
```

## API
Expand All @@ -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!).

Expand Down
27 changes: 27 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
};
39 changes: 38 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ test.before(t => {
// Tests


// Send Notification Tests

test('throws an error if no app id is specified', t => {

t.throws(() => {
Expand Down Expand Up @@ -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);
Expand All @@ -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');
});