@suprsend/node-sdk 1.2.0 → 1.4.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.
@@ -13,7 +13,7 @@ import {
13
13
  SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
14
14
  } from "./constants";
15
15
 
16
- class SubscribersListBroadcast {
16
+ class SubscriberListBroadcast {
17
17
  constructor(body, kwargs = {}) {
18
18
  if (!(body instanceof Object)) {
19
19
  throw new SuprsendError("broadcast body must be a json/dictionary");
@@ -43,7 +43,7 @@ class SubscribersListBroadcast {
43
43
  }
44
44
  }
45
45
 
46
- class SubscribersListApi {
46
+ class SubscriberListsApi {
47
47
  constructor(config) {
48
48
  this.config = config;
49
49
  this.subscriber_list_url = `${this.config.base_url}v1/subscriber_list/`;
@@ -241,56 +241,56 @@ class SubscribersListApi {
241
241
  }
242
242
  }
243
243
 
244
- // async broadcast(broadcast_instance) {
245
- // if (!(broadcast_instance instanceof SubscribersListBroadcast)) {
246
- // throw new SuprsendError(
247
- // "argument must be an instance of suprsend.SubscriberListBroadcast"
248
- // );
249
- // }
250
- // const [broadcast_body, body_size] = broadcast_instance.get_final_json(
251
- // this.config
252
- // );
253
- // const headers = { ...this.__headers, ...this.__dynamic_headers() };
254
- // const content_text = JSON.stringify(broadcast_body);
255
-
256
- // const signature = get_request_signature(
257
- // this.broadcast_url,
258
- // "POST",
259
- // content_text,
260
- // headers,
261
- // this.config.workspace_secret
262
- // );
263
- // headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
264
-
265
- // try {
266
- // const response = await axios.post(this.broadcast_url, content_text, {
267
- // headers,
268
- // });
269
- // const ok_response = Math.floor(response.status / 100) == 2;
270
- // if (ok_response) {
271
- // return {
272
- // success: true,
273
- // status: "success",
274
- // status_code: response.status,
275
- // message: response.statusText,
276
- // };
277
- // } else {
278
- // return {
279
- // success: false,
280
- // status: "fail",
281
- // status_code: response.status,
282
- // message: response.statusText,
283
- // };
284
- // }
285
- // } catch (err) {
286
- // return {
287
- // success: false,
288
- // status: "fail",
289
- // status_code: err.status || 500,
290
- // message: err.message,
291
- // };
292
- // }
293
- // }
244
+ async broadcast(broadcast_instance) {
245
+ if (!(broadcast_instance instanceof SubscriberListBroadcast)) {
246
+ throw new SuprsendError(
247
+ "argument must be an instance of suprsend.SubscriberListBroadcast"
248
+ );
249
+ }
250
+ const [broadcast_body, body_size] = broadcast_instance.get_final_json(
251
+ this.config
252
+ );
253
+ const headers = { ...this.__headers, ...this.__dynamic_headers() };
254
+ const content_text = JSON.stringify(broadcast_body);
255
+
256
+ const signature = get_request_signature(
257
+ this.broadcast_url,
258
+ "POST",
259
+ content_text,
260
+ headers,
261
+ this.config.workspace_secret
262
+ );
263
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
264
+
265
+ try {
266
+ const response = await axios.post(this.broadcast_url, content_text, {
267
+ headers,
268
+ });
269
+ const ok_response = Math.floor(response.status / 100) == 2;
270
+ if (ok_response) {
271
+ return {
272
+ success: true,
273
+ status: "success",
274
+ status_code: response.status,
275
+ message: response.statusText,
276
+ };
277
+ } else {
278
+ return {
279
+ success: false,
280
+ status: "fail",
281
+ status_code: response.status,
282
+ message: response.statusText,
283
+ };
284
+ }
285
+ } catch (err) {
286
+ return {
287
+ success: false,
288
+ status: "fail",
289
+ status_code: err.status || 500,
290
+ message: err.message,
291
+ };
292
+ }
293
+ }
294
294
  }
295
295
 
296
- export { SubscribersListApi, SubscribersListBroadcast };
296
+ export { SubscriberListsApi, SubscriberListBroadcast };
@@ -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;