Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
O
onesignal-node
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
Cedar Studios
onesignal-node
Commits
5da1d758
Commit
5da1d758
authored
Mar 15, 2018
by
Fellipe Capelli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update client file with code style used in other files and add editorconfig
parent
8402ed4e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
84 additions
and
74 deletions
+84
-74
.editorconfig
.editorconfig
+12
-0
client.js
lib/client.js
+62
-64
constants.js
lib/constants.js
+1
-1
notification.js
lib/notification.js
+9
-9
No files found.
.editorconfig
0 → 100644
View file @
5da1d758
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
lib/client.js
View file @
5da1d758
...
@@ -13,7 +13,7 @@ var ALLOWED_CREDENTIALS = [
...
@@ -13,7 +13,7 @@ var ALLOWED_CREDENTIALS = [
* make a basic request
* make a basic request
* @param url
* @param url
* @param apiKey
* @param apiKey
* @param method : [ GET, POS
t
, PUT ...]
* @param method : [ GET, POS
T
, PUT ...]
* @param body
* @param body
* @param callback (err, httpResponse, body)
* @param callback (err, httpResponse, body)
*/
*/
...
@@ -33,16 +33,16 @@ var basicRequest = function (url, apiKey, method, body, callback) {
...
@@ -33,16 +33,16 @@ var basicRequest = function (url, apiKey, method, body, callback) {
options
.
json
=
true
;
options
.
json
=
true
;
}
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
request
(
options
,
function
(
err
,
httpResponse
,
data
)
{
request
(
options
,
function
(
err
,
httpResponse
,
data
)
{
if
(
err
)
{
if
(
err
)
{
callback
&&
callback
(
err
,
httpResponse
,
data
);
callback
&&
callback
(
err
,
httpResponse
,
data
);
reject
(
err
);
reject
(
err
);
}
else
{
}
else
{
callback
&&
callback
(
err
,
httpResponse
,
data
);
callback
&&
callback
(
err
,
httpResponse
,
data
);
resolve
({
httpResponse
:
httpResponse
,
data
:
data
});
resolve
({
httpResponse
:
httpResponse
,
data
:
data
});
}
}
});
});
});
});
};
};
/**
/**
...
@@ -52,7 +52,7 @@ var basicRequest = function (url, apiKey, method, body, callback) {
...
@@ -52,7 +52,7 @@ var basicRequest = function (url, apiKey, method, body, callback) {
* @returns {boolean}
* @returns {boolean}
*/
*/
var
checkCredential
=
function
(
credentialName
,
credential
)
{
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
(
ALLOWED_CREDENTIALS
[
i
].
name
===
credentialName
)
{
if
(
typeof
credential
!==
ALLOWED_CREDENTIALS
[
i
].
type
)
{
if
(
typeof
credential
!==
ALLOWED_CREDENTIALS
[
i
].
type
)
{
throw
credentialName
+
' must be a '
+
ALLOWED_CREDENTIALS
[
i
].
type
;
throw
credentialName
+
' must be a '
+
ALLOWED_CREDENTIALS
[
i
].
type
;
...
@@ -71,7 +71,7 @@ var checkCredential = function (credentialName, credential) {
...
@@ -71,7 +71,7 @@ var checkCredential = function (credentialName, credential) {
};
};
/**
/**
*
*
* @param credentials { JSON }
* @param credentials { JSON }
* @constructor
* @constructor
*/
*/
...
@@ -89,7 +89,7 @@ var Client = function (credentials) {
...
@@ -89,7 +89,7 @@ var Client = function (credentials) {
/**
/**
*
*
* @param rootUrl { String } default 'https://onesignal.com/api/v1';
* @param rootUrl { String } default 'https://onesignal.com/api/v1';
*/
*/
Client
.
prototype
.
setRootUrl
=
function
(
rootUrl
)
{
Client
.
prototype
.
setRootUrl
=
function
(
rootUrl
)
{
...
@@ -127,7 +127,6 @@ Client.prototype.sendNotification = function (notification, callback) {
...
@@ -127,7 +127,6 @@ Client.prototype.sendNotification = function (notification, callback) {
}
else
{
}
else
{
throw
'You must set either an "app" or "apps" on Client'
;
throw
'You must set either an "app" or "apps" on Client'
;
}
}
};
};
/**
/**
...
@@ -140,11 +139,11 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
...
@@ -140,11 +139,11 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
var
notificationUri
=
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
+
'/'
+
notificationId
+
'?app_id='
+
this
.
app
.
appId
;
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 notificationId { String }
* @param callback
* @param callback
*/
*/
...
@@ -153,11 +152,11 @@ Client.prototype.viewNotification = function (notificationId, callback) {
...
@@ -153,11 +152,11 @@ Client.prototype.viewNotification = function (notificationId, callback) {
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
var
notificationUri
=
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
+
'/'
+
notificationId
+
'?app_id='
+
this
.
app
.
appId
;
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 query { String } ex: limit:100&offset=9
* @param callback
* @param callback
*/
*/
...
@@ -166,23 +165,23 @@ Client.prototype.viewNotifications = function (query, callback) {
...
@@ -166,23 +165,23 @@ Client.prototype.viewNotifications = function (query, callback) {
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
var
appUri
=
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
+
'?app_id='
+
this
.
app
.
appId
+
'&'
+
query
;
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
* @param callback
*/
*/
Client
.
prototype
.
viewApps
=
function
(
callback
)
{
Client
.
prototype
.
viewApps
=
function
(
callback
)
{
if
(
!
this
.
userAuthKey
)
{
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
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 appId { String }
* @param callback
* @param callback
*/
*/
...
@@ -190,11 +189,11 @@ Client.prototype.viewApp = function (appId, callback) {
...
@@ -190,11 +189,11 @@ Client.prototype.viewApp = function (appId, callback) {
if
(
!
this
.
userAuthKey
)
{
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
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 body { JSON }
* @param callback
* @param callback
*/
*/
...
@@ -203,9 +202,9 @@ Client.prototype.createApp = function (body, callback) {
...
@@ -203,9 +202,9 @@ Client.prototype.createApp = function (body, callback) {
throw
'You must specify a name in body'
;
throw
'You must specify a name in body'
;
}
}
if
(
!
this
.
userAuthKey
)
{
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
throw
'You must define "userAuthKey" on Client'
}
}
return
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
);
};
};
/**
/**
...
@@ -217,15 +216,15 @@ Client.prototype.updateApp = function (body, callback) {
...
@@ -217,15 +216,15 @@ Client.prototype.updateApp = function (body, callback) {
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
if
(
!
this
.
userAuthKey
)
{
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
throw
'You must define "userAuthKey" on Client'
}
}
return
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
);
};
};
/**
/**
*
*
* @param query { String } ex: limit=200&offset=10
* @param query { String } ex: limit=200&offset=10
* @param callback
* @param callback
*/
*/
...
@@ -234,36 +233,36 @@ Client.prototype.viewDevices = function (query, callback) {
...
@@ -234,36 +233,36 @@ Client.prototype.viewDevices = function (query, callback) {
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
var
viewUri
=
this
.
API_URI
+
constants
.
DEVICES_PATH
+
'?app_id='
+
this
.
app
.
appId
+
'&'
+
query
;
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 deviceId { String }
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
viewDevice
=
function
(
deviceId
,
callback
)
{
Client
.
prototype
.
viewDevice
=
function
(
deviceId
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
var
viewUri
=
this
.
API_URI
+
constants
.
DEVICES_PATH
+
'/'
+
deviceId
+
'?app_id='
+
this
.
app
.
appId
;
var
viewUri
=
this
.
API_URI
+
constants
.
DEVICES_PATH
+
'/'
+
deviceId
+
'?app_id='
+
this
.
app
.
appId
;
return
basicRequest
(
viewUri
,
this
.
app
.
appAuthKey
,
'GET'
,
null
,
callback
);
return
basicRequest
(
viewUri
,
this
.
app
.
appAuthKey
,
'GET'
,
null
,
callback
);
};
};
/**
/**
*
*
* @param body { JSON }
* @param body { JSON }
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
addDevice
=
function
(
body
,
callback
)
{
Client
.
prototype
.
addDevice
=
function
(
body
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
if
(
!
(
'app_id'
in
body
))
{
if
(
!
(
'app_id'
in
body
))
{
body
.
app_id
=
this
.
app
.
appId
;
body
.
app_id
=
this
.
app
.
appId
;
}
}
return
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
);
};
};
/**
/**
...
@@ -273,10 +272,10 @@ Client.prototype.addDevice = function (body, callback) {
...
@@ -273,10 +272,10 @@ Client.prototype.addDevice = function (body, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
editDevice
=
function
(
deviceId
,
body
,
callback
)
{
Client
.
prototype
.
editDevice
=
function
(
deviceId
,
body
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
return
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
);
};
};
/**
/**
...
@@ -286,13 +285,13 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
...
@@ -286,13 +285,13 @@ Client.prototype.editDevice = function (deviceId, body, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
trackOpen
=
function
(
notificationId
,
body
,
callback
)
{
Client
.
prototype
.
trackOpen
=
function
(
notificationId
,
body
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
if
(
!
(
'app_id'
in
body
))
{
if
(
!
(
'app_id'
in
body
))
{
body
.
app_id
=
this
.
app
.
appId
;
body
.
app_id
=
this
.
app
.
appId
;
}
}
return
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
);
};
};
/**
/**
...
@@ -301,12 +300,11 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
...
@@ -301,12 +300,11 @@ Client.prototype.trackOpen = function (notificationId, body, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
csvExport
=
function
(
body
,
callback
)
{
Client
.
prototype
.
csvExport
=
function
(
body
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
throw
'You must define an "app" object.'
}
}
var
csvUri
=
this
.
API_URI
+
constants
.
DEVICES_PATH
+
'/csv_export'
+
'?app_id='
+
this
.
app
.
appId
;
var
csvUri
=
this
.
API_URI
+
constants
.
DEVICES_PATH
+
'/csv_export'
+
'?app_id='
+
this
.
app
.
appId
;
return
basicRequest
(
csvUri
,
this
.
app
.
appAuthKey
,
'POST'
,
body
,
callback
);
return
basicRequest
(
csvUri
,
this
.
app
.
appAuthKey
,
'POST'
,
body
,
callback
);
};
};
module
.
exports
=
Client
;
module
.
exports
=
Client
;
lib/constants.js
View file @
5da1d758
var
Constants
=
{
var
Constants
=
{
API_ROOT
:
'https://onesignal.com/api/v1'
,
API_ROOT
:
'https://onesignal.com/api/v1'
,
/** PATHS **/
/** PATHS **/
NOTIFICATIONS_PATH
:
'/notifications'
,
NOTIFICATIONS_PATH
:
'/notifications'
,
APPS_PATH
:
'/apps'
,
APPS_PATH
:
'/apps'
,
...
...
lib/notification.js
View file @
5da1d758
...
@@ -12,7 +12,7 @@ var ALLOWED_FIELDS = ['contents', 'included_segments', 'excluded_segments', 'fil
...
@@ -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
* @param initialBody The body must include either one of these: contents, content_available, template_id
* @constructor
* @constructor
*/
*/
...
@@ -48,15 +48,15 @@ Notification.prototype.setParameter = function (name, value) {
...
@@ -48,15 +48,15 @@ Notification.prototype.setParameter = function (name, value) {
};
};
/**
/**
*
*
* @param contents
* @param contents
*/
*/
Notification
.
prototype
.
setContent
=
function
(
contents
)
{
Notification
.
prototype
.
setContent
=
function
(
contents
)
{
this
.
postBody
.
contents
=
contents
;
this
.
postBody
.
contents
=
contents
;
};
};
/**
/**
*
*
* @param included_segments The segment names you want to target
* @param included_segments The segment names you want to target
*/
*/
Notification
.
prototype
.
setIncludedSegments
=
function
(
included_segments
)
{
Notification
.
prototype
.
setIncludedSegments
=
function
(
included_segments
)
{
...
@@ -64,23 +64,23 @@ 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
* @param excluded_segments Segment that will be excluded when sending
*/
*/
Notification
.
prototype
.
setExcludedSegments
=
function
(
excluded_segments
)
{
Notification
.
prototype
.
setExcludedSegments
=
function
(
excluded_segments
)
{
this
.
postBody
.
excluded_segments
=
excluded_segments
;
this
.
postBody
.
excluded_segments
=
excluded_segments
;
};
};
/**
/**
*
*
* @param filters
* @param filters
*/
*/
Notification
.
prototype
.
setFilters
=
function
(
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
* @param include_player_ids Specific players to send your notification to
*/
*/
Notification
.
prototype
.
setTargetDevices
=
function
(
include_player_ids
)
{
Notification
.
prototype
.
setTargetDevices
=
function
(
include_player_ids
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment