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');
var
constants
=
require
(
'./constants'
);
var
ALLOWED_CREDENTIALS
=
[
{
name
:
'userAuthKey'
,
type
:
'string'
},
{
name
:
'app'
,
type
:
'object'
,
requiredFields
:
[
'appAuthKey'
,
'appId'
]
},
{
name
:
'apps'
,
type
:
'object'
}
{
name
:
'userAuthKey'
,
type
:
'string'
},
{
name
:
'app'
,
type
:
'object'
,
requiredFields
:
[
'appAuthKey'
,
'appId'
]
},
{
name
:
'apps'
,
type
:
'object'
}
];
/**
...
...
@@ -18,22 +18,22 @@ var ALLOWED_CREDENTIALS = [
* @param callback (err, httpResponse, body)
*/
var
basicRequest
=
function
(
url
,
apiKey
,
method
,
body
,
callback
)
{
var
options
=
{
url
:
url
,
method
:
method
};
if
(
apiKey
)
{
options
.
headers
=
{
'Content-Type'
:
'application/json; charset=utf-8'
,
'Authorization'
:
'Basic '
+
apiKey
}
}
if
(
body
)
{
options
.
body
=
body
;
options
.
json
=
true
;
var
options
=
{
url
:
url
,
method
:
method
};
if
(
apiKey
)
{
options
.
headers
=
{
'Content-Type'
:
'application/json; charset=utf-8'
,
'Authorization'
:
'Basic '
+
apiKey
}
}
if
(
body
)
{
options
.
body
=
body
;
options
.
json
=
true
;
}
return
new
Promise
(
function
(
resolve
,
reject
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
request
(
options
,
function
(
err
,
httpResponse
,
data
)
{
if
(
err
)
{
callback
&&
callback
(
err
,
httpResponse
,
data
);
...
...
@@ -53,22 +53,22 @@ var basicRequest = function (url, apiKey, method, body, callback) {
* @returns {boolean}
*/
var
checkCredential
=
function
(
credentialName
,
credential
)
{
for
(
var
i
=
0
,
credentialLen
=
ALLOWED_CREDENTIALS
.
length
;
i
<
credentialLen
;
i
++
)
{
if
(
ALLOWED_CREDENTIALS
[
i
].
name
===
credentialName
)
{
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
;
}
if
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
)
{
for
(
var
j
=
0
;
j
<
ALLOWED_CREDENTIALS
[
i
].
requiredFields
.
length
;
j
++
)
{
if
(
!
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
in
credential
))
{
throw
credentialName
+
' must contain '
+
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
}
}
}
return
true
;
}
}
return
false
;
throw
credentialName
+
' must be a '
+
ALLOWED_CREDENTIALS
[
i
].
type
;
}
if
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
)
{
for
(
var
j
=
0
;
j
<
ALLOWED_CREDENTIALS
[
i
].
requiredFields
.
length
;
j
++
)
{
if
(
!
(
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
in
credential
))
{
throw
credentialName
+
' must contain '
+
ALLOWED_CREDENTIALS
[
i
].
requiredFields
[
j
]
}
}
}
return
true
;
}
}
return
false
;
};
/**
...
...
@@ -77,15 +77,15 @@ var checkCredential = function (credentialName, credential) {
* @constructor
*/
var
Client
=
function
(
credentials
)
{
if
(
typeof
credentials
!==
'object'
)
{
throw
'credentials parameter must be a JSON object'
}
this
.
API_URI
=
constants
.
API_ROOT
;
for
(
var
key
in
credentials
)
{
if
(
credentials
.
hasOwnProperty
(
key
)
&&
checkCredential
(
key
,
credentials
[
key
]))
{
this
[
key
]
=
credentials
[
key
];
}
}
if
(
typeof
credentials
!==
'object'
)
{
throw
'credentials parameter must be a JSON object'
}
this
.
API_URI
=
constants
.
API_ROOT
;
for
(
var
key
in
credentials
)
{
if
(
credentials
.
hasOwnProperty
(
key
)
&&
checkCredential
(
key
,
credentials
[
key
]))
{
this
[
key
]
=
credentials
[
key
];
}
}
};
...
...
@@ -94,10 +94,10 @@ var Client = function (credentials) {
* @param rootUrl { String } default 'https://onesignal.com/api/v1';
*/
Client
.
prototype
.
setRootUrl
=
function
(
rootUrl
)
{
if
(
!
rootUrl
)
{
throw
'You must set a valid rootUsrl.'
}
this
.
API_URI
=
rootUrl
;
if
(
!
rootUrl
)
{
throw
'You must set a valid rootUsrl.'
}
this
.
API_URI
=
rootUrl
;
};
/**
...
...
@@ -105,8 +105,8 @@ Client.prototype.setRootUrl = function (rootUrl) {
* @param app { JSON } {}
*/
Client
.
prototype
.
setApp
=
function
(
app
)
{
checkCredential
(
'app'
,
app
);
this
.
app
=
app
;
checkCredential
(
'app'
,
app
);
this
.
app
=
app
;
};
/**
...
...
@@ -115,13 +115,13 @@ Client.prototype.setApp = function (app) {
* @param callback
*/
Client
.
prototype
.
sendNotification
=
function
(
notification
,
callback
)
{
if
(
!
notification
||
!
notification
.
postBody
)
{
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
(
!
notification
||
!
notification
.
postBody
)
{
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
;
...
...
@@ -136,10 +136,10 @@ Client.prototype.sendNotification = function (notification, callback) {
* @param callback
*/
Client
.
prototype
.
cancelNotification
=
function
(
notificationId
,
callback
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
}
var
notificationUri
=
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
+
'/'
+
notificationId
+
'?app_id='
+
this
.
app
.
appId
;
if
(
!
this
.
app
)
{
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
);
};
...
...
@@ -149,10 +149,10 @@ Client.prototype.cancelNotification = function (notificationId, callback) {
* @param callback
*/
Client
.
prototype
.
viewNotification
=
function
(
notificationId
,
callback
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
}
var
notificationUri
=
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
+
'/'
+
notificationId
+
'?app_id='
+
this
.
app
.
appId
;
if
(
!
this
.
app
)
{
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
);
};
...
...
@@ -162,10 +162,10 @@ Client.prototype.viewNotification = function (notificationId, callback) {
* @param callback
*/
Client
.
prototype
.
viewNotifications
=
function
(
query
,
callback
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
}
var
appUri
=
this
.
API_URI
+
constants
.
NOTIFICATIONS_PATH
+
'?app_id='
+
this
.
app
.
appId
+
'&'
+
query
;
if
(
!
this
.
app
)
{
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
);
};
...
...
@@ -175,9 +175,9 @@ Client.prototype.viewNotifications = function (query, callback) {
* @param callback
*/
Client
.
prototype
.
viewApps
=
function
(
callback
)
{
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
}
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
}
return
basicRequest
(
this
.
API_URI
+
constants
.
APPS_PATH
,
this
.
userAuthKey
,
'GET'
,
null
,
callback
);
};
...
...
@@ -187,9 +187,9 @@ Client.prototype.viewApps = function (callback) {
* @param callback
*/
Client
.
prototype
.
viewApp
=
function
(
appId
,
callback
)
{
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
}
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
}
return
basicRequest
(
this
.
API_URI
+
constants
.
APPS_PATH
+
'/'
+
appId
,
this
.
userAuthKey
,
'GET'
,
null
,
callback
);
};
...
...
@@ -199,9 +199,9 @@ Client.prototype.viewApp = function (appId, callback) {
* @param callback
*/
Client
.
prototype
.
createApp
=
function
(
body
,
callback
)
{
if
(
!
body
.
name
)
{
throw
'You must specify a name in body'
;
}
if
(
!
body
.
name
)
{
throw
'You must specify a name in body'
;
}
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
}
...
...
@@ -214,9 +214,9 @@ Client.prototype.createApp = function (body, callback) {
* @param callback
*/
Client
.
prototype
.
updateApp
=
function
(
body
,
callback
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
}
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
}
if
(
!
this
.
userAuthKey
)
{
throw
'You must define "userAuthKey" on Client'
}
...
...
@@ -230,10 +230,10 @@ Client.prototype.updateApp = function (body, callback) {
* @param callback
*/
Client
.
prototype
.
viewDevices
=
function
(
query
,
callback
)
{
if
(
!
this
.
app
)
{
throw
'You must define an "app" object.'
}
var
viewUri
=
this
.
API_URI
+
constants
.
DEVICES_PATH
+
'?app_id='
+
this
.
app
.
appId
+
'&'
+
query
;
if
(
!
this
.
app
)
{
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
);
};
...
...
@@ -305,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
;
return
basicRequest
(
csvUri
,
this
.
app
.
appAuthKey
,
'POST'
,
body
,
callback
);
return
basicRequest
(
csvUri
,
this
.
app
.
appAuthKey
,
'POST'
,
body
,
callback
);
};
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