@suprsend/node-sdk 1.2.0 → 1.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.
@@ -80,16 +80,12 @@ export default class _SubscriberInternalHelper {
80
80
  this.workspace_key = workspace_key;
81
81
 
82
82
  this.__dict_set = {};
83
- this.__set_count = 0;
84
83
 
85
84
  this.__dict_append = {};
86
- this.__append_count = 0;
87
85
 
88
86
  this.__dict_remove = {};
89
- this.__remove_count = 0;
90
87
 
91
88
  this.__list_unset = [];
92
- this.__unset_count = 0;
93
89
 
94
90
  this.__errors = [];
95
91
  this.__info = [];
@@ -97,17 +93,9 @@ export default class _SubscriberInternalHelper {
97
93
 
98
94
  reset() {
99
95
  this.__dict_set = {};
100
- this.__set_count = 0;
101
-
102
96
  this.__dict_append = {};
103
- this.__append_count = 0;
104
-
105
97
  this.__dict_remove = {};
106
- this.__remove_count = 0;
107
-
108
98
  this.__list_unset = [];
109
- this.__unset_count = 0;
110
-
111
99
  this.__errors = [];
112
100
  this.__info = [];
113
101
  }
@@ -118,48 +106,26 @@ export default class _SubscriberInternalHelper {
118
106
  errors: this.__errors,
119
107
  info: this.__info,
120
108
  event: evt,
121
- set: this.__set_count,
122
- append: this.__append_count,
123
- remove: this.__remove_count,
124
- unset: this.__unset_count,
125
109
  };
126
110
  this.reset();
127
111
  return ret_val;
128
112
  }
129
113
 
130
114
  __form_event() {
131
- if (
132
- !is_empty(this.__dict_set) ||
133
- !is_empty(this.__dict_append) ||
134
- !is_empty(this.__dict_remove) ||
135
- !is_empty(this.__list_unset)
136
- ) {
137
- let event = {
138
- $insert_id: uuid(),
139
- $time: epoch_milliseconds(),
140
- env: this.workspace_key,
141
- distinct_id: this.distinct_id,
142
- };
143
- if (!is_empty(this.__dict_set)) {
144
- event["$set"] = this.__dict_set;
145
- this.__set_count += 1;
146
- }
147
- if (!is_empty(this.__dict_append)) {
148
- event["$append"] = this.__dict_append;
149
- this.__append_count += 1;
150
- }
151
- if (!is_empty(this.__dict_remove)) {
152
- event["$remove"] = this.__dict_remove;
153
- this.__remove_count += 1;
154
- }
155
- if (!is_empty(this.__list_unset)) {
156
- event["$unset"] = this.__list_unset;
157
- this.__unset_count += 1;
158
- }
159
- return event;
160
- } else {
161
- return;
115
+ const event = {};
116
+ if (!is_empty(this.__dict_set)) {
117
+ event["$set"] = this.__dict_set;
118
+ }
119
+ if (!is_empty(this.__dict_append)) {
120
+ event["$append"] = this.__dict_append;
121
+ }
122
+ if (!is_empty(this.__dict_remove)) {
123
+ event["$remove"] = this.__dict_remove;
162
124
  }
125
+ if (!is_empty(this.__list_unset)) {
126
+ event["$unset"] = this.__list_unset;
127
+ }
128
+ return event;
163
129
  }
164
130
 
165
131
  __validate_key_basic(key, caller) {
@@ -240,33 +206,25 @@ export default class _SubscriberInternalHelper {
240
206
  }
241
207
 
242
208
  __add_identity(key, value, args, caller) {
209
+ const new_caller = `${caller}:${key}`;
243
210
  switch (key) {
244
211
  case IDENT_KEY_EMAIL:
245
- this._add_email(value, caller);
212
+ this._add_email(value, new_caller);
246
213
  break;
247
214
  case IDENT_KEY_SMS:
248
- this._add_sms(value, caller);
215
+ this._add_sms(value, new_caller);
249
216
  break;
250
217
  case IDENT_KEY_WHATSAPP:
251
- this._add_whatsapp(value, caller);
218
+ this._add_whatsapp(value, new_caller);
252
219
  break;
253
220
  case IDENT_KEY_ANDROIDPUSH:
254
- this._add_androidpush(value, args[KEY_PUSHVENDOR], caller);
255
- if (this.__dict_append[KEY_PUSHVENDOR]) {
256
- args[KEY_PUSHVENDOR] = this.__dict_append[KEY_PUSHVENDOR];
257
- }
221
+ this._add_androidpush(value, args[KEY_PUSHVENDOR], new_caller);
258
222
  break;
259
223
  case IDENT_KEY_IOSPUSH:
260
- this._add_iospush(value, args[KEY_PUSHVENDOR], caller);
261
- if (this.__dict_append[KEY_PUSHVENDOR]) {
262
- args[KEY_PUSHVENDOR] = this.__dict_append[KEY_PUSHVENDOR];
263
- }
224
+ this._add_iospush(value, args[KEY_PUSHVENDOR], new_caller);
264
225
  break;
265
226
  case IDENT_KEY_WEBPUSH:
266
- this._add_webpush(value, args[KEY_PUSHVENDOR], caller);
267
- if (this.__dict_append[KEY_PUSHVENDOR]) {
268
- args[KEY_PUSHVENDOR] = this.__dict_append[KEY_PUSHVENDOR];
269
- }
227
+ this._add_webpush(value, args[KEY_PUSHVENDOR], new_caller);
270
228
  break;
271
229
  case IDENT_KEY_SLACK:
272
230
  this._add_slack(value, caller);
@@ -277,33 +235,25 @@ export default class _SubscriberInternalHelper {
277
235
  }
278
236
 
279
237
  __remove_identity(key, value, args, caller) {
238
+ const new_caller = `${caller}:${key}`;
280
239
  switch (key) {
281
240
  case IDENT_KEY_EMAIL:
282
- this._remove_email(value, caller);
241
+ this._remove_email(value, new_caller);
283
242
  break;
284
243
  case IDENT_KEY_SMS:
285
- this._remove_sms(value, caller);
244
+ this._remove_sms(value, new_caller);
286
245
  break;
287
246
  case IDENT_KEY_WHATSAPP:
288
- this._remove_whatsapp(value, caller);
247
+ this._remove_whatsapp(value, new_caller);
289
248
  break;
290
249
  case IDENT_KEY_ANDROIDPUSH:
291
- this._remove_androidpush(value, args[KEY_PUSHVENDOR], caller);
292
- if (this.__dict_remove[KEY_PUSHVENDOR]) {
293
- args[KEY_PUSHVENDOR] = this.__dict_remove[KEY_PUSHVENDOR];
294
- }
250
+ this._remove_androidpush(value, args[KEY_PUSHVENDOR], new_caller);
295
251
  break;
296
252
  case IDENT_KEY_IOSPUSH:
297
- this._remove_iospush(value, args[KEY_PUSHVENDOR], caller);
298
- if (this.__dict_remove[KEY_PUSHVENDOR]) {
299
- args[KEY_PUSHVENDOR] = this.__dict_remove[KEY_PUSHVENDOR];
300
- }
253
+ this._remove_iospush(value, args[KEY_PUSHVENDOR], new_caller);
301
254
  break;
302
255
  case IDENT_KEY_WEBPUSH:
303
- this._remove_webpush(value, args[KEY_PUSHVENDOR], caller);
304
- if (this.__dict_remove[KEY_PUSHVENDOR]) {
305
- args[KEY_PUSHVENDOR] = this.__dict_remove[KEY_PUSHVENDOR];
306
- }
256
+ this._remove_webpush(value, args[KEY_PUSHVENDOR], new_caller);
307
257
  break;
308
258
  case IDENT_KEY_SLACK:
309
259
  this._remove_slack(val, caller);
@@ -430,6 +380,9 @@ export default class _SubscriberInternalHelper {
430
380
  if (!provider) {
431
381
  provider = "fcm";
432
382
  }
383
+ if (typeof provider === "string") {
384
+ provider = provider.toLocaleLowerCase();
385
+ }
433
386
  if (!["fcm", "xiaomi", "oppo"].includes(provider)) {
434
387
  this.__errors.push(
435
388
  `[${caller}] unsupported androidpush provider ${provider}`
@@ -475,6 +428,9 @@ export default class _SubscriberInternalHelper {
475
428
  if (!provider) {
476
429
  provider = "apns";
477
430
  }
431
+ if (typeof provider === "string") {
432
+ provider = provider.toLocaleLowerCase();
433
+ }
478
434
  if (!["apns"].includes(provider)) {
479
435
  this.__errors.push(
480
436
  `[${caller}] unsupported iospush provider ${provider}`
@@ -521,6 +477,9 @@ export default class _SubscriberInternalHelper {
521
477
  if (!provider) {
522
478
  provider = "vapid";
523
479
  }
480
+ if (typeof provider === "string") {
481
+ provider = provider.toLocaleLowerCase();
482
+ }
524
483
  if (!["vapid"].includes(provider)) {
525
484
  this.__errors.push(
526
485
  `[${caller}] unsupported webpush provider ${provider}`
@@ -2,7 +2,6 @@ import {
2
2
  IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
3
3
  IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
4
4
  BODY_MAX_APPARENT_SIZE_IN_BYTES,
5
- BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
6
5
  MAX_IDENTITY_EVENTS_IN_BULK_API,
7
6
  } from "./constants";
8
7
  import get_request_signature from "./signature";
@@ -34,16 +33,7 @@ class _BulkSubscribersChunk {
34
33
  }
35
34
 
36
35
  __get_url() {
37
- let url_template = "event/";
38
- if (this.config.include_signature_param) {
39
- if (this.config.auth_enabled) {
40
- url_template = url_template + "?verify=true";
41
- } else {
42
- url_template = url_template + "?verify=false";
43
- }
44
- }
45
- const url_formatted = `${this.config.base_url}${url_template}`;
46
- return url_formatted;
36
+ return `${this.config.base_url}event/`;
47
37
  }
48
38
 
49
39
  __common_headers() {
@@ -100,17 +90,16 @@ class _BulkSubscribersChunk {
100
90
  async trigger() {
101
91
  const headers = { ...this.__headers, ...this.__dynamic_headers() };
102
92
  const content_text = JSON.stringify(this.__chunk);
103
- // Based on whether signature is required or not, add Authorization header
104
- if (this.config.auth_enabled) {
105
- const signature = get_request_signature(
106
- this.__url,
107
- "POST",
108
- content_text,
109
- headers,
110
- this.config.workspace_secret
111
- );
112
- headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
113
- }
93
+
94
+ const signature = get_request_signature(
95
+ this.__url,
96
+ "POST",
97
+ content_text,
98
+ headers,
99
+ this.config.workspace_secret
100
+ );
101
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
102
+
114
103
  try {
115
104
  const response = await axios.post(this.__url, content_text, { headers });
116
105
  const ok_response = Math.floor(response.status / 100) == 2;
@@ -175,11 +164,9 @@ class BulkSubscribers {
175
164
  if (warnings_list) {
176
165
  this.response.warnings = [...this.response.warnings, ...warnings_list];
177
166
  }
178
- const ev_arr = sub.events();
179
- for (let ev of ev_arr) {
180
- const [ev_json, body_size] = sub.validate_event_size(ev);
181
- this.__pending_records.push([ev_json, body_size]);
182
- }
167
+ const ev = sub.get_events();
168
+ const [ev_json, body_size] = sub.validate_event_size(ev);
169
+ this.__pending_records.push([ev_json, body_size]);
183
170
  }
184
171
  }
185
172
 
@@ -204,7 +191,7 @@ class BulkSubscribers {
204
191
  }
205
192
  for (let sub of subscribers) {
206
193
  if (!sub) {
207
- throw new SuprsendError("null/empty element found in bulk instance");
194
+ continue;
208
195
  }
209
196
  if (!(sub instanceof Subscriber)) {
210
197
  throw new SuprsendError(
package/src/workflow.js CHANGED
@@ -71,16 +71,7 @@ export class _WorkflowTrigger {
71
71
  }
72
72
 
73
73
  _get_url() {
74
- let url_template = "/trigger/";
75
- if (this.config.include_signature_param) {
76
- if (this.config.auth_enabled) {
77
- url_template = url_template + "?verify=true";
78
- } else {
79
- url_template = url_template + "?verify=false";
80
- }
81
- }
82
- const url_formatted = `${this.config.base_url}${this.config.workspace_key}${url_template}`;
83
- return url_formatted;
74
+ return `${this.config.base_url}${this.config.workspace_key}/trigger/`;
84
75
  }
85
76
 
86
77
  _get_headers() {
@@ -103,16 +94,15 @@ export class _WorkflowTrigger {
103
94
  async send(workflow_body) {
104
95
  const headers = this._get_headers();
105
96
  const content_text = JSON.stringify(workflow_body);
106
- if (this.config.auth_enabled) {
107
- const signature = get_request_signature(
108
- this.url,
109
- "POST",
110
- content_text,
111
- headers,
112
- this.config.workspace_secret
113
- );
114
- headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
115
- }
97
+
98
+ const signature = get_request_signature(
99
+ this.url,
100
+ "POST",
101
+ content_text,
102
+ headers,
103
+ this.config.workspace_secret
104
+ );
105
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
116
106
 
117
107
  try {
118
108
  const response = await axios.post(this.url, content_text, { headers });
@@ -36,16 +36,7 @@ class _BulkWorkflowsChunk {
36
36
  }
37
37
 
38
38
  __get_url() {
39
- let url_template = "/trigger/";
40
- if (this.config.include_signature_param) {
41
- if (this.config.auth_enabled) {
42
- url_template = url_template + "?verify=true";
43
- } else {
44
- url_template = url_template + "?verify=false";
45
- }
46
- }
47
- const url_formatted = `${this.config.base_url}${this.config.workspace_key}${url_template}`;
48
- return url_formatted;
39
+ return `${this.config.base_url}${this.config.workspace_key}/trigger/`;
49
40
  }
50
41
 
51
42
  __common_headers() {
@@ -105,17 +96,16 @@ class _BulkWorkflowsChunk {
105
96
  async trigger() {
106
97
  const headers = { ...this.__headers, ...this.__dynamic_headers() };
107
98
  const content_text = JSON.stringify(this.__chunk);
108
- // Based on whether signature is required or not, add Authorization header
109
- if (this.config.auth_enabled) {
110
- const signature = get_request_signature(
111
- this.__url,
112
- "POST",
113
- content_text,
114
- headers,
115
- this.config.workspace_secret
116
- );
117
- headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
118
- }
99
+
100
+ const signature = get_request_signature(
101
+ this.__url,
102
+ "POST",
103
+ content_text,
104
+ headers,
105
+ this.config.workspace_secret
106
+ );
107
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
108
+
119
109
  try {
120
110
  const response = await axios.post(this.__url, content_text, { headers });
121
111
  const ok_response = Math.floor(response.status / 100) == 2;