@parse/push-adapter 6.1.1 → 6.3.0

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/lib/WEB.js DELETED
@@ -1,204 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.WEB = undefined;
7
-
8
- var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
9
-
10
- var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
11
-
12
- var _parse = require('parse');
13
-
14
- var _parse2 = _interopRequireDefault(_parse);
15
-
16
- var _npmlog = require('npmlog');
17
-
18
- var _npmlog2 = _interopRequireDefault(_npmlog);
19
-
20
- var _webPush = require('web-push');
21
-
22
- var _webPush2 = _interopRequireDefault(_webPush);
23
-
24
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
25
-
26
- function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
27
-
28
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
29
-
30
- var LOG_PREFIX = 'parse-server-push-adapter WEB';
31
-
32
- var WEB = exports.WEB = function () {
33
- /**
34
- * Create a new WEB push adapter.
35
- *
36
- * @param {Object} args https://github.com/web-push-libs/web-push#api-reference
37
- */
38
- function WEB(args) {
39
- _classCallCheck(this, WEB);
40
-
41
- if ((typeof args === 'undefined' ? 'undefined' : _typeof(args)) !== 'object' || !args.vapidDetails) {
42
- throw new _parse2.default.Error(_parse2.default.Error.PUSH_MISCONFIGURED, 'WEB Push Configuration is invalid');
43
- }
44
- this.options = args;
45
- }
46
-
47
- /**
48
- * Send web push notification request.
49
- *
50
- * @param {Object} data The data we need to send, the format is the same with api request body
51
- * @param {Array} devices An array of devices
52
- * @returns {Object} A promise which is resolved immediately
53
- */
54
-
55
-
56
- _createClass(WEB, [{
57
- key: 'send',
58
- value: function () {
59
- var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(data, devices) {
60
- var coreData, devicesMap, deviceTokens, resolvers, promises, length, response, results, sent, failed;
61
- return regeneratorRuntime.wrap(function _callee$(_context) {
62
- while (1) {
63
- switch (_context.prev = _context.next) {
64
- case 0:
65
- coreData = data && data.data;
66
-
67
- if (!(!coreData || !devices || !Array.isArray(devices))) {
68
- _context.next = 4;
69
- break;
70
- }
71
-
72
- _npmlog2.default.warn(LOG_PREFIX, 'invalid push payload');
73
- return _context.abrupt('return');
74
-
75
- case 4:
76
- devicesMap = devices.reduce(function (memo, device) {
77
- memo[device.deviceToken] = device;
78
- return memo;
79
- }, {});
80
- deviceTokens = Object.keys(devicesMap);
81
- resolvers = [];
82
- promises = deviceTokens.map(function () {
83
- return new Promise(function (resolve) {
84
- return resolvers.push(resolve);
85
- });
86
- });
87
- length = deviceTokens.length;
88
-
89
- _npmlog2.default.verbose(LOG_PREFIX, 'sending to ' + length + ' ' + (length > 1 ? 'devices' : 'device'));
90
-
91
- _context.next = 12;
92
- return WEB.sendNotifications(coreData, deviceTokens, this.options);
93
-
94
- case 12:
95
- response = _context.sent;
96
- results = response.results, sent = response.sent, failed = response.failed;
97
-
98
- if (sent) {
99
- _npmlog2.default.verbose(LOG_PREFIX, 'WEB Response: %d out of %d sent successfully', sent, results.length);
100
- }
101
- if (failed) {
102
- _npmlog2.default.error(LOG_PREFIX, 'send errored: %d out of %d failed with error %s', failed, results.length, 'push subscription has unsubscribed or expired.');
103
- }
104
- deviceTokens.forEach(function (token, index) {
105
- var resolve = resolvers[index];
106
- var _results$index = results[index],
107
- result = _results$index.result,
108
- error = _results$index.error;
109
-
110
- var device = devicesMap[token];
111
- device.deviceType = 'web';
112
- var resolution = {
113
- device: device,
114
- response: error || result,
115
- transmitted: !error
116
- };
117
- resolve(resolution);
118
- });
119
- return _context.abrupt('return', Promise.all(promises));
120
-
121
- case 18:
122
- case 'end':
123
- return _context.stop();
124
- }
125
- }
126
- }, _callee, this);
127
- }));
128
-
129
- function send(_x, _x2) {
130
- return _ref.apply(this, arguments);
131
- }
132
-
133
- return send;
134
- }()
135
-
136
- /**
137
- * Send multiple web push notification request.
138
- *
139
- * @param {Object} payload The data we need to send, the format is the same with api request body
140
- * @param {Array} deviceTokens An array of devicesTokens
141
- * @param {Object} options The options for the request
142
- * @returns {Object} A promise which is resolved immediately
143
- */
144
-
145
- }], [{
146
- key: 'sendNotifications',
147
- value: function () {
148
- var _ref2 = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee2(payload, deviceTokens, options) {
149
- var promises, allResults, response;
150
- return regeneratorRuntime.wrap(function _callee2$(_context2) {
151
- while (1) {
152
- switch (_context2.prev = _context2.next) {
153
- case 0:
154
- promises = deviceTokens.map(function (deviceToken) {
155
- if (typeof deviceToken === 'string') {
156
- deviceToken = JSON.parse(deviceToken);
157
- }
158
- if ((typeof payload === 'undefined' ? 'undefined' : _typeof(payload)) === 'object') {
159
- payload = JSON.stringify(payload);
160
- }
161
- return _webPush2.default.sendNotification(deviceToken, payload, options);
162
- });
163
- _context2.next = 3;
164
- return Promise.allSettled(promises);
165
-
166
- case 3:
167
- allResults = _context2.sent;
168
- response = {
169
- sent: 0,
170
- failed: 0,
171
- results: []
172
- };
173
-
174
- allResults.forEach(function (result) {
175
- if (result.status === 'fulfilled') {
176
- response.sent += 1;
177
- response.results.push({ result: result.value.statusCode });
178
- } else {
179
- response.failed += 1;
180
- response.results.push({ error: result.reason.body });
181
- }
182
- });
183
- return _context2.abrupt('return', response);
184
-
185
- case 7:
186
- case 'end':
187
- return _context2.stop();
188
- }
189
- }
190
- }, _callee2, this);
191
- }));
192
-
193
- function sendNotifications(_x3, _x4, _x5) {
194
- return _ref2.apply(this, arguments);
195
- }
196
-
197
- return sendNotifications;
198
- }()
199
- }]);
200
-
201
- return WEB;
202
- }();
203
-
204
- exports.default = WEB;
package/lib/index.js DELETED
@@ -1,47 +0,0 @@
1
- "use strict";
2
- // ParsePushAdapter is the default implementation of
3
- // PushAdapter, it uses GCM for android push, APNS for ios push.
4
- // WEB for web push.
5
-
6
- Object.defineProperty(exports, "__esModule", {
7
- value: true
8
- });
9
- exports.utils = exports.WEB = exports.GCM = exports.APNS = exports.ParsePushAdapter = undefined;
10
-
11
- var _npmlog = require('npmlog');
12
-
13
- var _npmlog2 = _interopRequireDefault(_npmlog);
14
-
15
- var _ParsePushAdapter = require('./ParsePushAdapter');
16
-
17
- var _ParsePushAdapter2 = _interopRequireDefault(_ParsePushAdapter);
18
-
19
- var _GCM = require('./GCM');
20
-
21
- var _GCM2 = _interopRequireDefault(_GCM);
22
-
23
- var _APNS = require('./APNS');
24
-
25
- var _APNS2 = _interopRequireDefault(_APNS);
26
-
27
- var _WEB = require('./WEB');
28
-
29
- var _WEB2 = _interopRequireDefault(_WEB);
30
-
31
- var _PushAdapterUtils = require('./PushAdapterUtils');
32
-
33
- var utils = _interopRequireWildcard(_PushAdapterUtils);
34
-
35
- function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
36
-
37
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
38
-
39
- /* istanbul ignore if */
40
- if (process.env.VERBOSE || process.env.VERBOSE_PARSE_SERVER_PUSH_ADAPTER) {
41
- _npmlog2.default.level = 'verbose';
42
- }exports.default = _ParsePushAdapter2.default;
43
- exports.ParsePushAdapter = _ParsePushAdapter2.default;
44
- exports.APNS = _APNS2.default;
45
- exports.GCM = _GCM2.default;
46
- exports.WEB = _WEB2.default;
47
- exports.utils = utils;