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
5a282702
Commit
5a282702
authored
Mar 16, 2018
by
Fellipe Capelli
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change kind of indentation
parent
1a8268b7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
83 additions
and
83 deletions
+83
-83
client.js
lib/client.js
+83
-83
No files found.
lib/client.js
View file @
5a282702
...
@@ -4,9 +4,9 @@ var request = require('request');
...
@@ -4,9 +4,9 @@ var request = require('request');
var
constants
=
require
(
'./constants'
);
var
constants
=
require
(
'./constants'
);
var
ALLOWED_CREDENTIALS
=
[
var
ALLOWED_CREDENTIALS
=
[
{
name
:
'userAuthKey'
,
type
:
'string'
},
{
name
:
'userAuthKey'
,
type
:
'string'
},
{
name
:
'app'
,
type
:
'object'
,
requiredFields
:
[
'appAuthKey'
,
'appId'
]
},
{
name
:
'app'
,
type
:
'object'
,
requiredFields
:
[
'appAuthKey'
,
'appId'
]
},
{
name
:
'apps'
,
type
:
'object'
}
{
name
:
'apps'
,
type
:
'object'
}
];
];
/**
/**
...
@@ -18,22 +18,22 @@ var ALLOWED_CREDENTIALS = [
...
@@ -18,22 +18,22 @@ var ALLOWED_CREDENTIALS = [
* @param callback (err, httpResponse, body)
* @param callback (err, httpResponse, body)
*/
*/
var
basicRequest
=
function
(
url
,
apiKey
,
method
,
body
,
callback
)
{
var
basicRequest
=
function
(
url
,
apiKey
,
method
,
body
,
callback
)
{
var
options
=
{
var
options
=
{
url
:
url
,
url
:
url
,
method
:
method
method
:
method
};
};
if
(
apiKey
)
{
if
(
apiKey
)
{
options
.
headers
=
{
options
.
headers
=
{
'Content-Type'
:
'application/json; charset=utf-8'
,
'Content-Type'
:
'application/json; charset=utf-8'
,
'Authorization'
:
'Basic '
+
apiKey
'Authorization'
:
'Basic '
+
apiKey
}
}
}
}
if
(
body
)
{
if
(
body
)
{
options
.
body
=
body
;
options
.
body
=
body
;
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
);
...
@@ -53,22 +53,22 @@ var basicRequest = function (url, apiKey, method, body, callback) {
...
@@ -53,22 +53,22 @@ 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
,
credentialLen
=
ALLOWED_CREDENTIALS
.
length
;
i
<
credentialLen
;
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
;
}
}
if
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
)
{
if
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
)
{
for
(
var
j
=
0
;
j
<
ALLOWED_CREDENTIALS
[
i
].
requiredFields
.
length
;
j
++
)
{
for
(
var
j
=
0
;
j
<
ALLOWED_CREDENTIALS
[
i
].
requiredFields
.
length
;
j
++
)
{
if
(
!
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
in
credential
))
{
if
(
!
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
in
credential
))
{
throw
credentialName
+
' must contain '
+
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
throw
credentialName
+
' must contain '
+
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
}
}
}
}
}
}
return
true
;
return
true
;
}
}
}
}
return
false
;
return
false
;
};
};
/**
/**
...
@@ -77,15 +77,15 @@ var checkCredential = function (credentialName, credential) {
...
@@ -77,15 +77,15 @@ var checkCredential = function (credentialName, credential) {
* @constructor
* @constructor
*/
*/
var
Client
=
function
(
credentials
)
{
var
Client
=
function
(
credentials
)
{
if
(
typeof
credentials
!==
'object'
)
{
if
(
typeof
credentials
!==
'object'
)
{
throw
'credentials parameter must be a JSON object'
throw
'credentials parameter must be a JSON object'
}
}
this
.
API_URI
=
constants
.
API_ROOT
;
this
.
API_URI
=
constants
.
API_ROOT
;
for
(
var
key
in
credentials
)
{
for
(
var
key
in
credentials
)
{
if
(
credentials
.
hasOwnProperty
(
key
)
&&
checkCredential
(
key
,
credentials
[
key
]))
{
if
(
credentials
.
hasOwnProperty
(
key
)
&&
checkCredential
(
key
,
credentials
[
key
]))
{
this
[
key
]
=
credentials
[
key
];
this
[
key
]
=
credentials
[
key
];
}
}
}
}
};
};
...
@@ -94,10 +94,10 @@ var Client = function (credentials) {
...
@@ -94,10 +94,10 @@ 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
)
{
if
(
!
rootUrl
)
{
if
(
!
rootUrl
)
{
throw
'You must set a valid rootUsrl.'
throw
'You must set a valid rootUsrl.'
}
}
this
.
API_URI
=
rootUrl
;
this
.
API_URI
=
rootUrl
;
};
};
/**
/**
...
@@ -105,8 +105,8 @@ Client.prototype.setRootUrl = function (rootUrl) {
...
@@ -105,8 +105,8 @@ Client.prototype.setRootUrl = function (rootUrl) {
* @param app { JSON } {}
* @param app { JSON } {}
*/
*/
Client
.
prototype
.
setApp
=
function
(
app
)
{
Client
.
prototype
.
setApp
=
function
(
app
)
{
checkCredential
(
'app'
,
app
);
checkCredential
(
'app'
,
app
);
this
.
app
=
app
;
this
.
app
=
app
;
};
};
/**
/**
...
@@ -115,13 +115,13 @@ Client.prototype.setApp = function (app) {
...
@@ -115,13 +115,13 @@ Client.prototype.setApp = function (app) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
sendNotification
=
function
(
notification
,
callback
)
{
Client
.
prototype
.
sendNotification
=
function
(
notification
,
callback
)
{
if
(
!
notification
||
!
notification
.
postBody
)
{
if
(
!
notification
||
!
notification
.
postBody
)
{
throw
'notification parameter must be a typeof Notification object.'
;
throw
'notification parameter must be a typeof Notification object.'
;
}
}
var
postBody
=
notification
.
postBody
;
var
postBody
=
notification
.
postBody
;
if
(
this
.
apps
&&
this
.
apps
.
length
>
0
)
{
if
(
this
.
apps
&&
this
.
apps
.
length
>
0
)
{
postBody
.
app_ids
=
this
.
apps
;
postBody
.
app_ids
=
this
.
apps
;
return
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
);
}
}
if
(
this
.
app
)
{
if
(
this
.
app
)
{
postBody
.
app_id
=
this
.
app
.
appId
;
postBody
.
app_id
=
this
.
app
.
appId
;
...
@@ -136,10 +136,10 @@ Client.prototype.sendNotification = function (notification, callback) {
...
@@ -136,10 +136,10 @@ Client.prototype.sendNotification = function (notification, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
cancelNotification
=
function
(
notificationId
,
callback
)
{
Client
.
prototype
.
cancelNotification
=
function
(
notificationId
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
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
);
};
};
...
@@ -149,10 +149,10 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
...
@@ -149,10 +149,10 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
viewNotification
=
function
(
notificationId
,
callback
)
{
Client
.
prototype
.
viewNotification
=
function
(
notificationId
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
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
);
};
};
...
@@ -162,10 +162,10 @@ Client.prototype.viewNotification = function (notificationId, callback) {
...
@@ -162,10 +162,10 @@ Client.prototype.viewNotification = function (notificationId, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
viewNotifications
=
function
(
query
,
callback
)
{
Client
.
prototype
.
viewNotifications
=
function
(
query
,
callback
)
{
if
(
!
this
.
app
)
{
if
(
!
this
.
app
)
{
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
);
};
};
...
@@ -175,9 +175,9 @@ Client.prototype.viewNotifications = function (query, callback) {
...
@@ -175,9 +175,9 @@ Client.prototype.viewNotifications = function (query, 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
);
};
};
...
@@ -187,9 +187,9 @@ Client.prototype.viewApps = function (callback) {
...
@@ -187,9 +187,9 @@ Client.prototype.viewApps = function (callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
viewApp
=
function
(
appId
,
callback
)
{
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
);
};
};
...
@@ -199,9 +199,9 @@ Client.prototype.viewApp = function (appId, callback) {
...
@@ -199,9 +199,9 @@ Client.prototype.viewApp = function (appId, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
createApp
=
function
(
body
,
callback
)
{
Client
.
prototype
.
createApp
=
function
(
body
,
callback
)
{
if
(
!
body
.
name
)
{
if
(
!
body
.
name
)
{
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'
}
}
...
@@ -214,9 +214,9 @@ Client.prototype.createApp = function (body, callback) {
...
@@ -214,9 +214,9 @@ Client.prototype.createApp = function (body, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
updateApp
=
function
(
body
,
callback
)
{
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'
}
}
...
@@ -230,10 +230,10 @@ Client.prototype.updateApp = function (body, callback) {
...
@@ -230,10 +230,10 @@ Client.prototype.updateApp = function (body, callback) {
* @param callback
* @param callback
*/
*/
Client
.
prototype
.
viewDevices
=
function
(
query
,
callback
)
{
Client
.
prototype
.
viewDevices
=
function
(
query
,
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
+
'?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
);
};
};
...
@@ -305,7 +305,7 @@ Client.prototype.csvExport = function (body, callback) {
...
@@ -305,7 +305,7 @@ Client.prototype.csvExport = function (body, callback) {
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
;
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