@suprsend/node-sdk 1.11.1 → 1.13.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.
Files changed (48) hide show
  1. package/dist/constants.js +2 -8
  2. package/dist/event.js +2 -2
  3. package/dist/events_bulk.js +2 -2
  4. package/dist/index.js +25 -2
  5. package/dist/object_edit.js +352 -0
  6. package/dist/object_edit_internal_helper.js +395 -0
  7. package/dist/objects_api.js +527 -0
  8. package/dist/request_json/workflow.json +7 -29
  9. package/dist/request_json/workflow_trigger.json +7 -29
  10. package/dist/subscriber.js +12 -28
  11. package/dist/subscriber_helper.js +40 -363
  12. package/dist/subscriber_list.js +2 -2
  13. package/dist/user_edit.js +412 -0
  14. package/dist/user_edit_internal_helper.js +363 -0
  15. package/dist/users_api.js +535 -0
  16. package/dist/users_edit_bulk.js +312 -0
  17. package/dist/workflow.js +2 -2
  18. package/dist/workflow_request.js +2 -2
  19. package/dist/workflow_trigger_bulk.js +2 -2
  20. package/dist/workflows_bulk.js +2 -2
  21. package/package.json +1 -1
  22. package/src/constants.js +0 -4
  23. package/src/event.js +4 -5
  24. package/src/events_bulk.js +3 -5
  25. package/src/index.js +9 -1
  26. package/src/object_edit.js +308 -0
  27. package/src/object_edit_internal_helper.js +332 -0
  28. package/src/objects_api.js +320 -0
  29. package/src/request_json/workflow.json +7 -29
  30. package/src/request_json/workflow_trigger.json +7 -29
  31. package/src/subscriber.js +10 -21
  32. package/src/subscriber_helper.js +41 -365
  33. package/src/subscriber_list.js +4 -4
  34. package/src/user_edit.js +360 -0
  35. package/src/user_edit_internal_helper.js +297 -0
  36. package/src/users_api.js +301 -0
  37. package/src/users_edit_bulk.js +211 -0
  38. package/src/workflow.js +4 -5
  39. package/src/workflow_request.js +4 -4
  40. package/src/workflow_trigger_bulk.js +2 -4
  41. package/src/workflows_bulk.js +3 -5
  42. package/types/index.d.ts +168 -13
  43. package/dist/language_codes.js +0 -197
  44. package/dist/object.js +0 -919
  45. package/dist/object_helper.js +0 -659
  46. package/src/language_codes.js +0 -188
  47. package/src/object.js +0 -621
  48. package/src/object_helper.js +0 -593
@@ -0,0 +1,320 @@
1
+ import { is_string, InputValueError, SuprsendApiError } from "./utils";
2
+ import get_request_signature from "./signature";
3
+ import axios from "axios";
4
+ import ObjectEdit from "./object_edit";
5
+
6
+ export default class ObjectsApi {
7
+ constructor(config) {
8
+ this.config = config;
9
+ this.list_url = `${this.config.base_url}v1/object/`;
10
+ this.bulk_url = `${this.config.base_url}v1/bulk/object/`;
11
+ }
12
+
13
+ __get_headers() {
14
+ return {
15
+ "Content-Type": "application/json; charset=utf-8",
16
+ "User-Agent": this.config.user_agent,
17
+ Date: new Date().toISOString(),
18
+ };
19
+ }
20
+
21
+ _validate_object_type(object_type) {
22
+ if (!object_type || !is_string(object_type) || !object_type.trim()) {
23
+ throw new InputValueError("missing object_type");
24
+ }
25
+ return object_type.trim();
26
+ }
27
+
28
+ _validate_object_id(object_id) {
29
+ if (!object_id || !is_string(object_id) || !object_id.trim()) {
30
+ throw new InputValueError("missing object_id");
31
+ }
32
+ return object_id.trim();
33
+ }
34
+
35
+ async list(object_type, options) {
36
+ object_type = this._validate_object_type(object_type);
37
+ const object_type_encoded = encodeURIComponent(object_type);
38
+ const encoded_options = options
39
+ ? new URLSearchParams(options).toString()
40
+ : "";
41
+
42
+ const url = `${this.list_url}${object_type_encoded}/${
43
+ encoded_options ? `?${encoded_options}` : ""
44
+ }`;
45
+ const headers = this.__get_headers();
46
+ const sig = get_request_signature(
47
+ url,
48
+ "GET",
49
+ "",
50
+ headers,
51
+ this.config.workspace_secret
52
+ );
53
+ headers["Authorization"] = `${this.config.workspace_key}:${sig}`;
54
+
55
+ try {
56
+ const response = await axios.get(url, { headers });
57
+ return response.data;
58
+ } catch (err) {
59
+ throw new SuprsendApiError(err);
60
+ }
61
+ }
62
+
63
+ detail_url(object_type, object_id) {
64
+ object_type = this._validate_object_type(object_type);
65
+ const object_type_encoded = encodeURIComponent(object_type);
66
+
67
+ object_id = this._validate_object_id(object_id);
68
+ const object_id_encoded = encodeURIComponent(object_id);
69
+
70
+ return `${this.list_url}${object_type_encoded}/${object_id_encoded}/`;
71
+ }
72
+
73
+ async get(object_type, object_id) {
74
+ const url = this.detail_url(object_type, object_id);
75
+ const headers = this.__get_headers();
76
+ const signature = get_request_signature(
77
+ url,
78
+ "GET",
79
+ "",
80
+ headers,
81
+ this.config.workspace_secret
82
+ );
83
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
84
+
85
+ try {
86
+ const response = await axios.get(url, { headers });
87
+ return response.data;
88
+ } catch (err) {
89
+ throw new SuprsendApiError(err);
90
+ }
91
+ }
92
+
93
+ async upsert(object_type, object_id, payload = {}) {
94
+ const url = this.detail_url(object_type, object_id);
95
+ payload = payload || {};
96
+ const headers = this.__get_headers();
97
+ const content_text = JSON.stringify(payload || {});
98
+ const signature = get_request_signature(
99
+ url,
100
+ "POST",
101
+ content_text,
102
+ headers,
103
+ this.config.workspace_secret
104
+ );
105
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
106
+
107
+ try {
108
+ const response = await axios.post(url, content_text, { headers });
109
+ return response.data;
110
+ } catch (err) {
111
+ throw new SuprsendApiError(err);
112
+ }
113
+ }
114
+
115
+ async edit(edit_ins_or_object_type, object_id, edit_payload) {
116
+ let payload, url;
117
+
118
+ if (edit_ins_or_object_type instanceof ObjectEdit) {
119
+ const edit_ins = edit_ins_or_object_type;
120
+ edit_ins.validate_body();
121
+ payload = edit_ins.get_payload();
122
+ url = this.detail_url(edit_ins.object_type, edit_ins.object_id);
123
+ } else {
124
+ const object_type = edit_ins_or_object_type;
125
+ payload = edit_payload || {};
126
+ url = this.detail_url(object_type, object_id);
127
+ }
128
+
129
+ const content_text = JSON.stringify(payload || {});
130
+ const headers = this.__get_headers();
131
+ const signature = get_request_signature(
132
+ url,
133
+ "PATCH",
134
+ content_text,
135
+ headers,
136
+ this.config.workspace_secret
137
+ );
138
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
139
+
140
+ try {
141
+ const response = await axios.patch(url, content_text, { headers });
142
+ return response.data;
143
+ } catch (err) {
144
+ throw new SuprsendApiError(err);
145
+ }
146
+ }
147
+
148
+ async delete(object_type, object_id) {
149
+ const url = this.detail_url(object_type, object_id);
150
+ const headers = this.__get_headers();
151
+ const signature = get_request_signature(
152
+ url,
153
+ "DELETE",
154
+ "",
155
+ headers,
156
+ this.config.workspace_secret
157
+ );
158
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
159
+
160
+ try {
161
+ const response = await axios.delete(url, { headers });
162
+ if (response.status >= 200 && response.status < 300) {
163
+ return { success: true, status_code: response.status };
164
+ } else {
165
+ throw new SuprsendApiError(response.statusText);
166
+ }
167
+ } catch (err) {
168
+ throw new SuprsendApiError(err);
169
+ }
170
+ }
171
+
172
+ async bulk_delete(object_type, payload) {
173
+ object_type = this._validate_object_type(object_type);
174
+ const object_type_encoded = encodeURIComponent(object_type);
175
+ const url = `${this.bulk_url}${object_type_encoded}/`;
176
+ payload = payload || {};
177
+ const headers = this.__get_headers();
178
+ const content_text = JSON.stringify(payload);
179
+ const signature = get_request_signature(
180
+ url,
181
+ "DELETE",
182
+ content_text,
183
+ headers,
184
+ this.config.workspace_secret
185
+ );
186
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
187
+
188
+ try {
189
+ const response = await axios.delete(url, {
190
+ headers: headers,
191
+ data: content_text,
192
+ });
193
+ if (response.status >= 200 && response.status < 300) {
194
+ return { success: true, status_code: response.status };
195
+ } else {
196
+ throw new SuprsendApiError(response.statusText);
197
+ }
198
+ } catch (err) {
199
+ throw new SuprsendApiError(err);
200
+ }
201
+ }
202
+
203
+ async get_subscriptions(object_type, object_id, options) {
204
+ const encoded_options = options
205
+ ? new URLSearchParams(options).toString()
206
+ : "";
207
+ const _detail_url = this.detail_url(object_type, object_id);
208
+ const url = `${_detail_url}subscription/${
209
+ encoded_options ? `?${encoded_options}` : ""
210
+ }`;
211
+ const headers = this.__get_headers();
212
+ const signature = get_request_signature(
213
+ url,
214
+ "GET",
215
+ "",
216
+ headers,
217
+ this.config.workspace_secret
218
+ );
219
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
220
+
221
+ try {
222
+ const response = await axios.get(url, { headers });
223
+ return response.data;
224
+ } catch (err) {
225
+ throw new SuprsendApiError(err);
226
+ }
227
+ }
228
+
229
+ async create_subscriptions(object_type, object_id, payload) {
230
+ const _detail_url = this.detail_url(object_type, object_id);
231
+ const url = `${_detail_url}subscription/`;
232
+ payload = payload || {};
233
+ const content_text = JSON.stringify(payload);
234
+ const headers = this.__get_headers();
235
+
236
+ const signature = get_request_signature(
237
+ url,
238
+ "POST",
239
+ content_text,
240
+ headers,
241
+ this.config.workspace_secret
242
+ );
243
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
244
+
245
+ try {
246
+ const response = await axios.post(url, content_text, { headers });
247
+ return response.data;
248
+ } catch (err) {
249
+ throw new SuprsendApiError(err);
250
+ }
251
+ }
252
+
253
+ async delete_subscriptions(object_type, object_id, payload) {
254
+ const _detail_url = this.detail_url(object_type, object_id);
255
+ const url = `${_detail_url}subscription/`;
256
+ payload = payload || {};
257
+ const content_text = JSON.stringify(payload);
258
+ const headers = this.__get_headers();
259
+ const signature = get_request_signature(
260
+ url,
261
+ "DELETE",
262
+ content_text,
263
+ headers,
264
+ this.config.workspace_secret
265
+ );
266
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
267
+
268
+ try {
269
+ const response = await axios.delete(url, {
270
+ headers: headers,
271
+ data: content_text,
272
+ });
273
+ if (response.status >= 200 && response.status < 300) {
274
+ return { success: true, status_code: response.status };
275
+ } else {
276
+ throw new SuprsendApiError(response.statusText);
277
+ }
278
+ } catch (err) {
279
+ throw new SuprsendApiError(err);
280
+ }
281
+ }
282
+
283
+ async get_objects_subscribed_to(object_type, object_id, options = null) {
284
+ const encoded_options = options
285
+ ? new URLSearchParams(options).toString()
286
+ : "";
287
+ const _detail_url = this.detail_url(object_type, object_id);
288
+ const url = `${_detail_url}subscribed_to/object/${
289
+ encoded_options ? `?${encoded_options}` : ""
290
+ }`;
291
+ const headers = this.__get_headers();
292
+ const signature = get_request_signature(
293
+ url,
294
+ "GET",
295
+ "",
296
+ headers,
297
+ this.config.workspace_secret
298
+ );
299
+ headers["Authorization"] = `${this.config.workspace_key}:${signature}`;
300
+
301
+ try {
302
+ const response = await axios.get(url, { headers });
303
+ return response.data;
304
+ } catch (err) {
305
+ throw new SuprsendApiError(err);
306
+ }
307
+ }
308
+
309
+ get_instance(object_type, object_id) {
310
+ object_type = this._validate_object_type(object_type);
311
+ object_id = this._validate_object_id(object_id);
312
+ return new ObjectEdit(this.config, object_type, object_id);
313
+ }
314
+
315
+ get_edit_instance(object_type, object_id) {
316
+ object_type = this._validate_object_type(object_type);
317
+ object_id = this._validate_object_id(object_id);
318
+ return new ObjectEdit(this.config, object_type, object_id);
319
+ }
320
+ }
@@ -51,7 +51,6 @@
51
51
  "type": "array",
52
52
  "items": { "$ref": "#/definitions/user_setting" },
53
53
  "minItems": 1,
54
- "maxItems": 100,
55
54
  "description": "user object to run workflow for. At least 1 user is required"
56
55
  },
57
56
  "data": {
@@ -65,27 +64,6 @@
65
64
  "type": "string",
66
65
  "minLength": 2
67
66
  },
68
- "mobile_number_pattern": {
69
- "type": "string",
70
- "minLength": 8,
71
- "pattern": "^\\+[0-9\\s]+",
72
- "message": {
73
- "required": "Either a mobile-number or an array of mobile-numbers. e.g [\"+41446681800\"]",
74
- "pattern": "number must start with + and must contain country code. e.g. +41446681800"
75
- }
76
- },
77
- "email_pattern": {
78
- "type": "string",
79
- "format": "email",
80
- "pattern": "^\\S+@\\S+\\.\\S+$",
81
- "description": "email of an user",
82
- "minLength": 6,
83
- "maxLength": 127,
84
- "message": {
85
- "required": "",
86
- "pattern": "value in email format required. e.g. user@example.com"
87
- }
88
- },
89
67
  "slack_setting": {
90
68
  "type": "object",
91
69
  "properties": {
@@ -113,7 +91,7 @@
113
91
  }
114
92
  },
115
93
  "email": {
116
- "$ref": "#/definitions/email_pattern"
94
+ "$ref": "#/definitions/non_empty_string"
117
95
  },
118
96
  "incoming_webhook": {
119
97
  "type": "object",
@@ -240,25 +218,25 @@
240
218
  },
241
219
  "$email": {
242
220
  "oneOf": [
243
- { "$ref": "#/definitions/email_pattern" },
221
+ { "$ref": "#/definitions/non_empty_string" },
244
222
  {
245
223
  "type": "array",
246
224
  "uniqueItems": false,
247
225
  "maxItems": 10,
248
226
  "minItems": 1,
249
- "items": { "$ref": "#/definitions/email_pattern" }
227
+ "items": { "$ref": "#/definitions/non_empty_string" }
250
228
  }
251
229
  ]
252
230
  },
253
231
  "$sms": {
254
232
  "oneOf": [
255
- { "$ref": "#/definitions/mobile_number_pattern" },
233
+ { "$ref": "#/definitions/non_empty_string" },
256
234
  {
257
235
  "type": "array",
258
236
  "uniqueItems": false,
259
237
  "maxItems": 10,
260
238
  "minItems": 1,
261
- "items": { "$ref": "#/definitions/mobile_number_pattern" }
239
+ "items": { "$ref": "#/definitions/non_empty_string" }
262
240
  }
263
241
  ]
264
242
  },
@@ -304,13 +282,13 @@
304
282
  },
305
283
  "$whatsapp": {
306
284
  "oneOf": [
307
- { "$ref": "#/definitions/mobile_number_pattern" },
285
+ { "$ref": "#/definitions/non_empty_string" },
308
286
  {
309
287
  "type": "array",
310
288
  "uniqueItems": false,
311
289
  "maxItems": 10,
312
290
  "minItems": 1,
313
- "items": { "$ref": "#/definitions/mobile_number_pattern" }
291
+ "items": { "$ref": "#/definitions/non_empty_string" }
314
292
  }
315
293
  ]
316
294
  },
@@ -36,7 +36,6 @@
36
36
  ]
37
37
  },
38
38
  "minItems": 1,
39
- "maxItems": 100,
40
39
  "description": "list of recipient id/object to run workflow for. At least 1 user is required"
41
40
  },
42
41
  "data": {
@@ -60,27 +59,6 @@
60
59
  "type": "string",
61
60
  "minLength": 1
62
61
  },
63
- "mobile_number_pattern": {
64
- "type": "string",
65
- "minLength": 8,
66
- "pattern": "^\\+[0-9\\s]+",
67
- "message": {
68
- "required": "Either a mobile-number or an array of mobile-numbers. e.g [\"+41446681800\"]",
69
- "pattern": "number must start with + and must contain country code. e.g. +41446681800"
70
- }
71
- },
72
- "email_pattern": {
73
- "type": "string",
74
- "format": "email",
75
- "pattern": "^\\S+@\\S+\\.\\S+$",
76
- "description": "email of an user",
77
- "minLength": 6,
78
- "maxLength": 127,
79
- "message": {
80
- "required": "",
81
- "pattern": "value in email format required. e.g. user@example.com"
82
- }
83
- },
84
62
  "slack_setting": {
85
63
  "type": "object",
86
64
  "properties": {
@@ -108,7 +86,7 @@
108
86
  }
109
87
  },
110
88
  "email": {
111
- "$ref": "#/definitions/email_pattern"
89
+ "$ref": "#/definitions/non_empty_string"
112
90
  },
113
91
  "incoming_webhook": {
114
92
  "type": "object",
@@ -235,25 +213,25 @@
235
213
  },
236
214
  "$email": {
237
215
  "oneOf": [
238
- { "$ref": "#/definitions/email_pattern" },
216
+ { "$ref": "#/definitions/non_empty_string" },
239
217
  {
240
218
  "type": "array",
241
219
  "uniqueItems": false,
242
220
  "maxItems": 10,
243
221
  "minItems": 1,
244
- "items": { "$ref": "#/definitions/email_pattern" }
222
+ "items": { "$ref": "#/definitions/non_empty_string" }
245
223
  }
246
224
  ]
247
225
  },
248
226
  "$sms": {
249
227
  "oneOf": [
250
- { "$ref": "#/definitions/mobile_number_pattern" },
228
+ { "$ref": "#/definitions/non_empty_string" },
251
229
  {
252
230
  "type": "array",
253
231
  "uniqueItems": false,
254
232
  "maxItems": 10,
255
233
  "minItems": 1,
256
- "items": { "$ref": "#/definitions/mobile_number_pattern" }
234
+ "items": { "$ref": "#/definitions/non_empty_string" }
257
235
  }
258
236
  ]
259
237
  },
@@ -283,13 +261,13 @@
283
261
  },
284
262
  "$whatsapp": {
285
263
  "oneOf": [
286
- { "$ref": "#/definitions/mobile_number_pattern" },
264
+ { "$ref": "#/definitions/non_empty_string" },
287
265
  {
288
266
  "type": "array",
289
267
  "uniqueItems": false,
290
268
  "maxItems": 10,
291
269
  "minItems": 1,
292
- "items": { "$ref": "#/definitions/mobile_number_pattern" }
270
+ "items": { "$ref": "#/definitions/non_empty_string" }
293
271
  }
294
272
  ]
295
273
  },
package/src/subscriber.js CHANGED
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  is_object,
3
- SuprsendError,
4
3
  epoch_milliseconds,
5
4
  uuid,
6
5
  is_empty,
@@ -44,8 +43,7 @@ export class Subscriber {
44
43
  constructor(config, distinct_id) {
45
44
  this.config = config;
46
45
  this.distinct_id = distinct_id;
47
- this.__url = this.__get_url();
48
- this.__super_props = this.__super_properties();
46
+ this.__url = `${this.config.base_url}event/`;
49
47
 
50
48
  this.__errors = [];
51
49
  this.__info = [];
@@ -54,10 +52,6 @@ export class Subscriber {
54
52
  this.__warnings_list = [];
55
53
  }
56
54
 
57
- __get_url() {
58
- return `${this.config.base_url}event/`;
59
- }
60
-
61
55
  __get_headers() {
62
56
  return {
63
57
  "Content-Type": "application/json; charset=utf-8",
@@ -66,12 +60,6 @@ export class Subscriber {
66
60
  };
67
61
  }
68
62
 
69
- __super_properties() {
70
- return {
71
- $ss_sdk_version: this.config.user_agent,
72
- };
73
- }
74
-
75
63
  get_events() {
76
64
  return {
77
65
  $schema: "2",
@@ -80,7 +68,7 @@ export class Subscriber {
80
68
  env: this.config.workspace_key,
81
69
  distinct_id: this.distinct_id,
82
70
  $user_operations: this.user_operations,
83
- properties: this.__super_props,
71
+ properties: { $ss_sdk_version: this.config.user_agent },
84
72
  };
85
73
  }
86
74
 
@@ -120,7 +108,8 @@ export class Subscriber {
120
108
  if (is_part_of_bulk) {
121
109
  console.log(err_msg);
122
110
  } else {
123
- throw new InputValueError(err_msg);
111
+ // throw new InputValueError(err_msg);
112
+ console.log(err_msg);
124
113
  }
125
114
  }
126
115
  return this.__warnings_list;
@@ -369,37 +358,37 @@ export class Subscriber {
369
358
  this._collect_event();
370
359
  }
371
360
 
372
- add_androidpush(push_token, provider = "fcm") {
361
+ add_androidpush(push_token, provider) {
373
362
  const caller = "add_androidpush";
374
363
  this._helper._add_androidpush(push_token, provider, caller);
375
364
  this._collect_event();
376
365
  }
377
366
 
378
- remove_androidpush(push_token, provider = "fcm") {
367
+ remove_androidpush(push_token, provider) {
379
368
  const caller = "remove_androidpush";
380
369
  this._helper._remove_androidpush(push_token, provider, caller);
381
370
  this._collect_event();
382
371
  }
383
372
 
384
- add_iospush(push_token, provider = "apns") {
373
+ add_iospush(push_token, provider) {
385
374
  const caller = "add_iospush";
386
375
  this._helper._add_iospush(push_token, provider, caller);
387
376
  this._collect_event();
388
377
  }
389
378
 
390
- remove_iospush(push_token, provider = "apns") {
379
+ remove_iospush(push_token, provider) {
391
380
  const caller = "remove_iospush";
392
381
  this._helper._remove_iospush(push_token, provider, caller);
393
382
  this._collect_event();
394
383
  }
395
384
 
396
- add_webpush(push_token, provider = "vapid") {
385
+ add_webpush(push_token, provider) {
397
386
  const caller = "add_webpush";
398
387
  this._helper._add_webpush(push_token, provider, caller);
399
388
  this._collect_event();
400
389
  }
401
390
 
402
- remove_webpush(push_token, provider = "vapid") {
391
+ remove_webpush(push_token, provider) {
403
392
  const caller = "remove_webpush";
404
393
  this._helper._remove_webpush(push_token, provider, caller);
405
394
  this._collect_event();