@suprsend/node-sdk 1.7.1 → 1.8.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.
@@ -33,6 +33,8 @@ var _lodash = require("lodash");
33
33
 
34
34
  var _axios = _interopRequireDefault(require("axios"));
35
35
 
36
+ var _utils = require("./utils");
37
+
36
38
  function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
37
39
 
38
40
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
@@ -123,7 +125,7 @@ var _BulkSubscribersChunk = /*#__PURE__*/function () {
123
125
  }
124
126
 
125
127
  if (event_size > _constants.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
126
- throw new SuprsendError("Event too big - ".concat(event_size, " Bytes, must not cross ").concat(_constants.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
128
+ throw new _utils.InputValueError("Event too big - ".concat(event_size, " Bytes, must not cross ").concat(_constants.IDENTITY_SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
127
129
  }
128
130
 
129
131
  if (this.__running_size + event_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
@@ -231,37 +233,42 @@ var BulkSubscribers = /*#__PURE__*/function () {
231
233
  this.__subscribers = [];
232
234
  this.__pending_records = [];
233
235
  this.chunks = [];
234
- this.response = new _bulk_response["default"]();
236
+ this.response = new _bulk_response["default"](); // invalid_record json: {"record": event-json, "error": error_str, "code": 500}
237
+
238
+ this.__invalid_records = [];
235
239
  }
236
240
 
237
241
  (0, _createClass2["default"])(BulkSubscribers, [{
238
242
  key: "__validate_subscriber_events",
239
243
  value: function __validate_subscriber_events() {
240
- if (!this.__subscribers) {
241
- throw new SuprsendError("users list is empty in bulk request");
242
- }
243
-
244
244
  var _iterator = _createForOfIteratorHelper(this.__subscribers),
245
245
  _step;
246
246
 
247
247
  try {
248
248
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
249
249
  var sub = _step.value;
250
- var is_part_of_bulk = true;
251
- var warnings_list = sub.validate_body(is_part_of_bulk);
252
250
 
253
- if (warnings_list) {
254
- this.response.warnings = [].concat((0, _toConsumableArray2["default"])(this.response.warnings), (0, _toConsumableArray2["default"])(warnings_list));
255
- }
251
+ try {
252
+ var is_part_of_bulk = true;
253
+ var warnings_list = sub.validate_body(is_part_of_bulk);
254
+
255
+ if (warnings_list) {
256
+ this.response.warnings = [].concat((0, _toConsumableArray2["default"])(this.response.warnings), (0, _toConsumableArray2["default"])(warnings_list));
257
+ }
256
258
 
257
- var ev = sub.get_events();
259
+ var ev = sub.get_events();
258
260
 
259
- var _sub$validate_event_s = sub.validate_event_size(ev),
260
- _sub$validate_event_s2 = (0, _slicedToArray2["default"])(_sub$validate_event_s, 2),
261
- ev_json = _sub$validate_event_s2[0],
262
- body_size = _sub$validate_event_s2[1];
261
+ var _sub$validate_event_s = sub.validate_event_size(ev),
262
+ _sub$validate_event_s2 = (0, _slicedToArray2["default"])(_sub$validate_event_s, 2),
263
+ ev_json = _sub$validate_event_s2[0],
264
+ body_size = _sub$validate_event_s2[1];
263
265
 
264
- this.__pending_records.push([ev_json, body_size]);
266
+ this.__pending_records.push([ev_json, body_size]);
267
+ } catch (ex) {
268
+ var inv_rec = (0, _utils.invalid_record_json)(sub.as_json(), ex);
269
+
270
+ this.__invalid_records.push(inv_rec);
271
+ }
265
272
  }
266
273
  } catch (err) {
267
274
  _iterator.e(err);
@@ -311,23 +318,17 @@ var BulkSubscribers = /*#__PURE__*/function () {
311
318
  }
312
319
 
313
320
  if (!subscribers) {
314
- throw SuprsendError("users list empty. must pass one or more users");
321
+ return;
315
322
  }
316
323
 
317
324
  for (var _i = 0, _subscribers = subscribers; _i < _subscribers.length; _i++) {
318
325
  var sub = _subscribers[_i];
319
326
 
320
- if (!sub) {
321
- continue;
322
- }
327
+ if (sub && sub instanceof _subscriber.Subscriber) {
328
+ var sub_copy = (0, _lodash.cloneDeep)(sub);
323
329
 
324
- if (!(sub instanceof _subscriber.Subscriber)) {
325
- throw new SuprsendError("element must be an instance of suprsend.Subscriber");
330
+ this.__subscribers.push(sub_copy);
326
331
  }
327
-
328
- var sub_copy = (0, _lodash.cloneDeep)(sub);
329
-
330
- this.__subscribers.push(sub_copy);
331
332
  }
332
333
  }
333
334
  }, {
@@ -339,7 +340,7 @@ var BulkSubscribers = /*#__PURE__*/function () {
339
340
  key: "save",
340
341
  value: function () {
341
342
  var _save = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
342
- var _iterator3, _step3, _step3$value, c_idx, ch;
343
+ var ch_response, _iterator3, _step3, _step3$value, c_idx, ch;
343
344
 
344
345
  return _regenerator["default"].wrap(function _callee2$(_context2) {
345
346
  while (1) {
@@ -347,16 +348,26 @@ var BulkSubscribers = /*#__PURE__*/function () {
347
348
  case 0:
348
349
  this.__validate_subscriber_events();
349
350
 
351
+ if (this.__invalid_records.length > 0) {
352
+ ch_response = _bulk_response["default"].invalid_records_chunk_response(this.__invalid_records);
353
+ this.response.merge_chunk_response(ch_response);
354
+ }
355
+
356
+ if (!this.__pending_records.length) {
357
+ _context2.next = 25;
358
+ break;
359
+ }
360
+
350
361
  this.__chunkify();
351
362
 
352
363
  _iterator3 = _createForOfIteratorHelper(this.chunks.entries());
353
- _context2.prev = 3;
364
+ _context2.prev = 5;
354
365
 
355
366
  _iterator3.s();
356
367
 
357
- case 5:
368
+ case 7:
358
369
  if ((_step3 = _iterator3.n()).done) {
359
- _context2.next = 13;
370
+ _context2.next = 15;
360
371
  break;
361
372
  }
362
373
 
@@ -367,43 +378,54 @@ var BulkSubscribers = /*#__PURE__*/function () {
367
378
  } // do api call
368
379
 
369
380
 
370
- _context2.next = 10;
381
+ _context2.next = 12;
371
382
  return ch.trigger();
372
383
 
373
- case 10:
384
+ case 12:
374
385
  // merge response
375
386
  this.response.merge_chunk_response(ch.response);
376
387
 
377
- case 11:
378
- _context2.next = 5;
379
- break;
380
-
381
388
  case 13:
382
- _context2.next = 18;
389
+ _context2.next = 7;
383
390
  break;
384
391
 
385
392
  case 15:
386
- _context2.prev = 15;
387
- _context2.t0 = _context2["catch"](3);
393
+ _context2.next = 20;
394
+ break;
395
+
396
+ case 17:
397
+ _context2.prev = 17;
398
+ _context2.t0 = _context2["catch"](5);
388
399
 
389
400
  _iterator3.e(_context2.t0);
390
401
 
391
- case 18:
392
- _context2.prev = 18;
402
+ case 20:
403
+ _context2.prev = 20;
393
404
 
394
405
  _iterator3.f();
395
406
 
396
- return _context2.finish(18);
407
+ return _context2.finish(20);
408
+
409
+ case 23:
410
+ _context2.next = 26;
411
+ break;
412
+
413
+ case 25:
414
+ // if no records. i.e. invalid_records.length and pending_records.length both are 0
415
+ // then add empty success response
416
+ if (this.__invalid_records.length === 0) {
417
+ this.response.merge_chunk_response(_bulk_response["default"].empty_chunk_success_response());
418
+ }
397
419
 
398
- case 21:
420
+ case 26:
399
421
  return _context2.abrupt("return", this.response);
400
422
 
401
- case 22:
423
+ case 27:
402
424
  case "end":
403
425
  return _context2.stop();
404
426
  }
405
427
  }
406
- }, _callee2, this, [[3, 15, 18, 21]]);
428
+ }, _callee2, this, [[5, 17, 20, 23]]);
407
429
  }));
408
430
 
409
431
  function save() {
package/dist/utils.js CHANGED
@@ -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.SuprsendError = exports.SuprsendConfigError = exports.SuprsendApiError = void 0;
8
+ exports.SuprsendError = exports.SuprsendConfigError = exports.SuprsendApiError = exports.InputValueError = void 0;
9
9
  exports.base64Encode = base64Encode;
10
10
  exports.epoch_milliseconds = epoch_milliseconds;
11
11
  exports.get_apparent_event_size = get_apparent_event_size;
@@ -13,6 +13,7 @@ exports.get_apparent_identity_event_size = get_apparent_identity_event_size;
13
13
  exports.get_apparent_list_broadcast_body_size = get_apparent_list_broadcast_body_size;
14
14
  exports.get_apparent_workflow_body_size = get_apparent_workflow_body_size;
15
15
  exports.has_special_char = void 0;
16
+ exports.invalid_record_json = invalid_record_json;
16
17
  exports.is_empty = is_empty;
17
18
  exports.is_object = is_object;
18
19
  exports.is_string = is_string;
@@ -145,6 +146,25 @@ var SuprsendApiError = /*#__PURE__*/function (_Error3) {
145
146
 
146
147
  exports.SuprsendApiError = SuprsendApiError;
147
148
 
149
+ var InputValueError = /*#__PURE__*/function (_Error4) {
150
+ (0, _inherits2["default"])(InputValueError, _Error4);
151
+
152
+ var _super4 = _createSuper(InputValueError);
153
+
154
+ function InputValueError(message) {
155
+ var _this4;
156
+
157
+ (0, _classCallCheck2["default"])(this, InputValueError);
158
+ _this4 = _super4.call(this, message);
159
+ _this4.name = "InputValueError";
160
+ return _this4;
161
+ }
162
+
163
+ return InputValueError;
164
+ }( /*#__PURE__*/(0, _wrapNativeSuper2["default"])(Error));
165
+
166
+ exports.InputValueError = InputValueError;
167
+
148
168
  function is_string(value) {
149
169
  return typeof value === "string";
150
170
  }
@@ -185,7 +205,7 @@ function validate_workflow_body_schema(body) {
185
205
  }
186
206
 
187
207
  if (!(body.data instanceof Object)) {
188
- throw new SuprsendError("data must be a object");
208
+ throw new InputValueError("data must be a object");
189
209
  }
190
210
 
191
211
  var schema = workflow_schema;
@@ -225,7 +245,7 @@ function validate_list_broadcast_body_schema(body) {
225
245
  }
226
246
 
227
247
  if (!(body.data instanceof Object)) {
228
- throw new SuprsendError("data must be a object");
248
+ throw new InputValueError("data must be a object");
229
249
  }
230
250
 
231
251
  var schema = list_broadcast_schema;
@@ -371,4 +391,22 @@ function get_apparent_identity_event_size(event) {
371
391
  function get_apparent_list_broadcast_body_size(body) {
372
392
  var body_size = JSON.stringify(body).length;
373
393
  return body_size;
394
+ }
395
+
396
+ function invalid_record_json(failed_record, err) {
397
+ var err_str;
398
+
399
+ if (err instanceof InputValueError) {
400
+ err_str = err.message;
401
+ } else {
402
+ // includes SuprsendValidationError,
403
+ // OR any other error
404
+ err_str = "".concat(err.message, "\n").concat(err.stack);
405
+ }
406
+
407
+ return {
408
+ record: failed_record,
409
+ error: err_str,
410
+ code: 500
411
+ };
374
412
  }
package/dist/workflow.js CHANGED
@@ -13,6 +13,8 @@ var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/
13
13
 
14
14
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
15
15
 
16
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
17
+
16
18
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
17
19
 
18
20
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
@@ -27,13 +29,17 @@ var _attachment = _interopRequireDefault(require("./attachment"));
27
29
 
28
30
  var _constants = require("./constants");
29
31
 
32
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
33
+
34
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
35
+
30
36
  var Workflow = /*#__PURE__*/function () {
31
37
  function Workflow(body) {
32
38
  var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
33
39
  (0, _classCallCheck2["default"])(this, Workflow);
34
40
 
35
41
  if (!(body instanceof Object)) {
36
- throw new _utils.SuprsendError("workflow body must be a json/dictionary");
42
+ throw new _utils.InputValueError("workflow body must be a json/dictionary");
37
43
  }
38
44
 
39
45
  this.body = body;
@@ -44,23 +50,29 @@ var Workflow = /*#__PURE__*/function () {
44
50
  (0, _createClass2["default"])(Workflow, [{
45
51
  key: "add_attachment",
46
52
  value: function add_attachment() {
47
- var _kwargs$ignore_if_err;
53
+ var _kwargs$ignore_if_err, _this$body;
48
54
 
49
55
  var file_path = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
50
56
  var kwargs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
51
57
  var file_name = kwargs === null || kwargs === void 0 ? void 0 : kwargs.file_name;
52
58
  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;
53
59
 
54
- if (!this.body.data) {
60
+ if (!((_this$body = this.body) !== null && _this$body !== void 0 && _this$body.data)) {
55
61
  this.body.data = {};
56
- }
62
+ } // if body["data"] is not a dict, not raising error while adding attachment.
57
63
 
58
- if (!(this.body instanceof Object)) {
59
- throw new _utils.SuprsendError("data must be a dictionary");
64
+
65
+ if (!(this.body["data"] instanceof Object)) {
66
+ console.log("WARNING: attachment cannot be added. please make sure body['data'] is a dictionary. Workflow ".concat(JSON.stringify(this.as_json())));
67
+ return;
60
68
  }
61
69
 
62
70
  var attachment = (0, _attachment["default"])(file_path, file_name, ignore_if_error);
63
71
 
72
+ if (!attachment) {
73
+ return;
74
+ }
75
+
64
76
  if (!this.body.data["$attachments"]) {
65
77
  this.body["data"]["$attachments"] = [];
66
78
  }
@@ -85,11 +97,26 @@ var Workflow = /*#__PURE__*/function () {
85
97
  var apparent_size = (0, _utils.get_apparent_workflow_body_size)(this.body, is_part_of_bulk); // review
86
98
 
87
99
  if (apparent_size > _constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
88
- 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));
100
+ throw new _utils.InputValueError("workflow body too big - ".concat(apparent_size, " Bytes, must not cross ").concat(_constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
89
101
  }
90
102
 
91
103
  return [this.body, apparent_size];
92
104
  }
105
+ }, {
106
+ key: "as_json",
107
+ value: function as_json() {
108
+ var body_dict = _objectSpread({}, this.body);
109
+
110
+ if (this.idempotency_key) {
111
+ body_dict["$idempotency_key"] = this.idempotency_key;
112
+ }
113
+
114
+ if (this.brand_id) {
115
+ body_dict["brand_id"] = this.brand_id;
116
+ }
117
+
118
+ return body_dict;
119
+ }
93
120
  }]);
94
121
  return Workflow;
95
122
  }();
@@ -123,7 +123,7 @@ var _BulkWorkflowsChunk = /*#__PURE__*/function () {
123
123
  }
124
124
 
125
125
  if (body_size > _constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES) {
126
- 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));
126
+ throw new _utils.InputValueError("workflow body too big - ".concat(body_size, " Bytes, must not cross ").concat(_constants.SINGLE_EVENT_MAX_APPARENT_SIZE_IN_BYTES_READABLE));
127
127
  }
128
128
 
129
129
  if (this.__running_size + body_size > _constants.BODY_MAX_APPARENT_SIZE_IN_BYTES) {
@@ -235,30 +235,35 @@ var BulkWorkflows = /*#__PURE__*/function () {
235
235
  this.__workflows = [];
236
236
  this.__pending_records = [];
237
237
  this.chunks = [];
238
- this.response = new _bulk_response["default"]();
238
+ this.response = new _bulk_response["default"](); // invalid_record json: {"record": workflow-json, "error": error_str, "code": 500}
239
+
240
+ this.__invalid_records = [];
239
241
  }
240
242
 
241
243
  (0, _createClass2["default"])(BulkWorkflows, [{
242
244
  key: "__validate_workflows",
243
245
  value: function __validate_workflows() {
244
- if (!this.__workflows) {
245
- throw new _utils.SuprsendError("workflow list is empty in bulk request");
246
- }
247
-
248
246
  var _iterator = _createForOfIteratorHelper(this.__workflows),
249
247
  _step;
250
248
 
251
249
  try {
252
250
  for (_iterator.s(); !(_step = _iterator.n()).done;) {
253
251
  var wf = _step.value;
254
- var is_part_of_bulk = true;
255
252
 
256
- var _wf$get_final_json = wf.get_final_json(this.config, is_part_of_bulk),
257
- _wf$get_final_json2 = (0, _slicedToArray2["default"])(_wf$get_final_json, 2),
258
- wf_body = _wf$get_final_json2[0],
259
- body_size = _wf$get_final_json2[1];
253
+ try {
254
+ var is_part_of_bulk = true;
255
+
256
+ var _wf$get_final_json = wf.get_final_json(this.config, is_part_of_bulk),
257
+ _wf$get_final_json2 = (0, _slicedToArray2["default"])(_wf$get_final_json, 2),
258
+ wf_body = _wf$get_final_json2[0],
259
+ body_size = _wf$get_final_json2[1];
260
260
 
261
- this.__pending_records.push([wf_body, body_size]);
261
+ this.__pending_records.push([wf_body, body_size]);
262
+ } catch (ex) {
263
+ var inv_rec = (0, _utils.invalid_record_json)(wf.as_json(), ex);
264
+
265
+ this.__invalid_records.push(inv_rec);
266
+ }
262
267
  }
263
268
  } catch (err) {
264
269
  _iterator.e(err);
@@ -307,31 +312,21 @@ var BulkWorkflows = /*#__PURE__*/function () {
307
312
  workflows[_key] = arguments[_key];
308
313
  }
309
314
 
310
- if (!workflows) {
311
- throw new _utils.SuprsendError("workflow list empty. must pass one or more valid workflow instances");
312
- }
313
-
314
315
  for (var _i = 0, _workflows = workflows; _i < _workflows.length; _i++) {
315
316
  var wf = _workflows[_i];
316
317
 
317
- if (!wf) {
318
- throw new _utils.SuprsendError("null/empty element found in bulk instance");
319
- }
318
+ if (wf && wf instanceof _workflow["default"]) {
319
+ var wf_copy = (0, _lodash.cloneDeep)(wf);
320
320
 
321
- if (!(wf instanceof _workflow["default"])) {
322
- throw new _utils.SuprsendError("element must be an instance of suprsend.Workflow");
321
+ this.__workflows.push(wf_copy);
323
322
  }
324
-
325
- var wf_copy = (0, _lodash.cloneDeep)(wf);
326
-
327
- this.__workflows.push(wf_copy);
328
323
  }
329
324
  }
330
325
  }, {
331
326
  key: "trigger",
332
327
  value: function () {
333
328
  var _trigger2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2() {
334
- var _iterator3, _step3, _step3$value, c_idx, ch;
329
+ var ch_response, _iterator3, _step3, _step3$value, c_idx, ch;
335
330
 
336
331
  return _regenerator["default"].wrap(function _callee2$(_context2) {
337
332
  while (1) {
@@ -339,16 +334,26 @@ var BulkWorkflows = /*#__PURE__*/function () {
339
334
  case 0:
340
335
  this.__validate_workflows();
341
336
 
337
+ if (this.__invalid_records.length > 0) {
338
+ ch_response = _bulk_response["default"].invalid_records_chunk_response(this.__invalid_records);
339
+ this.response.merge_chunk_response(ch_response);
340
+ }
341
+
342
+ if (!this.__pending_records.length) {
343
+ _context2.next = 25;
344
+ break;
345
+ }
346
+
342
347
  this.__chunkify();
343
348
 
344
349
  _iterator3 = _createForOfIteratorHelper(this.chunks.entries());
345
- _context2.prev = 3;
350
+ _context2.prev = 5;
346
351
 
347
352
  _iterator3.s();
348
353
 
349
- case 5:
354
+ case 7:
350
355
  if ((_step3 = _iterator3.n()).done) {
351
- _context2.next = 13;
356
+ _context2.next = 15;
352
357
  break;
353
358
  }
354
359
 
@@ -359,43 +364,52 @@ var BulkWorkflows = /*#__PURE__*/function () {
359
364
  } // do api call
360
365
 
361
366
 
362
- _context2.next = 10;
367
+ _context2.next = 12;
363
368
  return ch.trigger();
364
369
 
365
- case 10:
370
+ case 12:
366
371
  // merge response
367
372
  this.response.merge_chunk_response(ch.response);
368
373
 
369
- case 11:
370
- _context2.next = 5;
371
- break;
372
-
373
374
  case 13:
374
- _context2.next = 18;
375
+ _context2.next = 7;
375
376
  break;
376
377
 
377
378
  case 15:
378
- _context2.prev = 15;
379
- _context2.t0 = _context2["catch"](3);
379
+ _context2.next = 20;
380
+ break;
381
+
382
+ case 17:
383
+ _context2.prev = 17;
384
+ _context2.t0 = _context2["catch"](5);
380
385
 
381
386
  _iterator3.e(_context2.t0);
382
387
 
383
- case 18:
384
- _context2.prev = 18;
388
+ case 20:
389
+ _context2.prev = 20;
385
390
 
386
391
  _iterator3.f();
387
392
 
388
- return _context2.finish(18);
393
+ return _context2.finish(20);
394
+
395
+ case 23:
396
+ _context2.next = 26;
397
+ break;
398
+
399
+ case 25:
400
+ if (this.__invalid_records.length === 0) {
401
+ this.response.merge_chunk_response(_bulk_response["default"].empty_chunk_success_response());
402
+ }
389
403
 
390
- case 21:
404
+ case 26:
391
405
  return _context2.abrupt("return", this.response);
392
406
 
393
- case 22:
407
+ case 27:
394
408
  case "end":
395
409
  return _context2.stop();
396
410
  }
397
411
  }
398
- }, _callee2, this, [[3, 15, 18, 21]]);
412
+ }, _callee2, this, [[5, 17, 20, 23]]);
399
413
  }));
400
414
 
401
415
  function trigger() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suprsend/node-sdk",
3
- "version": "1.7.1",
3
+ "version": "1.8.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
@@ -7,18 +7,30 @@ function check_is_web_url(file_path = "") {
7
7
  }
8
8
 
9
9
  function get_attachment_json_for_file(file_path, file_name, ignore_if_error) {
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();
10
+ try {
11
+ const abs_path = path.resolve(resolveTilde(file_path));
12
+ let final_file_name = path.basename(abs_path);
13
+ if (file_name && file_name.trim()) {
14
+ final_file_name = file_name.trim();
15
+ }
16
+ return {
17
+ filename: final_file_name,
18
+ contentType: mime.lookup(abs_path),
19
+ data: base64Encode(abs_path),
20
+ url: null,
21
+ ignore_if_error: ignore_if_error,
22
+ };
23
+ } catch (ex) {
24
+ if (ignore_if_error) {
25
+ console.log(
26
+ "WARNING: ignoring error while processing attachment file.",
27
+ ex
28
+ );
29
+ return;
30
+ } else {
31
+ throw ex;
32
+ }
14
33
  }
15
- return {
16
- filename: final_file_name,
17
- contentType: mime.lookup(abs_path),
18
- data: base64Encode(abs_path),
19
- url: null,
20
- ignore_if_error: ignore_if_error,
21
- };
22
34
  }
23
35
 
24
36
  function get_attachment_json_for_url(file_url, file_name, ignore_if_error) {
@@ -32,4 +32,26 @@ export default class BulkResponse {
32
32
  const failed_recs = ch_resp.failed_records || [];
33
33
  this.failed_records = [...this.failed_records, ...failed_recs];
34
34
  }
35
+
36
+ static empty_chunk_success_response() {
37
+ return {
38
+ status: "success",
39
+ status_code: 200,
40
+ total: 0,
41
+ success: 0,
42
+ failure: 0,
43
+ failed_records: [],
44
+ };
45
+ }
46
+
47
+ static invalid_records_chunk_response(invalid_records) {
48
+ return {
49
+ status: "fail",
50
+ status_code: 500,
51
+ total: invalid_records.length,
52
+ success: 0,
53
+ failure: invalid_records.length,
54
+ failed_records: invalid_records,
55
+ };
56
+ }
35
57
  }