pushy 2.0.11 → 2.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,14 +1,11 @@
1
- var request = require('request');
2
- var Promise = require('bluebird');
3
-
4
1
  // Pushy API endpoint
5
2
  var apiEndpoint = 'https://api.pushy.me';
6
3
 
7
4
  // Package constructor
8
5
  function Pushy(apiKey) {
9
- // Make sure the developer provided his/her API key
6
+ // Make sure the developer provided an API key
10
7
  if (!apiKey) {
11
- throw new Error('Please provide your API key to use this package.');
8
+ throw new Error('Please provide your Secret API key to use this package.');
12
9
  }
13
10
 
14
11
  // Check for alphanumeric API key
@@ -20,448 +17,24 @@ function Pushy(apiKey) {
20
17
  this.apiKey = apiKey;
21
18
  }
22
19
 
23
- // Send Notifications API
24
- Pushy.prototype.sendPushNotification = function (data, recipient, options, callback) {
25
- // Keep track of instance 'this'
26
- var that = this;
27
-
28
- // Support empty options
29
- options = options || {};
30
-
31
- // Always return a promise
32
- return new Promise(function (resolve, reject) {
33
- // Custom callback provided?
34
- if (callback) {
35
- resolve = callback;
36
- reject = callback;
37
- }
38
-
39
- // No data provided?
40
- if (!data) {
41
- return reject(new Error('Please provide the push payload to send to devices.'));
42
- }
43
-
44
- // Data must be an object
45
- if ((Object.prototype.toString.call(data) !== '[object Object]')) {
46
- return reject(new Error('Please provide the push payload as an object.'));
47
- }
48
-
49
- // No recipient provided?
50
- if (!recipient) {
51
- return reject(new Error('Please provide the notification recipient.'));
52
- }
53
-
54
- // Options must be an object
55
- if (Object.prototype.toString.call(options) !== '[object Object]') {
56
- return reject(new Error('Please provide the options parameter as an object.'));
57
- }
58
-
59
- // Prepare JSON post data (defaults to options object)
60
- var postData = options;
61
-
62
- // Set payload and device tokens
63
- postData.data = data;
64
-
65
- // Recipient provided as string?
66
- if (typeof recipient === 'string' || Array.isArray(recipient)) {
67
- // Set "to" parameter
68
- postData.to = recipient;
69
- }
70
- else {
71
- // Invalid recipient type
72
- return reject(new Error('Please provide the notification recipient as a string or an array of strings.'));
73
- }
74
-
75
- // Callback must be a function (if provided)
76
- if (callback && typeof callback !== 'function') {
77
- return reject(new Error('Please provide the callback parameter as a function.'));
78
- }
79
-
80
- // Send push using the "request" package
81
- request(Object.assign({
82
- uri: that.getApiEndpoint() + '/push?api_key=' + that.apiKey,
83
- method: 'POST',
84
- json: postData
85
- }, that.extraRequestOptions || {}), function (err, res, body) {
86
- // Request error?
87
- if (err) {
88
- // Send to callback
89
- return reject(err);
90
- }
91
-
92
- // Missing body?
93
- if (!body) {
94
- return reject(new Error('An empty body was received from the Pushy API.'));
95
- }
96
-
97
- // Pushy error?
98
- if (body.error) {
99
- return reject(new Error(body.error));
100
- }
101
-
102
- // Check for 200 OK
103
- if (res.statusCode != 200) {
104
- return reject(new Error('An invalid response code was received from the Pushy API.'));
105
- }
106
-
107
- // Fetch push notification ID
108
- var pushId = body.id;
109
-
110
- // Callback?
111
- if (callback) {
112
- // Pass push ID to callback with a null error
113
- callback(null, pushId);
114
- }
115
- else {
116
- // Resolve the promise
117
- resolve(pushId);
118
- }
119
- });
120
- });
121
- };
122
-
123
- // Notification Deletion API
124
- Pushy.prototype.deletePushNotification = function (pushId, callback) {
125
- // Keep track of instance 'this'
126
- var that = this;
127
-
128
- // Always return a promise
129
- return new Promise(function (resolve, reject) {
130
- // Custom callback provided?
131
- if (callback) {
132
- resolve = callback;
133
- reject = callback;
134
- }
135
-
136
- // No pushId provided?
137
- if (!pushId) {
138
- return reject(new Error('Please provide the notification ID you wish to delete.'));
139
- }
140
-
141
- // pushId must be an string
142
- if (typeof pushId !== 'string') {
143
- return reject(new Error('Please provide the notification ID as a string.'));
144
- }
145
-
146
- // Callback must be a function (if provided)
147
- if (callback && typeof callback !== 'function') {
148
- return reject(new Error('Please provide the callback parameter as a function.'));
149
- }
150
-
151
- // Delete push using the "request" package
152
- request(Object.assign({
153
- uri: that.getApiEndpoint() + '/pushes/' + pushId + '?api_key=' + that.apiKey,
154
- method: 'DELETE',
155
- }, that.extraRequestOptions || {}), function (err, res, body) {
156
- // Request error?
157
- if (err) {
158
- // Send to callback
159
- return reject(err);
160
- }
161
-
162
- // Missing body?
163
- if (!body) {
164
- return reject(new Error('An empty body was received from the Pushy API.'));
165
- }
166
-
167
- // Pushy error?
168
- if (body.error) {
169
- return reject(new Error(body.error));
170
- }
171
-
172
- // Check for 200 OK
173
- if (res.statusCode != 200) {
174
- return reject(new Error('An invalid response code was received from the Pushy API.'));
175
- }
176
-
177
- // Callback?
178
- if (callback) {
179
- // Invoke callback with a null error
180
- callback(null);
181
- }
182
- else {
183
- // Resolve the promise
184
- resolve();
185
- }
186
- });
187
- });
188
- };
189
-
190
- // Pub/Sub Subscribers API
191
- Pushy.prototype.getSubscribers = function (topic, callback) {
192
- // Keep track of instance 'this'
193
- var that = this;
194
-
195
- // Always return a promise
196
- return new Promise((resolve, reject) => {
197
- // Custom callback provided?
198
- if (callback) {
199
- resolve = callback;
200
- reject = callback;
201
- }
202
-
203
- // Check the validity of topic
204
- if (!topic || typeof topic !== 'string') {
205
- return reject(new Error('Invalid topic name'));
206
- }
207
-
208
- // Build URL to Pub/Sub Subscribers API
209
- var endPoint = that.getApiEndpoint() + '/topics/' + topic + '?api_key=' + that.apiKey;
210
-
211
- // Make the request
212
- request(
213
- Object.assign({
214
- uri: endPoint,
215
- method: 'GET',
216
- json: true
217
- }, that.extraRequestOptions || {}), function (err, res, body) {
218
- // Request error?
219
- if (err) {
220
- // Send to callback
221
- return reject(err);
222
- }
223
-
224
- // Missing body?
225
- if (!body) {
226
- return reject(new Error('An empty body was received from the Pushy API.'));
227
- }
228
-
229
- // Pushy error?
230
- if (body.error) {
231
- return reject(new Error(body.error));
232
- }
233
-
234
- // Check for 200 OK
235
- if (res.statusCode != 200) {
236
- return reject(new Error('An invalid response code was received from the Pushy API.'));
237
- }
238
-
239
- // Fetch result
240
- var subscribers = body.subscribers;
241
-
242
- // Callback?
243
- if (callback) {
244
- // Invoke callback with subscribers list
245
- callback(null, subscribers);
246
- }
247
- else {
248
- // Resolve the promise
249
- resolve(body);
250
- }
251
- });
252
- });
253
- }
254
-
255
- // Pub/Sub Topics API
256
- Pushy.prototype.getTopics = function (callback) {
257
- // Keep track of instance 'this'
258
- var that = this;
259
-
260
- // Always return a promise
261
- return new Promise((resolve, reject) => {
262
- // Custom callback provided?
263
- if (callback) {
264
- resolve = callback;
265
- reject = callback;
266
- }
267
-
268
- // Build URL to Pub/Sub Topics API
269
- var endPoint = that.getApiEndpoint() + '/topics/' + '?api_key=' + that.apiKey;
270
-
271
- // Make the request
272
- request(
273
- Object.assign({
274
- uri: endPoint,
275
- method: 'GET',
276
- json: true
277
- }, that.extraRequestOptions || {}), function (err, res, body) {
278
- // Request error?
279
- if (err) {
280
- // Send to callback
281
- return reject(err);
282
- }
283
-
284
- // Missing body?
285
- if (!body) {
286
- return reject(new Error('An empty body was received from the Pushy API.'));
287
- }
288
-
289
- // Pushy error?
290
- if (body.error) {
291
- return reject(new Error(body.error));
292
- }
293
-
294
- // Check for 200 OK
295
- if (res.statusCode != 200) {
296
- return reject(new Error('An invalid response code was received from the Pushy API.'));
297
- }
298
-
299
- // Fetch result
300
- var topics = body.topics;
301
-
302
- // Callback?
303
- if (callback) {
304
- // Invoke callback with topics list
305
- callback(null, topics);
306
- }
307
- else {
308
- // Resolve the promise
309
- resolve(body);
310
- }
311
- });
312
- });
313
- }
314
-
315
- // Pub/Sub Subscribe API
316
- Pushy.prototype.subscribe = function (topics, deviceToken, callback) {
317
- // Keep track of instance 'this'
318
- var that = this;
319
-
320
- // Always return a promise
321
- return new Promise(function (resolve, reject) {
322
- // Custom callback provided?
323
- if (callback) {
324
- resolve = callback;
325
- reject = callback;
326
- }
327
-
328
- // Device token passed in must be a string
329
- if (typeof deviceToken !== 'string') {
330
- return reject(new Error('Please provide the device token as a string.'));
331
- }
332
-
333
- // Topics passed in must be in string or array format
334
- if (typeof topics !== 'string' && !Array.isArray(topics)) {
335
- return reject(new Error('Please provide the Pub/Sub topics parameter as a string or an array of strings.'));
336
- }
337
-
338
- // Build URL to Pub/Sub Subscribe API
339
- var endPoint = that.getApiEndpoint() + '/topics/subscribe/' + '?api_key=' + that.apiKey;
340
-
341
- // Prepare JSON post data
342
- var postData = {};
343
-
344
- // Add token to the post body
345
- postData.token = deviceToken;
346
-
347
- // Convert singular string topic to array
348
- postData.topics = Array.isArray(topics) ? topics : [topics];
349
-
350
- // Make the request
351
- request(
352
- Object.assign({
353
- uri: endPoint,
354
- method: 'POST',
355
- json: postData
356
- }, that.extraRequestOptions || {}), function (err, res, body) {
357
- // Request error?
358
- if (err) {
359
- // Send to callback
360
- return reject(err);
361
- }
362
-
363
- // Missing body?
364
- if (!body) {
365
- return reject(new Error('An empty body was received from the Pushy API.'));
366
- }
367
-
368
- // Pushy error?
369
- if (body.error) {
370
- return reject(new Error(body.error));
371
- }
372
-
373
- // Check for 200 OK
374
- if (res.statusCode != 200) {
375
- return reject(new Error('An invalid response code was received from the Pushy API.'));
376
- }
377
-
378
- // Callback?
379
- if (callback) {
380
- // Pass null error (success)
381
- callback(null);
382
- }
383
- else {
384
- // Resolve the promise successfully
385
- resolve();
386
- }
387
- });
388
- });
389
- }
390
-
391
- // Pub/Sub Unsubscribe API
392
- Pushy.prototype.unsubscribe = function (topics, deviceToken, callback) {
393
- // Keep track of instance 'this'
394
- var that = this;
395
-
396
- // Always return a promise
397
- return new Promise(function (resolve, reject) {
398
- // Custom callback provided?
399
- if (callback) {
400
- resolve = callback;
401
- reject = callback;
402
- }
403
-
404
- // Device token passed in must be a string
405
- if (typeof deviceToken !== 'string') {
406
- return reject(new Error('Please provide the device token as a string.'));
407
- }
408
-
409
- // Topics passed in must be in string or array format
410
- if (typeof topics !== 'string' && !Array.isArray(topics)) {
411
- return reject(new Error('Please provide the Pub/Sub topics parameter as a string or an array of strings.'));
412
- }
413
-
414
- // Build URL to Pub/Sub Unsubscribe API
415
- var endPoint = that.getApiEndpoint() + '/topics/unsubscribe/' + '?api_key=' + that.apiKey;
416
-
417
- // Prepare JSON post data
418
- var postData = {};
419
-
420
- // Add token to the post body
421
- postData.token = deviceToken;
422
-
423
- // Convert singular string topic to array
424
- postData.topics = Array.isArray(topics) ? topics : [topics];
20
+ // Push APIs
21
+ Pushy.prototype.sendPushNotification = require('./api/push/send');
22
+ Pushy.prototype.getNotificationStatus = require('./api/push/status');
23
+ Pushy.prototype.deletePushNotification = require('./api/push/delete');
425
24
 
426
- // Make the request
427
- request(
428
- Object.assign({
429
- uri: endPoint,
430
- method: 'POST',
431
- json: postData
432
- }, that.extraRequestOptions || {}), function (err, res, body) {
433
- // Request error?
434
- if (err) {
435
- // Send to callback
436
- return reject(err);
437
- }
25
+ // Device APIs
26
+ Pushy.prototype.getDeviceInfo = require('./api/device/info');
27
+ Pushy.prototype.getDevicePresence = require('./api/device/presence');
438
28
 
439
- // Missing body?
440
- if (!body) {
441
- return reject(new Error('An empty body was received from the Pushy API.'));
442
- }
29
+ // Pub/Sub APIs
30
+ Pushy.prototype.getTopics = require('./api/pubsub/topics');
31
+ Pushy.prototype.subscribe = require('./api/pubsub/subscribe');
32
+ Pushy.prototype.unsubscribe = require('./api/pubsub/unsubscribe');
33
+ Pushy.prototype.getSubscribers = require('./api/pubsub/subscribers');
443
34
 
444
- // Pushy error?
445
- if (body.error) {
446
- return reject(new Error(body.error));
447
- }
448
-
449
- // Check for 200 OK
450
- if (res.statusCode != 200) {
451
- return reject(new Error('An invalid response code was received from the Pushy API.'));
452
- }
453
-
454
- // Callback?
455
- if (callback) {
456
- // Pass null error (success)
457
- callback(null);
458
- }
459
- else {
460
- // Resolve the promise successfully
461
- resolve();
462
- }
463
- });
464
- });
35
+ // API endpoint selector
36
+ Pushy.prototype.getApiEndpoint = function () {
37
+ return (this.enterpriseEndpoint) ? this.enterpriseEndpoint : apiEndpoint;
465
38
  }
466
39
 
467
40
  // Support for Pushy Enterprise
@@ -469,15 +42,10 @@ Pushy.prototype.setEnterpriseConfig = function (endpoint) {
469
42
  this.enterpriseEndpoint = endpoint;
470
43
  }
471
44
 
472
- // API endpoint selector
473
- Pushy.prototype.getApiEndpoint = function () {
474
- return (this.enterpriseEndpoint) ? this.enterpriseEndpoint : apiEndpoint;
475
- }
476
-
477
45
  // Add extra options that will be passed to the request library
478
46
  Pushy.prototype.setExtraRequestOptions = function (extraRequestOptions) {
479
47
  this.extraRequestOptions = extraRequestOptions;
480
48
  }
481
49
 
482
50
  // Expose the Pushy object
483
- module.exports = Pushy;
51
+ module.exports = Pushy;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pushy",
3
- "version": "2.0.11",
3
+ "version": "2.0.14",
4
4
  "description": "The official Node.js package for sending push notifications with Pushy.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -8,14 +8,14 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/pushy-me/pushy-node.git"
11
+ "url": "git+https://github.com/pushy/pushy-node.git"
12
12
  },
13
13
  "author": "Pushy <support@pushy.me>",
14
14
  "license": "Apache-2.0",
15
15
  "bugs": {
16
- "url": "https://github.com/pushy-me/pushy-node/issues"
16
+ "url": "https://github.com/pushy/pushy-node/issues"
17
17
  },
18
- "homepage": "https://github.com/pushy-me/pushy-node#readme",
18
+ "homepage": "https://github.com/pushy/pushy-node#readme",
19
19
  "dependencies": {
20
20
  "bluebird": "^3.4.1",
21
21
  "request": "^2.72.0"
package/examples/send.js DELETED
@@ -1,35 +0,0 @@
1
- // Change to require('pushy') to use this code in your own project
2
- var Pushy = require('../');
3
-
4
- // Plug in your Secret API Key
5
- // Get it here: https://dashboard.pushy.me/
6
- var pushy = new Pushy('SECRET_API_KEY');
7
-
8
- // Set push payload data to deliver to device(s)
9
- var data = {
10
- message: 'Hello World!'
11
- };
12
-
13
- // Insert target device token(s) here
14
- var tokens = ['DEVICE_TOKEN'];
15
-
16
- // Set optional push notification options (such as iOS notification fields)
17
- var options = {
18
- notification: {
19
- badge: 1,
20
- sound: 'ping.aiff',
21
- body: 'Hello World \u270c'
22
- },
23
- };
24
-
25
- // Send push notification via the Send Notifications API
26
- // https://pushy.me/docs/api/send-notifications
27
- pushy.sendPushNotification(data, tokens, options, function (err, id) {
28
- // Log errors to console
29
- if (err) {
30
- return console.log('Fatal Error', err);
31
- }
32
-
33
- // Log success
34
- console.log('Push sent successfully! (ID: ' + id + ')');
35
- });