Skip to content

Commit

Permalink
Merge pull request #25 from zeyneloz/deprecate-apps
Browse files Browse the repository at this point in the history
Remove `apps` usage from Client.
  • Loading branch information
zeyneloz authored Apr 30, 2019
2 parents 84fa8ba + c7ba58e commit 32d35f8
Show file tree
Hide file tree
Showing 6 changed files with 993 additions and 226 deletions.
26 changes: 4 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,15 @@ var myClient = new OneSignal.Client({
app: { appAuthKey: 'XXXXX', appId: 'XXXXX' }
});
```
You can also create a Client for multiple Apps
```js
// create a Client for a multiple apps
var myClient = new OneSignal.Client({
userAuthKey: 'XXXXXX',
apps: ['id1', 'id2'] // your app ids
});
```
You can always create a Client with no credential and set them later:
```js
// create a Client for a multiple apps
```js
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';

myClient.app = { appAuthKey: 'XXXXX', appId: 'XXXXX' };
// or
myClient.setApp({ appAuthKey: 'XXXXX', appId: 'XXXXX' });

myClient.apps = ['id1', 'id2', 'id3']; // this will override "app"
myClient.setApp({ appAuthKey: 'XXXXX', appId: 'XXXXX' });
```
### Creating new notification object
Expand Down Expand Up @@ -232,14 +221,7 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) {

```
Note that `.sendNotification(notification, callback)` function will send the notification to
the `app` specified during the creation of Client object. If you want to send notification
to multiple apps, you must set `apps` array instead, on Client object:
```js
var myClient = new OneSignal.Client({});
myClient.userAuthKey = 'XXXXXX';
myClient.apps = ['id1', 'id2'];
```
Note that `.sendNotification(notification, callback)` function will send the notification to the `app` specified during the creation of Client object.

### Cancel a push notification
You can cancel a notification simply by calling `.cancel(notificationId, callback)` method
Expand Down
35 changes: 15 additions & 20 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ var constants = require('./constants');

var ALLOWED_CREDENTIALS = [
{ name: 'userAuthKey', type: 'string' },
{ name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] },
{ name: 'apps', type: 'object'}
{ name: 'app', type: 'object', requiredFields: ['appAuthKey', 'appId'] }
];

/**
Expand Down Expand Up @@ -119,15 +118,11 @@ Client.prototype.sendNotification = function (notification, callback) {
throw 'notification parameter must be a typeof Notification object.';
}
var postBody = notification.postBody;
if (this.apps && this.apps.length > 0) {
postBody.app_ids = this.apps;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
}
if (this.app) {
postBody.app_id = this.app.appId;
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback);
}
throw 'You must set either an "app" or "apps" on Client';
throw 'App credentials is not found on client.';
};

/**
Expand All @@ -137,7 +132,7 @@ Client.prototype.sendNotification = function (notification, callback) {
*/
Client.prototype.cancelNotification = function (notificationId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
Expand All @@ -150,7 +145,7 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
*/
Client.prototype.viewNotification = function (notificationId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
Expand All @@ -163,7 +158,7 @@ Client.prototype.viewNotification = function (notificationId, callback) {
*/
Client.prototype.viewNotifications = function (query, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
Expand Down Expand Up @@ -215,7 +210,7 @@ Client.prototype.createApp = function (body, callback) {
*/
Client.prototype.updateApp = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
Expand All @@ -231,7 +226,7 @@ Client.prototype.updateApp = function (body, callback) {
*/
Client.prototype.viewDevices = function (query, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
Expand All @@ -244,7 +239,7 @@ Client.prototype.viewDevices = function (query, callback) {
*/
Client.prototype.viewDevice = function (deviceId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
Expand All @@ -258,7 +253,7 @@ Client.prototype.viewDevice = function (deviceId, callback) {
*/
Client.prototype.addDevice = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
Expand All @@ -274,7 +269,7 @@ Client.prototype.addDevice = function (body, callback) {
*/
Client.prototype.editDevice = function (deviceId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
Expand All @@ -290,7 +285,7 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
*/
Client.prototype.trackOpen = function (notificationId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
Expand All @@ -305,7 +300,7 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
*/
Client.prototype.csvExport = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
Expand All @@ -320,7 +315,7 @@ Client.prototype.csvExport = function (body, callback) {
*/
Client.prototype.newSession = function (playerId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
Expand All @@ -338,7 +333,7 @@ Client.prototype.newSession = function (playerId, body, callback) {
*/
Client.prototype.newPurchase = function (playerId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
Expand All @@ -356,7 +351,7 @@ Client.prototype.newPurchase = function (playerId, body, callback) {
*/
Client.prototype.incrementSessionLength = function (playerId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
throw 'App credentials is not found on client.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
Expand Down
Loading

0 comments on commit 32d35f8

Please sign in to comment.