@suprsend/node-sdk 1.10.0 → 1.11.1

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/src/object.js ADDED
@@ -0,0 +1,621 @@
1
+ import {
2
+ is_object,
3
+ is_empty,
4
+ is_string,
5
+ InputValueError, SuprsendApiError,
6
+ } from "./utils";
7
+ import get_request_signature from "./signature";
8
+ import axios from "axios";
9
+ import _ObjectInternalHelper from "./object_helper";
10
+
11
+
12
+ class ObjectsApi {
13
+ constructor(config) {
14
+ this.config = config;
15
+ this.listUrl = this.create_list_url();
16
+ this.headers = this.common_headers();
17
+ }
18
+
19
+ create_list_url() {
20
+ return `${this.config.base_url}v1/object/`;
21
+ }
22
+
23
+ common_headers() {
24
+ return {
25
+ 'Content-Type': 'application/json; charset=utf-8',
26
+ 'User-Agent': this.config.user_agent,
27
+ };
28
+ }
29
+
30
+ dynamic_headers() {
31
+ return {
32
+ 'Date': new Date().toISOString(), // Adjust to your header date format
33
+ };
34
+ }
35
+
36
+ validate_object_entity_string(entityId) {
37
+ if (!is_string(entityId)) {
38
+ throw new InputValueError(
39
+ "object entity must be a string"
40
+ );
41
+ }
42
+ entityId = entityId.trim();
43
+ if (!entityId) {
44
+ throw new InputValueError("object entity must be passed");
45
+ }
46
+ return entityId;
47
+ }
48
+
49
+ async list(objectType, options = {}) {
50
+ const params = new URLSearchParams(options).toString();
51
+ const validatedType = this.validate_object_entity_string(objectType);
52
+ const encodedType = encodeURIComponent(validatedType);
53
+ const url = `${this.listUrl}${encodedType}/?${params}`;
54
+ const headers = { ...this.headers, ...this.dynamic_headers() };
55
+ const signature = get_request_signature(
56
+ url,
57
+ "GET",
58
+ "",
59
+ headers,
60
+ this.config.workspace_secret
61
+ );
62
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
63
+
64
+ try {
65
+ const response = await axios.get(url, { headers });
66
+ return response.data;
67
+ } catch (err) {
68
+ throw new SuprsendApiError(err);
69
+ }
70
+ }
71
+
72
+ async detail_url(objectType, objectId) {
73
+ const validatedType = this.validate_object_entity_string(objectType);
74
+ const encodedType = encodeURIComponent(validatedType);
75
+
76
+ const validatedId = this.validate_object_entity_string(objectId);
77
+ const encodedId = encodeURIComponent(validatedId);
78
+
79
+ return `${this.listUrl}${encodedType}/${encodedId}/`;
80
+ }
81
+
82
+ async get(objectType, objectId) {
83
+ const url = await this.detail_url(objectType, objectId);
84
+ const headers = { ...this.headers, ...this.dynamic_headers() };
85
+ const signature = get_request_signature(
86
+ url,
87
+ "GET",
88
+ "",
89
+ headers,
90
+ this.config.workspace_secret
91
+ );
92
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
93
+
94
+ try {
95
+ const response = await axios.get(url, { headers });
96
+ return response.data;
97
+ } catch (err) {
98
+ throw new SuprsendApiError(err);
99
+ }
100
+ }
101
+
102
+ async upsert(objectType, objectId, objectPayload = {}) {
103
+ const url = await this.detail_url(objectType, objectId);
104
+ const headers = { ...this.headers, ...this.dynamic_headers() };
105
+ const contentText = JSON.stringify(objectPayload);
106
+ const signature = get_request_signature(
107
+ url,
108
+ "POST",
109
+ contentText,
110
+ headers,
111
+ this.config.workspace_secret
112
+ );
113
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
114
+
115
+ try {
116
+ const response = await axios.post(url, contentText, { headers });
117
+ return response.data;
118
+ } catch (err) {
119
+ throw new SuprsendApiError(err);
120
+ }
121
+ }
122
+
123
+ async edit(objectType, objectId, editPayload = {}) {
124
+ const url = await this.detail_url(objectType, objectId);
125
+ const headers = { ...this.headers, ...this.dynamic_headers() };
126
+ const contentText = JSON.stringify(editPayload);
127
+ const signature = get_request_signature(
128
+ url,
129
+ "PATCH",
130
+ contentText,
131
+ headers,
132
+ this.config.workspace_secret
133
+ );
134
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
135
+
136
+ try {
137
+ const response = await axios.patch(url, contentText, { headers });
138
+ return response.data;
139
+ } catch (err) {
140
+ throw new SuprsendApiError(err);
141
+ }
142
+ }
143
+
144
+ async delete(objectType, objectId) {
145
+ const url = await this.detail_url(objectType, objectId);
146
+ const headers = { ...this.headers, ...this.dynamic_headers() };
147
+ const signature = get_request_signature(
148
+ url,
149
+ "DELETE",
150
+ "",
151
+ headers,
152
+ this.config.workspace_secret
153
+ );
154
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
155
+
156
+ try {
157
+ const response = await axios.delete(url, { headers });
158
+ return response.data;
159
+ } catch (err) {
160
+ throw new SuprsendApiError(err);
161
+ }
162
+ }
163
+
164
+ async bulk_ops_url(objectType) {
165
+ const validatedType = this.validate_object_entity_string(objectType);
166
+ const encodedType = encodeURIComponent(validatedType);
167
+
168
+ return `${this.config.base_url}v1/bulk/object/${encodedType}/`;
169
+ }
170
+
171
+ async bulk_delete(objectType, payload) {
172
+ const url = await this.bulk_ops_url(objectType);
173
+ const headers = { ...this.headers, ...this.dynamic_headers() };
174
+ payload = payload || {}
175
+ const contentText = JSON.stringify(payload);
176
+ const signature = get_request_signature(
177
+ url,
178
+ "DELETE",
179
+ contentText,
180
+ headers,
181
+ this.config.workspace_secret
182
+ );
183
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
184
+
185
+ try {
186
+ const response = await axios.delete(url, {
187
+ headers: headers,
188
+ data: payload
189
+ });
190
+ return response.data;
191
+ } catch (err) {
192
+ throw new SuprsendApiError(err);
193
+ }
194
+ }
195
+
196
+ async get_subscriptions(objectType, objectId, options = {}) {
197
+ const params = new URLSearchParams(options).toString();
198
+ const url = await this.detail_url(objectType, objectId);
199
+ const subscription_url = `${url}subscription/?${params}`;
200
+ const headers = { ...this.headers, ...this.dynamic_headers() };
201
+ const signature = get_request_signature(
202
+ subscription_url,
203
+ "GET",
204
+ "",
205
+ headers,
206
+ this.config.workspace_secret
207
+ );
208
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
209
+
210
+ try {
211
+ const response = await axios.get(subscription_url, { headers });
212
+ return response.data;
213
+ } catch (err) {
214
+ throw new SuprsendApiError(err);
215
+ }
216
+ }
217
+
218
+ async create_subscriptions(objectType, objectId, subscriptions) {
219
+ const url = await this.detail_url(objectType, objectId);
220
+ const subscription_url = `${url}subscription/`;
221
+ const headers = { ...this.headers, ...this.dynamic_headers() };
222
+ subscriptions = subscriptions || {}
223
+ const contentText = JSON.stringify(subscriptions);
224
+ const signature = get_request_signature(
225
+ subscription_url,
226
+ "POST",
227
+ contentText,
228
+ headers,
229
+ this.config.workspace_secret
230
+ );
231
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
232
+
233
+ try {
234
+ const response = await axios.post(subscription_url, contentText, { headers });
235
+ return response.data;
236
+ } catch (err) {
237
+ throw new SuprsendApiError(err);
238
+ }
239
+ }
240
+
241
+ async delete_subscriptions(objectType, objectId, subscriptions) {
242
+ const url = await this.detail_url(objectType, objectId);
243
+ const subscription_url = `${url}subscription/`;
244
+ const headers = { ...this.headers, ...this.dynamic_headers() };
245
+ subscriptions = subscriptions || {}
246
+ const contentText = JSON.stringify(subscriptions);
247
+ const signature = get_request_signature(
248
+ subscription_url,
249
+ "DELETE",
250
+ contentText,
251
+ headers,
252
+ this.config.workspace_secret
253
+ );
254
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
255
+
256
+ try {
257
+ const response = await axios.delete(subscription_url, {
258
+ headers: headers,
259
+ data: subscriptions
260
+ });
261
+ return response.data;
262
+ } catch (err) {
263
+ throw new SuprsendApiError(err);
264
+ }
265
+ }
266
+
267
+ get_instance(object_type, object_id) {
268
+ if (!is_string(object_type)) {
269
+ throw new InputValueError(
270
+ "object_type must be a string"
271
+ );
272
+ }
273
+ object_type = object_type.trim();
274
+ if (!object_type) {
275
+ throw new InputValueError("object_type must be passed");
276
+ }
277
+
278
+ if (!is_string(object_id)) {
279
+ throw new InputValueError(
280
+ "object_id must be a string"
281
+ );
282
+ }
283
+ object_id = object_id.trim();
284
+ if (!object_id) {
285
+ throw new InputValueError("object_id must be passed");
286
+ }
287
+ return new _Object(this.config, object_type, object_id);
288
+ }
289
+
290
+ }
291
+
292
+ export class _Object {
293
+ constructor(config, object_type, object_id) {
294
+ this.config = config;
295
+ this.object_type = object_type;
296
+ this.object_id = object_id;
297
+ this.__url = this.__get_url();
298
+ this.__super_props = this.__super_properties();
299
+
300
+ this.__errors = [];
301
+ this.__info = [];
302
+ this.operations = [];
303
+ this._helper = new _ObjectInternalHelper();
304
+ }
305
+
306
+ __get_url() {
307
+ return `${this.config.base_url}v1/object/${this.object_type}/${this.object_id}/`;
308
+ }
309
+
310
+ __get_headers() {
311
+ return {
312
+ "Content-Type": "application/json; charset=utf-8",
313
+ Date: new Date().toUTCString(),
314
+ "User-Agent": this.config.user_agent,
315
+ };
316
+ }
317
+
318
+ __super_properties() {
319
+ return {
320
+ $ss_sdk_version: this.config.user_agent,
321
+ };
322
+ }
323
+
324
+ validate_body() {
325
+ if (!is_empty(this.__info)) {
326
+ const msg = `[object_type: ${this.object_type}, ${this.object_id}]${this.__info.join("\n")}`;
327
+ console.log(`WARNING: ${msg}`);
328
+ }
329
+ if (!is_empty(this.__errors)) {
330
+ const msg = `[object_type: ${this.object_type}, ${this.object_id}]${this.__errors.join("\n")}`;
331
+ console.log(`ERROR: ${msg}`);
332
+ }
333
+ }
334
+
335
+ async save() {
336
+ this.validate_body();
337
+ const headers = this.__get_headers();
338
+ //
339
+ const payload = {
340
+ operations: this.operations
341
+ }
342
+ const content_text = JSON.stringify(payload);
343
+
344
+ const signature = get_request_signature(
345
+ this.__url,
346
+ "PATCH",
347
+ content_text,
348
+ headers,
349
+ this.config.workspace_secret
350
+ );
351
+ headers['Authorization'] = `${this.config.workspace_key}:${signature}`;
352
+
353
+ try {
354
+ const response = await axios.patch(this.__url, content_text, { headers });
355
+ return response.data;
356
+ } catch (err) {
357
+ throw new SuprsendApiError(err);
358
+ }
359
+ }
360
+
361
+ _collect_payload() {
362
+ const resp = this._helper.get_identity_events();
363
+ if (!is_empty(resp["errors"])) {
364
+ this.__errors = [...this.__errors, ...resp["errors"]];
365
+ }
366
+ if (!is_empty(resp["info"])) {
367
+ this.__info = [...this.__info, ...resp["info"]];
368
+ }
369
+ if (!is_empty(resp["payload"])) {
370
+ this.operations.push(resp["payload"]);
371
+ }
372
+ }
373
+
374
+ append(key, value) {
375
+ const caller = "append";
376
+ if (!is_string(key) && !is_object(key)) {
377
+ this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
378
+ return;
379
+ }
380
+ if (is_string(key)) {
381
+ if (!value) {
382
+ this.__errors.push(
383
+ `[${caller}] if arg1 is a string, then arg2 must be passed`
384
+ );
385
+ return;
386
+ } else {
387
+ this._helper._append_kv(key, value, {}, caller);
388
+ this._collect_payload();
389
+ }
390
+ } else {
391
+ for (let item in key) {
392
+ this._helper._append_kv(item, key[item], key, caller);
393
+ }
394
+ this._collect_payload();
395
+ }
396
+ }
397
+
398
+ set(key, value) {
399
+ const caller = "set";
400
+ if (!is_string(key) && !is_object(key)) {
401
+ this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
402
+ return;
403
+ }
404
+ if (is_string(key)) {
405
+ if (value === null || value === undefined) {
406
+ this.__errors.push(
407
+ `[${caller}] if arg1 is a string, then arg2 must be passed`
408
+ );
409
+ return;
410
+ } else {
411
+ this._helper._set_kv(key, value, {}, caller);
412
+ this._collect_payload();
413
+ }
414
+ } else {
415
+ for (let item in key) {
416
+ this._helper._set_kv(item, key[item], key, caller);
417
+ }
418
+ this._collect_payload();
419
+ }
420
+ }
421
+
422
+ set_once(key, value) {
423
+ const caller = "set_once";
424
+ if (!is_string(key) && !is_object(key)) {
425
+ this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
426
+ return;
427
+ }
428
+ if (is_string(key)) {
429
+ if (value === null || value === undefined) {
430
+ this.__errors.push(
431
+ `[${caller}] if arg1 is a string, then arg2 must be passed`
432
+ );
433
+ return;
434
+ } else {
435
+ this._helper._set_once_kv(key, value, {}, caller);
436
+ this._collect_payload();
437
+ }
438
+ } else {
439
+ for (let item in key) {
440
+ this._helper._set_once_kv(item, key[item], key, caller);
441
+ }
442
+ this._collect_payload();
443
+ }
444
+ }
445
+
446
+ increment(key, value) {
447
+ const caller = "increment";
448
+ if (!is_string(key) && !is_object(key)) {
449
+ this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
450
+ return;
451
+ }
452
+ if (is_string(key)) {
453
+ if (value === null || value === undefined) {
454
+ this.__errors.push(
455
+ `[${caller}] if arg1 is a string, then arg2 must be passed`
456
+ );
457
+ return;
458
+ } else {
459
+ this._helper._increment_kv(key, value, {}, caller);
460
+ this._collect_payload();
461
+ }
462
+ } else {
463
+ for (let item in key) {
464
+ this._helper._increment_kv(item, key[item], key, caller);
465
+ }
466
+ this._collect_payload();
467
+ }
468
+ }
469
+
470
+ remove(key, value) {
471
+ const caller = "remove";
472
+ if (!is_string(key) && !is_object(key)) {
473
+ this.__errors.push(`[${caller}] arg1 must be either string or a dict`);
474
+ return;
475
+ }
476
+ if (is_string(key)) {
477
+ if (!value) {
478
+ this.__errors.push(
479
+ `[${caller}] if arg1 is a string, then arg2 must be passed`
480
+ );
481
+ return;
482
+ } else {
483
+ this._helper._remove_kv(key, value, {}, caller);
484
+ this._collect_payload();
485
+ }
486
+ } else {
487
+ for (let item in key) {
488
+ this._helper._remove_kv(item, key[item], key, caller);
489
+ }
490
+ this._collect_payload();
491
+ }
492
+ }
493
+
494
+ unset(key) {
495
+ const caller = "unset";
496
+ if (!is_string(key) && !Array.isArray(key)) {
497
+ this.__errors.push(`[${caller}] key must be either string or array`);
498
+ return;
499
+ }
500
+ if (is_string(key)) {
501
+ this._helper._unset_k(key, caller);
502
+ this._collect_payload();
503
+ } else {
504
+ for (let item of key) {
505
+ this._helper._unset_k(item, caller);
506
+ }
507
+ this._collect_payload();
508
+ }
509
+ }
510
+
511
+ set_preferred_language(lang_code) {
512
+ const caller = "set_preferred_language";
513
+ this._helper._set_preferred_language(lang_code, caller);
514
+ this._collect_payload();
515
+ }
516
+
517
+ set_timezone(timezone) {
518
+ const caller = "set_timezone";
519
+ this._helper._set_timezone(timezone, caller);
520
+ this._collect_payload();
521
+ }
522
+
523
+ add_email(email) {
524
+ const caller = "add_email";
525
+ this._helper._add_email(email, caller);
526
+ this._collect_payload();
527
+ }
528
+
529
+ remove_email(email) {
530
+ const caller = "remove_email";
531
+ this._helper._remove_email(email, caller);
532
+ this._collect_payload();
533
+ }
534
+
535
+ add_sms(mobile_no) {
536
+ const caller = "add_sms";
537
+ this._helper._add_sms(mobile_no, caller);
538
+ this._collect_payload();
539
+ }
540
+
541
+ remove_sms(mobile_no) {
542
+ const caller = "remove_sms";
543
+ this._helper._remove_sms(mobile_no, caller);
544
+ this._collect_payload();
545
+ }
546
+
547
+ add_whatsapp(mobile_no) {
548
+ const caller = "add_whatsapp";
549
+ this._helper._add_whatsapp(mobile_no, caller);
550
+ this._collect_payload();
551
+ }
552
+
553
+ remove_whatsapp(mobile_no) {
554
+ const caller = "remove_whatsapp";
555
+ this._helper._remove_whatsapp(mobile_no, caller);
556
+ this._collect_payload();
557
+ }
558
+
559
+ add_androidpush(push_token, provider = "fcm") {
560
+ const caller = "add_androidpush";
561
+ this._helper._add_androidpush(push_token, provider, caller);
562
+ this._collect_payload();
563
+ }
564
+
565
+ remove_androidpush(push_token, provider = "fcm") {
566
+ const caller = "remove_androidpush";
567
+ this._helper._remove_androidpush(push_token, provider, caller);
568
+ this._collect_payload();
569
+ }
570
+
571
+ add_iospush(push_token, provider = "apns") {
572
+ const caller = "add_iospush";
573
+ this._helper._add_iospush(push_token, provider, caller);
574
+ this._collect_payload();
575
+ }
576
+
577
+ remove_iospush(push_token, provider = "apns") {
578
+ const caller = "remove_iospush";
579
+ this._helper._remove_iospush(push_token, provider, caller);
580
+ this._collect_payload();
581
+ }
582
+
583
+ add_webpush(push_token, provider = "vapid") {
584
+ const caller = "add_webpush";
585
+ this._helper._add_webpush(push_token, provider, caller);
586
+ this._collect_payload();
587
+ }
588
+
589
+ remove_webpush(push_token, provider = "vapid") {
590
+ const caller = "remove_webpush";
591
+ this._helper._remove_webpush(push_token, provider, caller);
592
+ this._collect_payload();
593
+ }
594
+
595
+ add_slack(value) {
596
+ const caller = "add_slack";
597
+ this._helper._add_slack(value, caller);
598
+ this._collect_payload();
599
+ }
600
+
601
+ remove_slack(value) {
602
+ const caller = "remove_slack";
603
+ this._helper._remove_slack(value, caller);
604
+ this._collect_payload();
605
+ }
606
+
607
+ add_ms_teams(value) {
608
+ const caller = "add_ms_teams";
609
+ this._helper._add_ms_teams(value, caller);
610
+ this._collect_payload();
611
+ }
612
+
613
+ remove_ms_teams(value) {
614
+ const caller = "remove_ms_teams";
615
+ this._helper._remove_ms_teams(value, caller);
616
+ this._collect_payload();
617
+ }
618
+
619
+ }
620
+
621
+ export default ObjectsApi;