@suprsend/node-sdk 1.1.2 → 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.
@@ -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(
@@ -0,0 +1,296 @@
1
+ import {
2
+ SuprsendError,
3
+ SuprsendApiError,
4
+ validate_list_broadcast_body_schema,
5
+ get_apparent_list_broadcast_body_size,
6
+ uuid,
7
+ epoch_milliseconds,
8
+ } from "./utils";
9
+ import get_request_signature from "./signature";
10
+ import axios from "axios";
11
+ import {
12
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
13
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
14
+ } from "./constants";
15
+
16
+ class SubscribersListBroadcast {
17
+ constructor(body, kwargs = {}) {
18
+ if (!(body instanceof Object)) {
19
+ throw new SuprsendError("broadcast body must be a json/dictionary");
20
+ }
21
+ this.body = body;
22
+ this.idempotency_key = kwargs?.idempotency_key;
23
+ this.brand_id = kwargs?.brand_id;
24
+ }
25
+
26
+ get_final_json() {
27
+ this.body["$insert_id"] = uuid();
28
+ this.body["$time"] = epoch_milliseconds();
29
+ if (this.idempotency_key) {
30
+ this.body["$idempotency_key"] = this.idempotency_key;
31
+ }
32
+ if (this.brand_id) {
33
+ this.body["brand_id"] = this.brand_id;
34
+ }
35
+ this.body = validate_list_broadcast_body_schema(this.body);
36
+ const apparent_size = get_apparent_list_broadcast_body_size(this.body);
37
+ if (apparent_size > SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
38
+ throw new SuprsendError(
39
+ `SubscriberListBroadcast body too big - ${apparent_size} Bytes, must not cross ${SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
40
+ );
41
+ }
42
+ return [this.body, apparent_size];
43
+ }
44
+ }
45
+
46
+ class SubscribersListApi {
47
+ constructor(config) {
48
+ this.config = config;
49
+ this.subscriber_list_url = `${this.config.base_url}v1/subscriber_list/`;
50
+ this.broadcast_url = `${this.config.base_url}${this.config.workspace_key}/broadcast/`;
51
+ this.__headers = this.__common_headers();
52
+ this.non_error_default_response = { success: true };
53
+ }
54
+
55
+ __common_headers() {
56
+ return {
57
+ "Content-Type": "application/json; charset=utf-8",
58
+ "User-Agent": this.config.user_agent,
59
+ };
60
+ }
61
+
62
+ __dynamic_headers() {
63
+ return {
64
+ Date: new Date().toUTCString(),
65
+ };
66
+ }
67
+
68
+ _validate_list_id(list_id) {
69
+ if (typeof list_id != "string") {
70
+ throw new SuprsendError("list_id must be a string");
71
+ }
72
+ let cleaned_list_id = list_id.trim();
73
+ if (!cleaned_list_id) {
74
+ throw new SuprsendError("missing list_id");
75
+ }
76
+ return list_id;
77
+ }
78
+
79
+ async create(payload) {
80
+ if (!payload) {
81
+ throw new SuprsendError("missing payload");
82
+ }
83
+ let list_id = payload["list_id"];
84
+ if (!list_id) {
85
+ throw new SuprsendError("missing list_id in payload");
86
+ }
87
+ list_id = this._validate_list_id(list_id);
88
+ payload["list_id"] = list_id;
89
+
90
+ const headers = { ...this.__headers, ...this.__dynamic_headers() };
91
+ const content_text = JSON.stringify(payload);
92
+
93
+ const signature = get_request_signature(
94
+ this.subscriber_list_url,
95
+ "POST",
96
+ content_text,
97
+ headers,
98
+ this.config.workspace_secret
99
+ );
100
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
101
+
102
+ try {
103
+ const response = await axios.post(
104
+ this.subscriber_list_url,
105
+ content_text,
106
+ { headers }
107
+ );
108
+ return response.data;
109
+ } catch (err) {
110
+ throw new SuprsendApiError(err);
111
+ }
112
+ }
113
+
114
+ cleaned_limit_offset(limit, offset) {
115
+ let cleaned_limit =
116
+ typeof limit === "number" && limit > 0 && limit <= 1000 ? limit : 20;
117
+ let cleaned_offset = typeof offset === "number" && offset >= 0 ? offset : 0;
118
+ return [cleaned_limit, cleaned_offset];
119
+ }
120
+
121
+ async get_all(kwargs = {}) {
122
+ let limit = kwargs?.limit;
123
+ let offset = kwargs?.offset;
124
+ const [cleaned_limit, cleaner_offset] = this.cleaned_limit_offset(
125
+ limit,
126
+ offset
127
+ );
128
+ const final_url_obj = new URL(`${this.config.base_url}v1/subscriber_list`);
129
+ final_url_obj.searchParams.append("limit", cleaned_limit);
130
+ final_url_obj.searchParams.append("offset", cleaner_offset);
131
+ const url = final_url_obj.href;
132
+
133
+ const headers = { ...this.__headers, ...this.__dynamic_headers() };
134
+
135
+ const signature = get_request_signature(
136
+ url,
137
+ "GET",
138
+ "",
139
+ headers,
140
+ this.config.workspace_secret
141
+ );
142
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
143
+
144
+ try {
145
+ const response = await axios.get(url, { headers });
146
+ return response.data;
147
+ } catch (err) {
148
+ throw new SuprsendApiError(err);
149
+ }
150
+ }
151
+
152
+ __subscriber_list_detail_url(list_id) {
153
+ return `${this.config.base_url}v1/subscriber_list/${list_id}/`;
154
+ }
155
+
156
+ async get(list_id) {
157
+ const cleaned_list_id = this._validate_list_id(list_id);
158
+
159
+ const url = this.__subscriber_list_detail_url(cleaned_list_id);
160
+
161
+ const headers = { ...this.__headers, ...this.__dynamic_headers() };
162
+
163
+ const signature = get_request_signature(
164
+ url,
165
+ "GET",
166
+ "",
167
+ headers,
168
+ this.config.workspace_secret
169
+ );
170
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
171
+
172
+ try {
173
+ const response = await axios.get(url, { headers });
174
+ return response.data;
175
+ } catch (err) {
176
+ throw new SuprsendApiError(err);
177
+ }
178
+ }
179
+
180
+ async add(list_id, distinct_ids) {
181
+ const cleaned_list_id = this._validate_list_id(list_id);
182
+ if (!Array.isArray(distinct_ids)) {
183
+ throw new SuprsendError("distinct_ids must be list of strings");
184
+ }
185
+ if (distinct_ids.length === 0) {
186
+ return this.non_error_default_response;
187
+ }
188
+
189
+ const url = `${this.__subscriber_list_detail_url(
190
+ cleaned_list_id
191
+ )}subscriber/add`;
192
+ const headers = { ...this.__headers, ...this.__dynamic_headers() };
193
+ const content_text = JSON.stringify({ distinct_ids: distinct_ids });
194
+
195
+ const signature = get_request_signature(
196
+ url,
197
+ "POST",
198
+ content_text,
199
+ headers,
200
+ this.config.workspace_secret
201
+ );
202
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
203
+
204
+ try {
205
+ const response = await axios.post(url, content_text, { headers });
206
+ return response.data;
207
+ } catch (err) {
208
+ throw new SuprsendApiError(err);
209
+ }
210
+ }
211
+
212
+ async remove(list_id, distinct_ids = []) {
213
+ const cleaned_list_id = this._validate_list_id(list_id);
214
+ if (!Array.isArray(distinct_ids)) {
215
+ throw new SuprsendError("distinct_ids must be list of strings");
216
+ }
217
+ if (distinct_ids.length === 0) {
218
+ return this.non_error_default_response;
219
+ }
220
+
221
+ const url = `${this.__subscriber_list_detail_url(
222
+ cleaned_list_id
223
+ )}subscriber/remove`;
224
+ const headers = { ...this.__headers, ...this.__dynamic_headers() };
225
+ const content_text = JSON.stringify({ distinct_ids: distinct_ids });
226
+
227
+ const signature = get_request_signature(
228
+ url,
229
+ "POST",
230
+ content_text,
231
+ headers,
232
+ this.config.workspace_secret
233
+ );
234
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
235
+
236
+ try {
237
+ const response = await axios.post(url, content_text, { headers });
238
+ return response.data;
239
+ } catch (err) {
240
+ throw new SuprsendApiError(err);
241
+ }
242
+ }
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
+ // }
294
+ }
295
+
296
+ export { SubscribersListApi, SubscribersListBroadcast };
package/src/utils.js CHANGED
@@ -12,6 +12,7 @@ import { cloneDeep } from "lodash";
12
12
 
13
13
  const workflow_schema = require("./request_json/workflow.json");
14
14
  const event_schema = require("./request_json/event.json");
15
+ const list_broadcast_schema = require("./request_json/list_broadcast.json");
15
16
 
16
17
  export function base64Encode(file) {
17
18
  var body = fs.readFileSync(file);
@@ -122,6 +123,25 @@ export function validate_track_event_schema(body) {
122
123
  }
123
124
  }
124
125
 
126
+ export function validate_list_broadcast_body_schema(body) {
127
+ if (!body?.data) {
128
+ body.data = {};
129
+ }
130
+ if (!(body.data instanceof Object)) {
131
+ throw new SuprsendError("data must be a object");
132
+ }
133
+ const schema = list_broadcast_schema;
134
+ var v = new Validator();
135
+ const validated_data = v.validate(body, schema);
136
+ if (validated_data.valid) {
137
+ return body;
138
+ } else {
139
+ const error_obj = validated_data.errors[0];
140
+ const error_msg = `${error_obj.property} ${error_obj.message}`;
141
+ throw new SuprsendError(error_msg);
142
+ }
143
+ }
144
+
125
145
  export function get_apparent_workflow_body_size(body, is_part_of_bulk) {
126
146
  let extra_bytes = WORKFLOW_RUNTIME_KEYS_POTENTIAL_SIZE_IN_BYTES;
127
147
  let apparent_body = body;
@@ -199,6 +219,11 @@ export function get_apparent_event_size(event, is_part_of_bulk) {
199
219
  }
200
220
 
201
221
  export function get_apparent_identity_event_size(event) {
202
- const body_size = JSON.stringify(event);
222
+ const body_size = JSON.stringify(event).length;
223
+ return body_size;
224
+ }
225
+
226
+ export function get_apparent_list_broadcast_body_size(body) {
227
+ const body_size = JSON.stringify(body).length;
203
228
  return body_size;
204
229
  }
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;