Commit 592a9642 authored by ='s avatar =

add Promise support

parent a0534d33
......@@ -137,6 +137,18 @@ myClient.sendNotification(firstNotification, function (err, httpResponse,data) {
});
```
You can also use Promises:
```js
myClient.sendNotification(firstNotification)
.then(function (response) {
console.log(response.data, response.httpResponse.statusCode);
})
.catch(function (err) {
console.log('Something went wrong...', err);
});
```
To send a notification based on filters, use `.setFilters(filters)` method:
``` js
var OneSignal = require('onesignal-node');
......
......@@ -32,7 +32,17 @@ var basicRequest = function (url, apiKey, method, body, callback) {
options.body = body;
options.json = true;
}
request(options, callback);
return new Promise(function (resolve, reject) {
request(options, function (err, httpResponse, data) {
if (err) {
callback && callback(err, httpResponse, data);
reject(err);
} else {
callback && callback(err, httpResponse, data);
resolve({ httpResponse: httpResponse, data: data });
}
});
});
};
/**
......@@ -110,10 +120,10 @@ Client.prototype.sendNotification = function (notification, callback) {
var postBody = notification.postBody;
if (this.apps && this.apps.length > 0) {
postBody.app_ids = this.apps;
basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.userAuthKey, 'POST', postBody, callback);
} else if (this.app) {
postBody.app_id = this.app.appId;
basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback);
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH, this.app.appAuthKey, 'POST', postBody, callback);
} else {
throw 'You must set either an "app" or "apps" on Client';
}
......@@ -130,7 +140,7 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
};
/**
......@@ -143,7 +153,7 @@ Client.prototype.viewNotification = function (notificationId, callback) {
throw 'You must define an "app" object.'
}
var notificationUri = this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId + '?app_id=' + this.app.appId;
basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
......@@ -156,7 +166,7 @@ Client.prototype.viewNotifications = function (query, callback) {
throw 'You must define an "app" object.'
}
var appUri = this.API_URI + constants.NOTIFICATIONS_PATH + '?app_id=' + this.app.appId + '&' + query;
basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
};
......@@ -168,7 +178,7 @@ Client.prototype.viewApps = function (callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback);
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'GET', null, callback);
};
/**
......@@ -180,7 +190,7 @@ Client.prototype.viewApp = function (appId, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback);
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + appId, this.userAuthKey, 'GET', null, callback);
};
/**
......@@ -195,7 +205,7 @@ Client.prototype.createApp = function (body, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
};
/**
......@@ -210,7 +220,7 @@ Client.prototype.updateApp = function (body, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
};
......@@ -224,7 +234,7 @@ Client.prototype.viewDevices = function (query, callback) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '?app_id=' + this.app.appId + '&' + query;
basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
......@@ -237,7 +247,7 @@ Client.prototype.viewDevice = function (deviceId, callback) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
......@@ -253,7 +263,7 @@ Client.prototype.addDevice = function (body, callback) {
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
};
/**
......@@ -266,7 +276,7 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
};
/**
......@@ -282,7 +292,7 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
};
/**
......@@ -295,7 +305,7 @@ Client.prototype.csvExport = function (body, callback) {
throw 'You must define an "app" object.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment