Commit 5da1d758 authored by Fellipe Capelli's avatar Fellipe Capelli

Update client file with code style used in other files and add editorconfig

parent 8402ed4e
root = true
[*.js]
indent_style = space
end_of_line = lf
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
......@@ -13,7 +13,7 @@ var ALLOWED_CREDENTIALS = [
* make a basic request
* @param url
* @param apiKey
* @param method : [ GET, POSt, PUT ...]
* @param method : [ GET, POST, PUT ...]
* @param body
* @param callback (err, httpResponse, body)
*/
......@@ -33,16 +33,16 @@ var basicRequest = function (url, apiKey, method, body, callback) {
options.json = true;
}
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 });
}
});
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 });
}
});
});
};
/**
......@@ -52,7 +52,7 @@ var basicRequest = function (url, apiKey, method, body, callback) {
* @returns {boolean}
*/
var checkCredential = function (credentialName, credential) {
for (var i = 0; i < ALLOWED_CREDENTIALS.length; i++) {
for (var i = 0, credentialLen = ALLOWED_CREDENTIALS.length; i < credentialLen; i++) {
if (ALLOWED_CREDENTIALS[i].name === credentialName) {
if (typeof credential !== ALLOWED_CREDENTIALS[i].type) {
throw credentialName + ' must be a ' + ALLOWED_CREDENTIALS[i].type;
......@@ -71,7 +71,7 @@ var checkCredential = function (credentialName, credential) {
};
/**
*
*
* @param credentials { JSON }
* @constructor
*/
......@@ -89,7 +89,7 @@ var Client = function (credentials) {
/**
*
*
* @param rootUrl { String } default 'https://onesignal.com/api/v1';
*/
Client.prototype.setRootUrl = function (rootUrl) {
......@@ -127,7 +127,6 @@ Client.prototype.sendNotification = function (notification, callback) {
} else {
throw 'You must set either an "app" or "apps" on Client';
}
};
/**
......@@ -140,11 +139,11 @@ 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;
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
return basicRequest(notificationUri, this.app.appAuthKey, 'DELETE', null, callback);
};
/**
*
*
* @param notificationId { String }
* @param callback
*/
......@@ -153,11 +152,11 @@ 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;
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(notificationUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param query { String } ex: limit:100&offset=9
* @param callback
*/
......@@ -166,23 +165,23 @@ 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;
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(appUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param callback
*/
Client.prototype.viewApps = function (callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return 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);
};
/**
*
*
* @param appId { String }
* @param callback
*/
......@@ -190,11 +189,11 @@ Client.prototype.viewApp = function (appId, callback) {
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return 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);
};
/**
*
*
* @param body { JSON }
* @param callback
*/
......@@ -203,9 +202,9 @@ Client.prototype.createApp = function (body, callback) {
throw 'You must specify a name in body';
}
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH, this.userAuthKey, 'POST', body, callback);
};
/**
......@@ -217,15 +216,15 @@ Client.prototype.updateApp = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
if (!this.userAuthKey) {
throw 'You must define "userAuthKey" on Client'
}
return basicRequest(this.API_URI + constants.APPS_PATH + '/' + this.app.appId, this.userAuthKey, 'PUT', body, callback);
};
/**
*
*
* @param query { String } ex: limit=200&offset=10
* @param callback
*/
......@@ -234,36 +233,36 @@ 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;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param deviceId { String }
* @param callback
*/
Client.prototype.viewDevice = function (deviceId, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var viewUri = this.API_URI + constants.DEVICES_PATH + '/' + deviceId + '?app_id=' + this.app.appId;
return basicRequest(viewUri, this.app.appAuthKey, 'GET', null, callback);
};
/**
*
*
* @param body { JSON }
* @param callback
*/
Client.prototype.addDevice = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.DEVICES_PATH, this.app.appAuthKey, 'POST', body, callback);
};
/**
......@@ -273,10 +272,10 @@ Client.prototype.addDevice = function (body, callback) {
* @param callback
*/
Client.prototype.editDevice = function (deviceId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
return basicRequest(this.API_URI + constants.DEVICES_PATH + '/' + deviceId, this.app.appAuthKey, 'PUT', body, callback);
};
/**
......@@ -286,13 +285,13 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
* @param callback
*/
Client.prototype.trackOpen = function (notificationId, body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
if (!('app_id' in body)) {
body.app_id = this.app.appId;
}
return basicRequest(this.API_URI + constants.NOTIFICATIONS_PATH + '/' + notificationId, this.app.appAuthKey, 'PUT', body, callback);
};
/**
......@@ -301,12 +300,11 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
* @param callback
*/
Client.prototype.csvExport = function (body, callback) {
if (!this.app) {
throw 'You must define an "app" object.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
if (!this.app) {
throw 'You must define an "app" object.'
}
var csvUri = this.API_URI + constants.DEVICES_PATH + '/csv_export' + '?app_id=' + this.app.appId;
return basicRequest(csvUri, this.app.appAuthKey, 'POST', body, callback);
};
module.exports = Client;
var Constants = {
API_ROOT: 'https://onesignal.com/api/v1',
/** PATHS **/
NOTIFICATIONS_PATH: '/notifications',
APPS_PATH: '/apps',
......
......@@ -12,7 +12,7 @@ var ALLOWED_FIELDS = ['contents', 'included_segments', 'excluded_segments', 'fil
/**
*
*
* @param initialBody The body must include either one of these: contents, content_available, template_id
* @constructor
*/
......@@ -48,15 +48,15 @@ Notification.prototype.setParameter = function (name, value) {
};
/**
*
*
* @param contents
*/
Notification.prototype.setContent = function (contents) {
this.postBody.contents = contents;
this.postBody.contents = contents;
};
/**
*
*
* @param included_segments The segment names you want to target
*/
Notification.prototype.setIncludedSegments = function (included_segments) {
......@@ -64,23 +64,23 @@ Notification.prototype.setIncludedSegments = function (included_segments) {
};
/**
*
*
* @param excluded_segments Segment that will be excluded when sending
*/
Notification.prototype.setExcludedSegments = function (excluded_segments) {
this.postBody.excluded_segments = excluded_segments;
this.postBody.excluded_segments = excluded_segments;
};
/**
*
*
* @param filters
*/
Notification.prototype.setFilters = function (filters) {
this.postBody.filters = filters;
this.postBody.filters = filters;
};
/**
*
*
* @param include_player_ids Specific players to send your notification to
*/
Notification.prototype.setTargetDevices = function (include_player_ids) {
......
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