@suprsend/node-sdk 1.0.0 → 1.1.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/README.md CHANGED
@@ -3,109 +3,20 @@ This package can be included in a node project to easily integrate with `Suprsen
3
3
 
4
4
  Refer full documentation [here](https://docs.suprsend.com/docs/node)
5
5
 
6
- We're working towards creating SDK in other languages as well.
7
-
8
- ### Suprsend SDKs available in following languages
9
- * node (`suprsend-node-sdk`)
10
- * python3 >= 3.7 (`suprsend-py-sdk`)
11
-
12
6
  ### Installation
13
7
  `suprsend-node-sdk` is available as npm package. You can install using npm or yarn.
14
-
15
- Using npm:
16
8
  ```bash
17
9
  npm install @suprsend/node-sdk
18
10
  ```
19
- Using yarn:
20
- ```bash
21
- yarn add @suprsend/node-sdk
22
- ```
23
11
 
24
- ### Usage
12
+ ### Initialization
25
13
  Initialize the Suprsend SDK
26
14
  ```node
27
- const Suprsend = require("@suprsend/node-sdk");
15
+ const {Suprsend} = require("@suprsend/node-sdk");
28
16
 
29
17
  // Initialize SDK
30
- const supr_client = new Suprsend("env_key", "env_secret");
31
- ```
32
-
33
- Following example shows a sample request for triggering a workflow.
34
- It triggers a notification to a user with id: `distinct_id`,
35
- email: `user@example.com` & androidpush-token: `__android_push_token__`
36
- using template `purchase-made` and notification_category `system`
37
-
38
- ```node
39
- // Prepare Workflow body
40
- const workflow_body = {
41
- "name": "Purchase Workflow",
42
- "template": "purchase-made",
43
- "notification_category": "system",
44
- "delay": "15m",
45
- "users": [
46
- {
47
- "distinct_id": "0f988f74-6982-41c5-8752-facb6911fb08",
48
- "$email": ["user@example.com"],
49
- "$androidpush": ["__android_push_token__"],
50
- }
51
- ],
52
- "data": {
53
- "template": {
54
- "first_name": "User",
55
- "spend_amount": "$10"
56
- },
57
- }
58
- }
59
-
60
- // Trigger workflow
61
- const response = supr_client.trigger_workflow(workflow_body); // returns promise
62
- response.then((res) => console.log("response", res));
63
- ```
64
-
65
- When you call `supr_client.trigger_workflow`, the SDK internally makes an HTTP call to SuprSend
66
- Platform to register this request, and you'll receive a promise which resolve to response indicating
67
- the acceptance status.
68
-
69
- Note: The actual processing/execution of workflow happens asynchronously.
70
-
71
- ```node
72
- // If the call succeeds, response will looks like:
73
- {
74
- "success": true,
75
- "status_code": 202,
76
- "message": "Accepted",
77
- }
78
-
79
- // In case the call fails. You will receive a response with success=false
80
- {
81
- "success": false,
82
- "status": 400,
83
- "message": "error message",
84
- }
18
+ const supr_client = new Suprsend("workspace_key", "workspace_secret");
85
19
  ```
86
20
 
87
- ### Add attachments
88
-
89
- To add one or more Attachments to a Notification (viz. Email, Whatsapp),
90
- call `supr_client.add_attachment(...)` for each file.
91
- Ensure that file_path is proper, otherwise it will raise error.
92
- ```node
93
- // this snippet can be used to add attachment to workflow_body.
94
- const file_path = "/home/user/billing.pdf"
95
- supr_client.add_attachment(workflow_body, file_path);
96
- ```
97
-
98
- #### Attachment structure
99
- The `add_attachment(...)` call appends below structure to `data->'$attachments'`
100
-
101
- ```json
102
- {
103
- "filename": "billing.pdf",
104
- "contentType": "application/pdf",
105
- "data": "Q29uZ3JhdHVsYXRpb25zLCB5b3UgY2FuIGJhc2U2NCBkZWNvZGUh",
106
- }
107
- ```
108
- Where
109
- * `filename` - name of file.
110
- * `contentType` - MIME-type of file content.
111
- * `data` - base64-encoded content of file.
21
+ ### License
22
+ MIT © [https://github.com/suprsend](https://github.com/https://github.com/suprsend)
@@ -5,7 +5,7 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports["default"] = get_attachment_json_for_file;
8
+ exports["default"] = get_attachment_json;
9
9
 
10
10
  var _path = _interopRequireDefault(require("path"));
11
11
 
@@ -13,14 +13,47 @@ var _mimeTypes = _interopRequireDefault(require("mime-types"));
13
13
 
14
14
  var _utils = require("./utils");
15
15
 
16
- function get_attachment_json_for_file(file_path) {
16
+ function check_is_web_url() {
17
+ var file_path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
18
+ return file_path.startsWith("http://") || file_path.startsWith("https://");
19
+ }
20
+
21
+ function get_attachment_json_for_file(file_path, file_name, ignore_if_error) {
17
22
  var abs_path = _path["default"].resolve((0, _utils.resolveTilde)(file_path));
18
23
 
24
+ var final_file_name = _path["default"].basename(abs_path);
25
+
26
+ if (file_name && file_name.trim()) {
27
+ final_file_name = file_name.trim();
28
+ }
29
+
19
30
  return {
20
- filename: _path["default"].basename(abs_path),
31
+ filename: final_file_name,
21
32
  contentType: _mimeTypes["default"].lookup(abs_path),
22
- data: (0, _utils.base64Encode)(abs_path)
33
+ data: (0, _utils.base64Encode)(abs_path),
34
+ url: null,
35
+ ignore_if_error: ignore_if_error
23
36
  };
24
37
  }
25
38
 
39
+ function get_attachment_json_for_url(file_url, file_name, ignore_if_error) {
40
+ return {
41
+ filename: file_name,
42
+ contentType: null,
43
+ data: null,
44
+ url: file_url,
45
+ ignore_if_error: ignore_if_error
46
+ };
47
+ }
48
+
49
+ function get_attachment_json(file_path, file_name) {
50
+ var ignore_if_error = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
51
+
52
+ if (check_is_web_url(file_path)) {
53
+ return get_attachment_json_for_url(file_path, file_name, ignore_if_error);
54
+ } else {
55
+ return get_attachment_json_for_file(file_path, file_name, ignore_if_error);
56
+ }
57
+ }
58
+
26
59
  module.exports = exports.default;
package/dist/constants.js CHANGED
@@ -3,13 +3,19 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.WORKFLOW_RUNTIME_KEYS_POTENTIAL_SIZE_IN_BYTES = exports.MAX_WORKFLOWS_IN_BULK_API = exports.MAX_IDENTITY_EVENTS_IN_BULK_API = exports.MAX_EVENTS_IN_BULK_API = exports.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE = exports.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES = exports.DEFAULT_URL = exports.DEFAULT_UAT_URL = exports.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE = exports.BODY_MAX_APPARENT_SIZE_IN_BYTES = exports.ATTACHMENT_URL_POTENTIAL_SIZE_IN_BYTES = exports.ATTACHMENT_UPLOAD_ENABLED = exports.ALLOW_ATTACHMENTS_IN_BULK_API = void 0;
6
+ exports.WORKFLOW_RUNTIME_KEYS_POTENTIAL_SIZE_IN_BYTES = exports.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE = exports.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES = exports.MAX_WORKFLOWS_IN_BULK_API = exports.MAX_IDENTITY_EVENTS_IN_BULK_API = exports.MAX_EVENTS_IN_BULK_API = exports.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE = exports.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES = exports.DEFAULT_URL = exports.DEFAULT_UAT_URL = exports.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE = exports.BODY_MAX_APPARENT_SIZE_IN_BYTES = exports.ATTACHMENT_URL_POTENTIAL_SIZE_IN_BYTES = exports.ATTACHMENT_UPLOAD_ENABLED = exports.ALLOW_ATTACHMENTS_IN_BULK_API = void 0;
7
7
  // Default urls
8
8
  var DEFAULT_URL = "https://hub.suprsend.com/";
9
9
  exports.DEFAULT_URL = DEFAULT_URL;
10
- var DEFAULT_UAT_URL = "https://collector-staging.suprsend.workers.dev/"; // a API call should not have apparent body size of more than 800KB
10
+ var DEFAULT_UAT_URL = "https://collector-staging.suprsend.workers.dev/"; // an Event should not have apparent body size of more than 100KB
11
11
 
12
12
  exports.DEFAULT_UAT_URL = DEFAULT_UAT_URL;
13
+ var SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES = 100 * 1024; // 100 * 1024
14
+
15
+ exports.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES = SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES;
16
+ var SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE = "100KB"; // a API call should not have apparent body size of more than 800KB
17
+
18
+ exports.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE = SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE;
13
19
  var BODY_MAX_APPARENT_SIZE_IN_BYTES = 800 * 1024; // 800 * 1024
14
20
 
15
21
  exports.BODY_MAX_APPARENT_SIZE_IN_BYTES = BODY_MAX_APPARENT_SIZE_IN_BYTES;
@@ -27,7 +33,7 @@ var MAX_WORKFLOWS_IN_BULK_API = 100; // max event-records in one bulk api call
27
33
  exports.MAX_WORKFLOWS_IN_BULK_API = MAX_WORKFLOWS_IN_BULK_API;
28
34
  var MAX_EVENTS_IN_BULK_API = 100;
29
35
  exports.MAX_EVENTS_IN_BULK_API = MAX_EVENTS_IN_BULK_API;
30
- var ALLOW_ATTACHMENTS_IN_BULK_API = false;
36
+ var ALLOW_ATTACHMENTS_IN_BULK_API = true;
31
37
  exports.ALLOW_ATTACHMENTS_IN_BULK_API = ALLOW_ATTACHMENTS_IN_BULK_API;
32
38
  var ATTACHMENT_UPLOAD_ENABLED = false; // -- single Identity event limit
33
39
 
package/dist/event.js CHANGED
@@ -36,12 +36,13 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
36
36
  var RESERVED_EVENT_NAMES = ["$identify", "$notification_delivered", "$notification_dismiss", "$notification_clicked", "$app_launched", "$user_login", "$user_logout"];
37
37
 
38
38
  var Event = /*#__PURE__*/function () {
39
- function Event(distinct_id, event_name, properties, idempotency_key) {
39
+ function Event(distinct_id, event_name, properties) {
40
+ var kwargs = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
40
41
  (0, _classCallCheck2["default"])(this, Event);
41
42
  this.distinct_id = distinct_id;
42
43
  this.event_name = event_name;
43
44
  this.properties = properties;
44
- this.idempotency_key = idempotency_key; // --- validate
45
+ this.idempotency_key = kwargs === null || kwargs === void 0 ? void 0 : kwargs.idempotency_key; // --- validate
45
46
 
46
47
  this.__validate_distinct_id();
47
48
 
@@ -101,7 +102,12 @@ var Event = /*#__PURE__*/function () {
101
102
  }, {
102
103
  key: "add_attachment",
103
104
  value: function add_attachment(file_path) {
104
- var attachment = (0, _attachment["default"])(file_path); // --- add the attachment to properties->$attachments
105
+ var _kwargs$ignore_if_err;
106
+
107
+ var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
108
+ var file_name = kwargs === null || kwargs === void 0 ? void 0 : kwargs.file_name;
109
+ var ignore_if_error = (_kwargs$ignore_if_err = kwargs === null || kwargs === void 0 ? void 0 : kwargs.ignore_if_error) !== null && _kwargs$ignore_if_err !== void 0 ? _kwargs$ignore_if_err : false;
110
+ var attachment = (0, _attachment["default"])(file_path, file_name, ignore_if_error); // --- add the attachment to properties->$attachments
105
111
 
106
112
  if (!this.properties["$attachments"]) {
107
113
  this.properties["$attachments"] = [];
@@ -132,8 +138,8 @@ var Event = /*#__PURE__*/function () {
132
138
  event_dict = (0, _utils.validate_track_event_schema)(event_dict);
133
139
  var apparent_size = (0, _utils.get_apparent_event_size)(event_dict, is_part_of_bulk);
134
140
 
135
- if (apparent_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
136
- throw new _utils.SuprsendError("Event properties too big - ".concat(apparent_size, " Bytes,must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
141
+ if (apparent_size > _constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
142
+ throw new _utils.SuprsendError("Event size too big - ".concat(apparent_size, " Bytes,must not cross ").concat(_constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
137
143
  }
138
144
 
139
145
  return [event_dict, apparent_size];
@@ -133,8 +133,8 @@ var _BulkEventsChunk = /*#__PURE__*/function () {
133
133
  return false;
134
134
  }
135
135
 
136
- if (event_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
137
- throw new _utils.SuprsendError("workflow body (discounting attachment if any) too big - ".concat(event_size, " Bytes, must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
136
+ if (event_size > _constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
137
+ throw new _utils.SuprsendError("Event properties too big - ".concat(event_size, " Bytes, must not cross ").concat(_constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
138
138
  }
139
139
 
140
140
  if (this.__running_size + event_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
package/dist/index.js CHANGED
@@ -127,6 +127,12 @@ var Suprsend = /*#__PURE__*/function () {
127
127
  }, {
128
128
  key: "add_attachment",
129
129
  value: function add_attachment(body, file_path) {
130
+ var _kwargs$ignore_if_err;
131
+
132
+ var kwargs = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
133
+ var file_name = kwargs === null || kwargs === void 0 ? void 0 : kwargs.file_name;
134
+ var ignore_if_error = (_kwargs$ignore_if_err = kwargs === null || kwargs === void 0 ? void 0 : kwargs.ignore_if_error) !== null && _kwargs$ignore_if_err !== void 0 ? _kwargs$ignore_if_err : false;
135
+
130
136
  if (!body.data) {
131
137
  body.data = {};
132
138
  }
@@ -135,7 +141,7 @@ var Suprsend = /*#__PURE__*/function () {
135
141
  throw new _utils.SuprsendError("data must be an object");
136
142
  }
137
143
 
138
- var attachment = (0, _attachment["default"])(file_path);
144
+ var attachment = (0, _attachment["default"])(file_path, file_name, ignore_if_error);
139
145
 
140
146
  if (!body.data["$attachments"]) {
141
147
  body["data"]["$attachments"] = [];
@@ -161,8 +167,8 @@ var Suprsend = /*#__PURE__*/function () {
161
167
  key: "track",
162
168
  value: function track(distinct_id, event_name) {
163
169
  var properties = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
164
- var idempotency_key = arguments.length > 3 ? arguments[3] : undefined;
165
- var event = new _event["default"](distinct_id, event_name, properties, idempotency_key);
170
+ var kwargs = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
171
+ var event = new _event["default"](distinct_id, event_name, properties, kwargs);
166
172
  return this._eventcollector.collect(event);
167
173
  }
168
174
  }, {
@@ -0,0 +1,200 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports["default"] = void 0;
9
+
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
11
+
12
+ var LANG_CODE_NAME_MAPPING = {
13
+ aa: "Afar",
14
+ ab: "Abkhazian",
15
+ ae: "Avestan",
16
+ af: "Afrikaans",
17
+ ak: "Akan",
18
+ am: "Amharic",
19
+ an: "Aragonese",
20
+ ar: "Arabic",
21
+ as: "Assamese",
22
+ av: "Avaric",
23
+ ay: "Aymara",
24
+ az: "Azerbaijani",
25
+ ba: "Bashkir",
26
+ be: "Belarusian",
27
+ bg: "Bulgarian",
28
+ bi: "Bislama",
29
+ bm: "Bambara",
30
+ bn: "Bengali",
31
+ bo: "Tibetan",
32
+ br: "Breton",
33
+ bs: "Bosnian",
34
+ ca: "Catalan; Valencian",
35
+ ce: "Chechen",
36
+ ch: "Chamorro",
37
+ co: "Corsican",
38
+ cr: "Cree",
39
+ cs: "Czech",
40
+ cu: "Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic",
41
+ cv: "Chuvash",
42
+ cy: "Welsh",
43
+ da: "Danish",
44
+ de: "German",
45
+ dv: "Divehi; Dhivehi; Maldivian",
46
+ dz: "Dzongkha",
47
+ ee: "Ewe",
48
+ el: "Greek, Modern (1453-)",
49
+ en: "English",
50
+ eo: "Esperanto",
51
+ es: "Spanish; Castilian",
52
+ et: "Estonian",
53
+ eu: "Basque",
54
+ fa: "Persian",
55
+ ff: "Fulah",
56
+ fi: "Finnish",
57
+ fj: "Fijian",
58
+ fo: "Faroese",
59
+ fr: "French",
60
+ fy: "Western Frisian",
61
+ ga: "Irish",
62
+ gd: "Gaelic; Scottish Gaelic",
63
+ gl: "Galician",
64
+ gn: "Guarani",
65
+ gu: "Gujarati",
66
+ gv: "Manx",
67
+ ha: "Hausa",
68
+ he: "Hebrew",
69
+ hi: "Hindi",
70
+ ho: "Hiri Motu",
71
+ hr: "Croatian",
72
+ ht: "Haitian; Haitian Creole",
73
+ hu: "Hungarian",
74
+ hy: "Armenian",
75
+ hz: "Herero",
76
+ ia: "Interlingua (International Auxiliary Language Association)",
77
+ id: "Indonesian",
78
+ ie: "Interlingue; Occidental",
79
+ ig: "Igbo",
80
+ ii: "Sichuan Yi; Nuosu",
81
+ ik: "Inupiaq",
82
+ io: "Ido",
83
+ is: "Icelandic",
84
+ it: "Italian",
85
+ iu: "Inuktitut",
86
+ ja: "Japanese",
87
+ jv: "Javanese",
88
+ ka: "Georgian",
89
+ kg: "Kongo",
90
+ ki: "Kikuyu; Gikuyu",
91
+ kj: "Kuanyama; Kwanyama",
92
+ kk: "Kazakh",
93
+ kl: "Kalaallisut; Greenlandic",
94
+ km: "Central Khmer",
95
+ kn: "Kannada",
96
+ ko: "Korean",
97
+ kr: "Kanuri",
98
+ ks: "Kashmiri",
99
+ ku: "Kurdish",
100
+ kv: "Komi",
101
+ kw: "Cornish",
102
+ ky: "Kirghiz; Kyrgyz",
103
+ la: "Latin",
104
+ lb: "Luxembourgish; Letzeburgesch",
105
+ lg: "Ganda",
106
+ li: "Limburgan; Limburger; Limburgish",
107
+ ln: "Lingala",
108
+ lo: "Lao",
109
+ lt: "Lithuanian",
110
+ lu: "Luba-Katanga",
111
+ lv: "Latvian",
112
+ mg: "Malagasy",
113
+ mh: "Marshallese",
114
+ mi: "Maori",
115
+ mk: "Macedonian",
116
+ ml: "Malayalam",
117
+ mn: "Mongolian",
118
+ mr: "Marathi",
119
+ ms: "Malay",
120
+ mt: "Maltese",
121
+ my: "Burmese",
122
+ na: "Nauru",
123
+ nb: "Bokmål, Norwegian; Norwegian Bokmål",
124
+ nd: "Ndebele, North; North Ndebele",
125
+ ne: "Nepali",
126
+ ng: "Ndonga",
127
+ nl: "Dutch; Flemish",
128
+ nn: "Norwegian Nynorsk; Nynorsk, Norwegian",
129
+ no: "Norwegian",
130
+ nr: "Ndebele, South; South Ndebele",
131
+ nv: "Navajo; Navaho",
132
+ ny: "Chichewa; Chewa; Nyanja",
133
+ oc: "Occitan (post 1500)",
134
+ oj: "Ojibwa",
135
+ om: "Oromo",
136
+ or: "Oriya",
137
+ os: "Ossetian; Ossetic",
138
+ pa: "Panjabi; Punjabi",
139
+ pi: "Pali",
140
+ pl: "Polish",
141
+ ps: "Pushto; Pashto",
142
+ pt: "Portuguese",
143
+ qu: "Quechua",
144
+ rm: "Romansh",
145
+ rn: "Rundi",
146
+ ro: "Romanian; Moldavian; Moldovan",
147
+ ru: "Russian",
148
+ rw: "Kinyarwanda",
149
+ sa: "Sanskrit",
150
+ sc: "Sardinian",
151
+ sd: "Sindhi",
152
+ se: "Northern Sami",
153
+ sg: "Sango",
154
+ si: "Sinhala; Sinhalese",
155
+ sk: "Slovak",
156
+ sl: "Slovenian",
157
+ sm: "Samoan",
158
+ sn: "Shona",
159
+ so: "Somali",
160
+ sq: "Albanian",
161
+ sr: "Serbian",
162
+ ss: "Swati",
163
+ st: "Sotho, Southern",
164
+ su: "Sundanese",
165
+ sv: "Swedish",
166
+ sw: "Swahili",
167
+ ta: "Tamil",
168
+ te: "Telugu",
169
+ tg: "Tajik",
170
+ th: "Thai",
171
+ ti: "Tigrinya",
172
+ tk: "Turkmen",
173
+ tl: "Tagalog",
174
+ tn: "Tswana",
175
+ to: "Tonga (Tonga Islands)",
176
+ tr: "Turkish",
177
+ ts: "Tsonga",
178
+ tt: "Tatar",
179
+ tw: "Twi",
180
+ ty: "Tahitian",
181
+ ug: "Uighur; Uyghur",
182
+ uk: "Ukrainian",
183
+ ur: "Urdu",
184
+ uz: "Uzbek",
185
+ ve: "Venda",
186
+ vi: "Vietnamese",
187
+ vo: "Volapük",
188
+ wa: "Walloon",
189
+ wo: "Wolof",
190
+ xh: "Xhosa",
191
+ yi: "Yiddish",
192
+ yo: "Yoruba",
193
+ za: "Zhuang; Chuang",
194
+ zh: "Chinese",
195
+ zu: "Zulu"
196
+ };
197
+ var ALL_LANG_CODES = (0, _toConsumableArray2["default"])(new Set(Object.keys(LANG_CODE_NAME_MAPPING)));
198
+ var _default = ALL_LANG_CODES;
199
+ exports["default"] = _default;
200
+ module.exports = exports.default;
@@ -84,6 +84,7 @@ var Subscriber = /*#__PURE__*/function () {
84
84
  this.__info = [];
85
85
  this._append_count = 0;
86
86
  this._remove_count = 0;
87
+ this._set_count = 0;
87
88
  this._unset_count = 0;
88
89
  this._events = [];
89
90
  this._helper = new _subscriber_helper["default"](distinct_id, config.workspace_key);
@@ -139,7 +140,7 @@ var Subscriber = /*#__PURE__*/function () {
139
140
  _iterator.f();
140
141
  }
141
142
 
142
- if (all_events.length === 0 || this._append_count > 0) {
143
+ if (all_events.length === 0 || this._append_count > 0 || this._set_count > 0) {
143
144
  var user_identify_event = {
144
145
  $insert_id: (0, _utils.uuid)(),
145
146
  $time: (0, _utils.epoch_milliseconds)(),
@@ -300,6 +301,7 @@ var Subscriber = /*#__PURE__*/function () {
300
301
  if (!(0, _utils.is_empty)(resp["event"])) {
301
302
  this._events.push(resp["event"]);
302
303
 
304
+ this._set_count += resp["set"];
303
305
  this._append_count += resp["append"];
304
306
  this._remove_count += resp["remove"];
305
307
  this._unset_count += resp["unset"];
@@ -397,6 +399,15 @@ var Subscriber = /*#__PURE__*/function () {
397
399
  this._collect_event();
398
400
  }
399
401
  }
402
+ }, {
403
+ key: "set_preferred_language",
404
+ value: function set_preferred_language(lang_code) {
405
+ var caller = "set_preferred_language";
406
+
407
+ this._helper._set_preferred_language(lang_code, caller);
408
+
409
+ this._collect_event();
410
+ }
400
411
  }, {
401
412
  key: "add_email",
402
413
  value: function add_email(email) {
@@ -15,6 +15,8 @@ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/creat
15
15
 
16
16
  var _utils = require("./utils");
17
17
 
18
+ var _language_codes = _interopRequireDefault(require("./language_codes"));
19
+
18
20
  // ---------- Identity keys
19
21
  var IDENT_KEY_EMAIL = "$email";
20
22
  var IDENT_KEY_SMS = "$sms";
@@ -25,7 +27,8 @@ var IDENT_KEY_WEBPUSH = "$webpush";
25
27
  var IDENT_KEY_SLACK = "$slack";
26
28
  var IDENT_KEYS_ALL = [IDENT_KEY_EMAIL, IDENT_KEY_SMS, IDENT_KEY_ANDROIDPUSH, IDENT_KEY_IOSPUSH, IDENT_KEY_WHATSAPP, IDENT_KEY_WEBPUSH, IDENT_KEY_SLACK];
27
29
  var KEY_PUSHVENDOR = "$pushvendor";
28
- var OTHER_RESERVED_KEYS = ["$messenger", "$inbox", KEY_PUSHVENDOR, "$device_id", "$insert_id", "$time", "$set", "$set_once", "$add", "$append", "$remove", "$unset", "$identify", "$anon_id", "$identified_id", "$notification_delivered", "$notification_dismiss", "$notification_clicked"];
30
+ var KEY_PREFERRED_LANGUAGE = "$preferred_language";
31
+ var OTHER_RESERVED_KEYS = ["$messenger", "$inbox", KEY_PUSHVENDOR, "$device_id", "$insert_id", "$time", "$set", "$set_once", "$add", "$append", "$remove", "$unset", "$identify", "$anon_id", "$identified_id", KEY_PREFERRED_LANGUAGE, "$notification_delivered", "$notification_dismiss", "$notification_clicked"];
29
32
  var SUPER_PROPERTY_KEYS = ["$app_version_string", "$app_build_number", "$brand", "$carrier", "$manufacturer", "$model", "$os", "$ss_sdk_version", "$insert_id", "$time"];
30
33
  var ALL_RESERVED_KEYS = [].concat(SUPER_PROPERTY_KEYS, OTHER_RESERVED_KEYS, IDENT_KEYS_ALL);
31
34
  var EMAIL_REGEX = /\S+@\S+\.\S+/;
@@ -36,6 +39,8 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
36
39
  (0, _classCallCheck2["default"])(this, _SubscriberInternalHelper);
37
40
  this.distinct_id = distinct_id;
38
41
  this.workspace_key = workspace_key;
42
+ this.__dict_set = {};
43
+ this.__set_count = 0;
39
44
  this.__dict_append = {};
40
45
  this.__append_count = 0;
41
46
  this.__dict_remove = {};
@@ -49,6 +54,8 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
49
54
  (0, _createClass2["default"])(_SubscriberInternalHelper, [{
50
55
  key: "reset",
51
56
  value: function reset() {
57
+ this.__dict_set = {};
58
+ this.__set_count = 0;
52
59
  this.__dict_append = {};
53
60
  this.__append_count = 0;
54
61
  this.__dict_remove = {};
@@ -67,6 +74,7 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
67
74
  errors: this.__errors,
68
75
  info: this.__info,
69
76
  event: evt,
77
+ set: this.__set_count,
70
78
  append: this.__append_count,
71
79
  remove: this.__remove_count,
72
80
  unset: this.__unset_count
@@ -77,7 +85,7 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
77
85
  }, {
78
86
  key: "__form_event",
79
87
  value: function __form_event() {
80
- if (!(0, _utils.is_empty)(this.__dict_append) || !(0, _utils.is_empty)(this.__dict_remove) || !(0, _utils.is_empty)(this.__list_unset)) {
88
+ if (!(0, _utils.is_empty)(this.__dict_set) || !(0, _utils.is_empty)(this.__dict_append) || !(0, _utils.is_empty)(this.__dict_remove) || !(0, _utils.is_empty)(this.__list_unset)) {
81
89
  var event = {
82
90
  $insert_id: (0, _utils.uuid)(),
83
91
  $time: (0, _utils.epoch_milliseconds)(),
@@ -85,6 +93,11 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
85
93
  distinct_id: this.distinct_id
86
94
  };
87
95
 
96
+ if (!(0, _utils.is_empty)(this.__dict_set)) {
97
+ event["$set"] = this.__dict_set;
98
+ this.__set_count += 1;
99
+ }
100
+
88
101
  if (!(0, _utils.is_empty)(this.__dict_append)) {
89
102
  event["$append"] = this.__dict_append;
90
103
  this.__append_count += 1;
@@ -208,6 +221,17 @@ var _SubscriberInternalHelper = /*#__PURE__*/function () {
208
221
 
209
222
  this.__list_unset.push(validated_key);
210
223
  }
224
+ }, {
225
+ key: "_set_preferred_language",
226
+ value: function _set_preferred_language(lang_code, caller) {
227
+ if (!_language_codes["default"].includes(lang_code)) {
228
+ this.__info.push("[".concat(caller, "] invalid value ").concat(lang_code));
229
+
230
+ return;
231
+ }
232
+
233
+ this.__dict_set[KEY_PREFERRED_LANGUAGE] = lang_code;
234
+ }
211
235
  }, {
212
236
  key: "__add_identity",
213
237
  value: function __add_identity(key, value, args, caller) {
@@ -133,8 +133,8 @@ var _BulkSubscribersChunk = /*#__PURE__*/function () {
133
133
  return false;
134
134
  }
135
135
 
136
- if (event_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
137
- throw new SuprsendError("workflow body (discounting attachment if any) too big - ".concat(event_size, " Bytes, must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
136
+ if (event_size > _constants.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
137
+ throw new SuprsendError("Event too big - ".concat(event_size, " Bytes, must not cross ").concat(_constants.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
138
138
  }
139
139
 
140
140
  if (this.__running_size + event_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
package/dist/workflow.js CHANGED
@@ -28,7 +28,8 @@ var _attachment = _interopRequireDefault(require("./attachment"));
28
28
  var _constants = require("./constants");
29
29
 
30
30
  var Workflow = /*#__PURE__*/function () {
31
- function Workflow(body, idempotency_key) {
31
+ function Workflow(body) {
32
+ var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
32
33
  (0, _classCallCheck2["default"])(this, Workflow);
33
34
 
34
35
  if (!(body instanceof Object)) {
@@ -36,13 +37,18 @@ var Workflow = /*#__PURE__*/function () {
36
37
  }
37
38
 
38
39
  this.body = body;
39
- this.idempotency_key = idempotency_key;
40
+ this.idempotency_key = kwargs === null || kwargs === void 0 ? void 0 : kwargs.idempotency_key;
40
41
  }
41
42
 
42
43
  (0, _createClass2["default"])(Workflow, [{
43
44
  key: "add_attachment",
44
45
  value: function add_attachment() {
46
+ var _kwargs$ignore_if_err;
47
+
45
48
  var file_path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
49
+ var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
50
+ var file_name = kwargs === null || kwargs === void 0 ? void 0 : kwargs.file_name;
51
+ var ignore_if_error = (_kwargs$ignore_if_err = kwargs === null || kwargs === void 0 ? void 0 : kwargs.ignore_if_error) !== null && _kwargs$ignore_if_err !== void 0 ? _kwargs$ignore_if_err : false;
46
52
 
47
53
  if (!this.body.data) {
48
54
  this.body.data = {};
@@ -52,7 +58,7 @@ var Workflow = /*#__PURE__*/function () {
52
58
  throw new _utils.SuprsendError("data must be a dictionary");
53
59
  }
54
60
 
55
- var attachment = (0, _attachment["default"])(file_path);
61
+ var attachment = (0, _attachment["default"])(file_path, file_name, ignore_if_error);
56
62
 
57
63
  if (!this.body.data["$attachments"]) {
58
64
  this.body["data"]["$attachments"] = [];
@@ -73,8 +79,8 @@ var Workflow = /*#__PURE__*/function () {
73
79
  this.body = (0, _utils.validate_workflow_body_schema)(this.body);
74
80
  var apparent_size = (0, _utils.get_apparent_workflow_body_size)(this.body, is_part_of_bulk); // review
75
81
 
76
- if (apparent_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
77
- throw new _utils.SuprsendError("workflow body (discounting attachment if any) too big - ".concat(apparent_size, " Bytes, must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
82
+ if (apparent_size > _constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
83
+ throw new _utils.SuprsendError("workflow body too big - ".concat(apparent_size, " Bytes, must not cross ").concat(_constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
78
84
  }
79
85
 
80
86
  return [this.body, apparent_size];
@@ -133,8 +133,8 @@ var _BulkWorkflowsChunk = /*#__PURE__*/function () {
133
133
  return false;
134
134
  }
135
135
 
136
- if (body_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
137
- throw new _utils.SuprsendError("workflow body (discounting attachment if any) too big - ".concat(body_size, " Bytes, must not cross ").concat(_constants.BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
136
+ if (body_size > _constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
137
+ throw new _utils.SuprsendError("workflow body too big - ".concat(body_size, " Bytes, must not cross ").concat(_constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
138
138
  }
139
139
 
140
140
  if (this.__running_size + body_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suprsend/node-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.1.1",
4
4
  "description": "Suprsend Node SDK to trigger workflow from backend",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/attachment.js CHANGED
@@ -2,11 +2,43 @@ import path from "path";
2
2
  import mime from "mime-types";
3
3
  import { base64Encode, resolveTilde } from "./utils";
4
4
 
5
- export default function get_attachment_json_for_file(file_path) {
5
+ function check_is_web_url(file_path = "") {
6
+ return file_path.startsWith("http://") || file_path.startsWith("https://");
7
+ }
8
+
9
+ function get_attachment_json_for_file(file_path, file_name, ignore_if_error) {
6
10
  const abs_path = path.resolve(resolveTilde(file_path));
11
+ let final_file_name = path.basename(abs_path);
12
+ if (file_name && file_name.trim()) {
13
+ final_file_name = file_name.trim();
14
+ }
7
15
  return {
8
- filename: path.basename(abs_path),
16
+ filename: final_file_name,
9
17
  contentType: mime.lookup(abs_path),
10
18
  data: base64Encode(abs_path),
19
+ url: null,
20
+ ignore_if_error: ignore_if_error,
21
+ };
22
+ }
23
+
24
+ function get_attachment_json_for_url(file_url, file_name, ignore_if_error) {
25
+ return {
26
+ filename: file_name,
27
+ contentType: null,
28
+ data: null,
29
+ url: file_url,
30
+ ignore_if_error: ignore_if_error,
11
31
  };
12
32
  }
33
+
34
+ export default function get_attachment_json(
35
+ file_path,
36
+ file_name,
37
+ ignore_if_error = false
38
+ ) {
39
+ if (check_is_web_url(file_path)) {
40
+ return get_attachment_json_for_url(file_path, file_name, ignore_if_error);
41
+ } else {
42
+ return get_attachment_json_for_file(file_path, file_name, ignore_if_error);
43
+ }
44
+ }
package/src/constants.js CHANGED
@@ -3,6 +3,10 @@ export const DEFAULT_URL = "https://hub.suprsend.com/";
3
3
  export const DEFAULT_UAT_URL =
4
4
  "https://collector-staging.suprsend.workers.dev/";
5
5
 
6
+ // an Event should not have apparent body size of more than 100KB
7
+ export const SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES = 100 * 1024; // 100 * 1024
8
+ export const SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE = "100KB";
9
+
6
10
  // a API call should not have apparent body size of more than 800KB
7
11
  export const BODY_MAX_APPARENT_SIZE_IN_BYTES = 800 * 1024; // 800 * 1024
8
12
  export const BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE = "800KB";
@@ -18,7 +22,7 @@ export const MAX_WORKFLOWS_IN_BULK_API = 100;
18
22
  // max event-records in one bulk api call
19
23
  export const MAX_EVENTS_IN_BULK_API = 100;
20
24
 
21
- export const ALLOW_ATTACHMENTS_IN_BULK_API = false;
25
+ export const ALLOW_ATTACHMENTS_IN_BULK_API = true;
22
26
  export const ATTACHMENT_UPLOAD_ENABLED = false;
23
27
 
24
28
  // -- single Identity event limit
package/src/event.js CHANGED
@@ -10,10 +10,10 @@ import {
10
10
  } from "./utils";
11
11
  import get_request_signature from "./signature";
12
12
  import axios from "axios";
13
- import get_attachment_json_for_file from "./attachment";
13
+ import get_attachment_json from "./attachment";
14
14
  import {
15
- BODY_MAX_APPARENT_SIZE_IN_BYTES,
16
- BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
15
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
16
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
17
17
  } from "./constants";
18
18
 
19
19
  const RESERVED_EVENT_NAMES = [
@@ -27,11 +27,11 @@ const RESERVED_EVENT_NAMES = [
27
27
  ];
28
28
 
29
29
  export default class Event {
30
- constructor(distinct_id, event_name, properties, idempotency_key) {
30
+ constructor(distinct_id, event_name, properties, kwargs = {}) {
31
31
  this.distinct_id = distinct_id;
32
32
  this.event_name = event_name;
33
33
  this.properties = properties;
34
- this.idempotency_key = idempotency_key;
34
+ this.idempotency_key = kwargs?.idempotency_key;
35
35
  // --- validate
36
36
  this.__validate_distinct_id();
37
37
  this.__validate_event_name();
@@ -79,8 +79,14 @@ export default class Event {
79
79
  this.event_name = event_name;
80
80
  }
81
81
 
82
- add_attachment(file_path) {
83
- const attachment = get_attachment_json_for_file(file_path);
82
+ add_attachment(file_path, kwargs = {}) {
83
+ const file_name = kwargs?.file_name;
84
+ const ignore_if_error = kwargs?.ignore_if_error ?? false;
85
+ const attachment = get_attachment_json(
86
+ file_path,
87
+ file_name,
88
+ ignore_if_error
89
+ );
84
90
  // --- add the attachment to properties->$attachments
85
91
  if (!this.properties["$attachments"]) {
86
92
  this.properties["$attachments"] = [];
@@ -103,9 +109,9 @@ export default class Event {
103
109
  }
104
110
  event_dict = validate_track_event_schema(event_dict);
105
111
  const apparent_size = get_apparent_event_size(event_dict, is_part_of_bulk);
106
- if (apparent_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
112
+ if (apparent_size > SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
107
113
  throw new SuprsendError(
108
- `Event properties too big - ${apparent_size} Bytes,must not cross ${BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
114
+ `Event size too big - ${apparent_size} Bytes,must not cross ${SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
109
115
  );
110
116
  }
111
117
  return [event_dict, apparent_size];
@@ -1,4 +1,6 @@
1
1
  import {
2
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
3
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
2
4
  ALLOW_ATTACHMENTS_IN_BULK_API,
3
5
  BODY_MAX_APPARENT_SIZE_IN_BYTES,
4
6
  BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
@@ -84,9 +86,9 @@ class _BulkEventsChunk {
84
86
  if (this.__check_limit_reached()) {
85
87
  return false;
86
88
  }
87
- if (event_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
89
+ if (event_size > SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
88
90
  throw new SuprsendError(
89
- `workflow body (discounting attachment if any) too big - ${event_size} Bytes, must not cross ${BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
91
+ `Event properties too big - ${event_size} Bytes, must not cross ${SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
90
92
  );
91
93
  }
92
94
  if (this.__running_size + event_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { SuprsendError, SuprsendConfigError } from "./utils";
2
- import get_attachment_json_for_file from "./attachment";
2
+ import get_attachment_json from "./attachment";
3
3
  import Workflow, { _WorkflowTrigger } from "./workflow";
4
4
  import { BulkWorkflowsFactory } from "./workflows_bulk";
5
5
  import Event, { EventCollector } from "./event";
@@ -78,14 +78,20 @@ class Suprsend {
78
78
  return base_url;
79
79
  }
80
80
 
81
- add_attachment(body, file_path) {
81
+ add_attachment(body, file_path, kwargs = {}) {
82
+ const file_name = kwargs?.file_name;
83
+ const ignore_if_error = kwargs?.ignore_if_error ?? false;
82
84
  if (!body.data) {
83
85
  body.data = {};
84
86
  }
85
87
  if (!body.data instanceof Object) {
86
88
  throw new SuprsendError("data must be an object");
87
89
  }
88
- const attachment = get_attachment_json_for_file(file_path);
90
+ const attachment = get_attachment_json(
91
+ file_path,
92
+ file_name,
93
+ ignore_if_error
94
+ );
89
95
  if (!body.data["$attachments"]) {
90
96
  body["data"]["$attachments"] = [];
91
97
  }
@@ -103,13 +109,8 @@ class Suprsend {
103
109
  return this._workflow_trigger.trigger(wf_ins);
104
110
  }
105
111
 
106
- track(distinct_id, event_name, properties = {}, idempotency_key) {
107
- const event = new Event(
108
- distinct_id,
109
- event_name,
110
- properties,
111
- idempotency_key
112
- );
112
+ track(distinct_id, event_name, properties = {}, kwargs = {}) {
113
+ const event = new Event(distinct_id, event_name, properties, kwargs);
113
114
  return this._eventcollector.collect(event);
114
115
  }
115
116
 
@@ -0,0 +1,188 @@
1
+ const LANG_CODE_NAME_MAPPING = {
2
+ aa: "Afar",
3
+ ab: "Abkhazian",
4
+ ae: "Avestan",
5
+ af: "Afrikaans",
6
+ ak: "Akan",
7
+ am: "Amharic",
8
+ an: "Aragonese",
9
+ ar: "Arabic",
10
+ as: "Assamese",
11
+ av: "Avaric",
12
+ ay: "Aymara",
13
+ az: "Azerbaijani",
14
+ ba: "Bashkir",
15
+ be: "Belarusian",
16
+ bg: "Bulgarian",
17
+ bi: "Bislama",
18
+ bm: "Bambara",
19
+ bn: "Bengali",
20
+ bo: "Tibetan",
21
+ br: "Breton",
22
+ bs: "Bosnian",
23
+ ca: "Catalan; Valencian",
24
+ ce: "Chechen",
25
+ ch: "Chamorro",
26
+ co: "Corsican",
27
+ cr: "Cree",
28
+ cs: "Czech",
29
+ cu: "Church Slavic; Old Slavonic; Church Slavonic; Old Bulgarian; Old Church Slavonic",
30
+ cv: "Chuvash",
31
+ cy: "Welsh",
32
+ da: "Danish",
33
+ de: "German",
34
+ dv: "Divehi; Dhivehi; Maldivian",
35
+ dz: "Dzongkha",
36
+ ee: "Ewe",
37
+ el: "Greek, Modern (1453-)",
38
+ en: "English",
39
+ eo: "Esperanto",
40
+ es: "Spanish; Castilian",
41
+ et: "Estonian",
42
+ eu: "Basque",
43
+ fa: "Persian",
44
+ ff: "Fulah",
45
+ fi: "Finnish",
46
+ fj: "Fijian",
47
+ fo: "Faroese",
48
+ fr: "French",
49
+ fy: "Western Frisian",
50
+ ga: "Irish",
51
+ gd: "Gaelic; Scottish Gaelic",
52
+ gl: "Galician",
53
+ gn: "Guarani",
54
+ gu: "Gujarati",
55
+ gv: "Manx",
56
+ ha: "Hausa",
57
+ he: "Hebrew",
58
+ hi: "Hindi",
59
+ ho: "Hiri Motu",
60
+ hr: "Croatian",
61
+ ht: "Haitian; Haitian Creole",
62
+ hu: "Hungarian",
63
+ hy: "Armenian",
64
+ hz: "Herero",
65
+ ia: "Interlingua (International Auxiliary Language Association)",
66
+ id: "Indonesian",
67
+ ie: "Interlingue; Occidental",
68
+ ig: "Igbo",
69
+ ii: "Sichuan Yi; Nuosu",
70
+ ik: "Inupiaq",
71
+ io: "Ido",
72
+ is: "Icelandic",
73
+ it: "Italian",
74
+ iu: "Inuktitut",
75
+ ja: "Japanese",
76
+ jv: "Javanese",
77
+ ka: "Georgian",
78
+ kg: "Kongo",
79
+ ki: "Kikuyu; Gikuyu",
80
+ kj: "Kuanyama; Kwanyama",
81
+ kk: "Kazakh",
82
+ kl: "Kalaallisut; Greenlandic",
83
+ km: "Central Khmer",
84
+ kn: "Kannada",
85
+ ko: "Korean",
86
+ kr: "Kanuri",
87
+ ks: "Kashmiri",
88
+ ku: "Kurdish",
89
+ kv: "Komi",
90
+ kw: "Cornish",
91
+ ky: "Kirghiz; Kyrgyz",
92
+ la: "Latin",
93
+ lb: "Luxembourgish; Letzeburgesch",
94
+ lg: "Ganda",
95
+ li: "Limburgan; Limburger; Limburgish",
96
+ ln: "Lingala",
97
+ lo: "Lao",
98
+ lt: "Lithuanian",
99
+ lu: "Luba-Katanga",
100
+ lv: "Latvian",
101
+ mg: "Malagasy",
102
+ mh: "Marshallese",
103
+ mi: "Maori",
104
+ mk: "Macedonian",
105
+ ml: "Malayalam",
106
+ mn: "Mongolian",
107
+ mr: "Marathi",
108
+ ms: "Malay",
109
+ mt: "Maltese",
110
+ my: "Burmese",
111
+ na: "Nauru",
112
+ nb: "Bokmål, Norwegian; Norwegian Bokmål",
113
+ nd: "Ndebele, North; North Ndebele",
114
+ ne: "Nepali",
115
+ ng: "Ndonga",
116
+ nl: "Dutch; Flemish",
117
+ nn: "Norwegian Nynorsk; Nynorsk, Norwegian",
118
+ no: "Norwegian",
119
+ nr: "Ndebele, South; South Ndebele",
120
+ nv: "Navajo; Navaho",
121
+ ny: "Chichewa; Chewa; Nyanja",
122
+ oc: "Occitan (post 1500)",
123
+ oj: "Ojibwa",
124
+ om: "Oromo",
125
+ or: "Oriya",
126
+ os: "Ossetian; Ossetic",
127
+ pa: "Panjabi; Punjabi",
128
+ pi: "Pali",
129
+ pl: "Polish",
130
+ ps: "Pushto; Pashto",
131
+ pt: "Portuguese",
132
+ qu: "Quechua",
133
+ rm: "Romansh",
134
+ rn: "Rundi",
135
+ ro: "Romanian; Moldavian; Moldovan",
136
+ ru: "Russian",
137
+ rw: "Kinyarwanda",
138
+ sa: "Sanskrit",
139
+ sc: "Sardinian",
140
+ sd: "Sindhi",
141
+ se: "Northern Sami",
142
+ sg: "Sango",
143
+ si: "Sinhala; Sinhalese",
144
+ sk: "Slovak",
145
+ sl: "Slovenian",
146
+ sm: "Samoan",
147
+ sn: "Shona",
148
+ so: "Somali",
149
+ sq: "Albanian",
150
+ sr: "Serbian",
151
+ ss: "Swati",
152
+ st: "Sotho, Southern",
153
+ su: "Sundanese",
154
+ sv: "Swedish",
155
+ sw: "Swahili",
156
+ ta: "Tamil",
157
+ te: "Telugu",
158
+ tg: "Tajik",
159
+ th: "Thai",
160
+ ti: "Tigrinya",
161
+ tk: "Turkmen",
162
+ tl: "Tagalog",
163
+ tn: "Tswana",
164
+ to: "Tonga (Tonga Islands)",
165
+ tr: "Turkish",
166
+ ts: "Tsonga",
167
+ tt: "Tatar",
168
+ tw: "Twi",
169
+ ty: "Tahitian",
170
+ ug: "Uighur; Uyghur",
171
+ uk: "Ukrainian",
172
+ ur: "Urdu",
173
+ uz: "Uzbek",
174
+ ve: "Venda",
175
+ vi: "Vietnamese",
176
+ vo: "Volapük",
177
+ wa: "Walloon",
178
+ wo: "Wolof",
179
+ xh: "Xhosa",
180
+ yi: "Yiddish",
181
+ yo: "Yoruba",
182
+ za: "Zhuang; Chuang",
183
+ zh: "Chinese",
184
+ zu: "Zulu",
185
+ };
186
+
187
+ const ALL_LANG_CODES = [...new Set(Object.keys(LANG_CODE_NAME_MAPPING))];
188
+ export default ALL_LANG_CODES;
package/src/subscriber.js CHANGED
@@ -50,6 +50,7 @@ export class Subscriber {
50
50
  this.__info = [];
51
51
  this._append_count = 0;
52
52
  this._remove_count = 0;
53
+ this._set_count = 0;
53
54
  this._unset_count = 0;
54
55
  this._events = [];
55
56
  this._helper = new _SubscriberInternalHelper(
@@ -90,7 +91,11 @@ export class Subscriber {
90
91
  e["properties"] = this.__supr_props;
91
92
  }
92
93
 
93
- if (all_events.length === 0 || this._append_count > 0) {
94
+ if (
95
+ all_events.length === 0 ||
96
+ this._append_count > 0 ||
97
+ this._set_count > 0
98
+ ) {
94
99
  const user_identify_event = {
95
100
  $insert_id: uuid(),
96
101
  $time: epoch_milliseconds(),
@@ -196,6 +201,7 @@ export class Subscriber {
196
201
  }
197
202
  if (!is_empty(resp["event"])) {
198
203
  this._events.push(resp["event"]);
204
+ this._set_count += resp["set"];
199
205
  this._append_count += resp["append"];
200
206
  this._remove_count += resp["remove"];
201
207
  this._unset_count += resp["unset"];
@@ -267,6 +273,12 @@ export class Subscriber {
267
273
  }
268
274
  }
269
275
 
276
+ set_preferred_language(lang_code) {
277
+ const caller = "set_preferred_language";
278
+ this._helper._set_preferred_language(lang_code, caller);
279
+ this._collect_event();
280
+ }
281
+
270
282
  add_email(email) {
271
283
  const caller = "add_email";
272
284
  this._helper._add_email(email, caller);
@@ -6,6 +6,7 @@ import {
6
6
  is_empty,
7
7
  is_string,
8
8
  } from "./utils";
9
+ import ALL_LANG_CODES from "./language_codes";
9
10
 
10
11
  // ---------- Identity keys
11
12
  const IDENT_KEY_EMAIL = "$email";
@@ -27,6 +28,7 @@ const IDENT_KEYS_ALL = [
27
28
  ];
28
29
 
29
30
  const KEY_PUSHVENDOR = "$pushvendor";
31
+ const KEY_PREFERRED_LANGUAGE = "$preferred_language";
30
32
 
31
33
  const OTHER_RESERVED_KEYS = [
32
34
  "$messenger",
@@ -44,6 +46,7 @@ const OTHER_RESERVED_KEYS = [
44
46
  "$identify",
45
47
  "$anon_id",
46
48
  "$identified_id",
49
+ KEY_PREFERRED_LANGUAGE,
47
50
  "$notification_delivered",
48
51
  "$notification_dismiss",
49
52
  "$notification_clicked",
@@ -76,6 +79,9 @@ export default class _SubscriberInternalHelper {
76
79
  this.distinct_id = distinct_id;
77
80
  this.workspace_key = workspace_key;
78
81
 
82
+ this.__dict_set = {};
83
+ this.__set_count = 0;
84
+
79
85
  this.__dict_append = {};
80
86
  this.__append_count = 0;
81
87
 
@@ -90,6 +96,9 @@ export default class _SubscriberInternalHelper {
90
96
  }
91
97
 
92
98
  reset() {
99
+ this.__dict_set = {};
100
+ this.__set_count = 0;
101
+
93
102
  this.__dict_append = {};
94
103
  this.__append_count = 0;
95
104
 
@@ -109,6 +118,7 @@ export default class _SubscriberInternalHelper {
109
118
  errors: this.__errors,
110
119
  info: this.__info,
111
120
  event: evt,
121
+ set: this.__set_count,
112
122
  append: this.__append_count,
113
123
  remove: this.__remove_count,
114
124
  unset: this.__unset_count,
@@ -119,6 +129,7 @@ export default class _SubscriberInternalHelper {
119
129
 
120
130
  __form_event() {
121
131
  if (
132
+ !is_empty(this.__dict_set) ||
122
133
  !is_empty(this.__dict_append) ||
123
134
  !is_empty(this.__dict_remove) ||
124
135
  !is_empty(this.__list_unset)
@@ -129,6 +140,10 @@ export default class _SubscriberInternalHelper {
129
140
  env: this.workspace_key,
130
141
  distinct_id: this.distinct_id,
131
142
  };
143
+ if (!is_empty(this.__dict_set)) {
144
+ event["$set"] = this.__dict_set;
145
+ this.__set_count += 1;
146
+ }
132
147
  if (!is_empty(this.__dict_append)) {
133
148
  event["$append"] = this.__dict_append;
134
149
  this.__append_count += 1;
@@ -216,6 +231,14 @@ export default class _SubscriberInternalHelper {
216
231
  this.__list_unset.push(validated_key);
217
232
  }
218
233
 
234
+ _set_preferred_language(lang_code, caller) {
235
+ if (!ALL_LANG_CODES.includes(lang_code)) {
236
+ this.__info.push(`[${caller}] invalid value ${lang_code}`);
237
+ return;
238
+ }
239
+ this.__dict_set[KEY_PREFERRED_LANGUAGE] = lang_code;
240
+ }
241
+
219
242
  __add_identity(key, value, args, caller) {
220
243
  switch (key) {
221
244
  case IDENT_KEY_EMAIL:
@@ -1,4 +1,6 @@
1
1
  import {
2
+ IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
3
+ IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
2
4
  BODY_MAX_APPARENT_SIZE_IN_BYTES,
3
5
  BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
4
6
  MAX_IDENTITY_EVENTS_IN_BULK_API,
@@ -82,9 +84,9 @@ class _BulkSubscribersChunk {
82
84
  if (this.__check_limit_reached()) {
83
85
  return false;
84
86
  }
85
- if (event_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
87
+ if (event_size > IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
86
88
  throw new SuprsendError(
87
- `workflow body (discounting attachment if any) too big - ${event_size} Bytes, must not cross ${BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
89
+ `Event too big - ${event_size} Bytes, must not cross ${IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
88
90
  );
89
91
  }
90
92
  if (this.__running_size + event_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
package/src/workflow.js CHANGED
@@ -5,29 +5,35 @@ import {
5
5
  validate_workflow_body_schema,
6
6
  get_apparent_workflow_body_size,
7
7
  } from "./utils";
8
- import get_attachment_json_for_file from "./attachment";
8
+ import get_attachment_json from "./attachment";
9
9
  import {
10
- BODY_MAX_APPARENT_SIZE_IN_BYTES,
11
- BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
10
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
11
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
12
12
  } from "./constants";
13
13
 
14
14
  export default class Workflow {
15
- constructor(body, idempotency_key) {
15
+ constructor(body, kwargs = {}) {
16
16
  if (!(body instanceof Object)) {
17
17
  throw new SuprsendError("workflow body must be a json/dictionary");
18
18
  }
19
19
  this.body = body;
20
- this.idempotency_key = idempotency_key;
20
+ this.idempotency_key = kwargs?.idempotency_key;
21
21
  }
22
22
 
23
- add_attachment(file_path = "") {
23
+ add_attachment(file_path = "", kwargs = {}) {
24
+ const file_name = kwargs?.file_name;
25
+ const ignore_if_error = kwargs?.ignore_if_error ?? false;
24
26
  if (!this.body.data) {
25
27
  this.body.data = {};
26
28
  }
27
29
  if (!(this.body instanceof Object)) {
28
30
  throw new SuprsendError("data must be a dictionary");
29
31
  }
30
- const attachment = get_attachment_json_for_file(file_path);
32
+ const attachment = get_attachment_json(
33
+ file_path,
34
+ file_name,
35
+ ignore_if_error
36
+ );
31
37
 
32
38
  if (!this.body.data["$attachments"]) {
33
39
  this.body["data"]["$attachments"] = [];
@@ -45,9 +51,9 @@ export default class Workflow {
45
51
  this.body,
46
52
  is_part_of_bulk
47
53
  ); // review
48
- if (apparent_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
54
+ if (apparent_size > SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
49
55
  throw new SuprsendError(
50
- `workflow body (discounting attachment if any) too big - ${apparent_size} Bytes, must not cross ${BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
56
+ `workflow body too big - ${apparent_size} Bytes, must not cross ${SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
51
57
  );
52
58
  }
53
59
  return [this.body, apparent_size];
@@ -1,4 +1,6 @@
1
1
  import {
2
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES,
3
+ SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
2
4
  BODY_MAX_APPARENT_SIZE_IN_BYTES,
3
5
  BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE,
4
6
  MAX_WORKFLOWS_IN_BULK_API,
@@ -84,9 +86,9 @@ class _BulkWorkflowsChunk {
84
86
  if (this.__check_limit_reached()) {
85
87
  return false;
86
88
  }
87
- if (body_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {
89
+ if (body_size > SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
88
90
  throw new SuprsendError(
89
- `workflow body (discounting attachment if any) too big - ${body_size} Bytes, must not cross ${BODY_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
91
+ `workflow body too big - ${body_size} Bytes, must not cross ${SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE}`
90
92
  );
91
93
  }
92
94
  if (this.__running_size + body_size > BODY_MAX_APPARENT_SIZE_IN_BYTES) {