@xapp/chat-widget 1.46.3 → 1.46.4

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/dist/index.js CHANGED
@@ -1468,33 +1468,54 @@ var RetryRequest = /** @class */ (function () {
1468
1468
  if (options) {
1469
1469
  this.options = Object.assign(new RetryOptions(), options);
1470
1470
  }
1471
- this.attempts = 0;
1472
1471
  }
1473
- RetryRequest.prototype.retry = function (executor) {
1474
- var _this = this;
1475
- return new Promise(function (resolve, reject) {
1476
- if (_this.attempts < _this.options.retries) {
1477
- setTimeout(function () {
1478
- _this.attempts++;
1479
- executor
1480
- .then(function (response) { return resolve(response); })
1481
- .catch(function (error) {
1482
- _this.lastError = error;
1483
- _this.log("Retry: ".concat(_this.attempts, " : ").concat(JSON.stringify(error)));
1484
- return _this.retry(executor).then(resolve).catch(reject);
1485
- });
1486
- }, _this.options.timeout);
1487
- }
1488
- else {
1489
- reject(_this.lastError);
1490
- }
1472
+ /**
1473
+ * Fetch with retry and timeout
1474
+ *
1475
+ * Help from https://stackoverflow.com/a/66499718
1476
+ * @param executor
1477
+ * @param options
1478
+ * @returns
1479
+ */
1480
+ RetryRequest.prototype.fetch = function (executor, options) {
1481
+ return __awaiter$1(this, void 0, void 0, function () {
1482
+ var controller_1, timeoutId, response, e_1;
1483
+ return __generator$1(this, function (_a) {
1484
+ switch (_a.label) {
1485
+ case 0:
1486
+ if (!options) {
1487
+ options = this.options;
1488
+ }
1489
+ _a.label = 1;
1490
+ case 1:
1491
+ _a.trys.push([1, 3, , 5]);
1492
+ controller_1 = new AbortController();
1493
+ timeoutId = setTimeout(function () {
1494
+ controller_1.abort();
1495
+ }, options.timeout);
1496
+ return [4 /*yield*/, executor(controller_1.signal)];
1497
+ case 2:
1498
+ response = _a.sent();
1499
+ clearTimeout(timeoutId);
1500
+ return [2 /*return*/, response];
1501
+ case 3:
1502
+ e_1 = _a.sent();
1503
+ if (options.retries === 1 || options.retries < 1) {
1504
+ // Failed
1505
+ throw (e_1);
1506
+ }
1507
+ return [4 /*yield*/, this.fetch(executor, {
1508
+ timeout: options.timeout,
1509
+ retries: options.retries - 1
1510
+ })];
1511
+ case 4:
1512
+ // decrement the retries
1513
+ return [2 /*return*/, _a.sent()];
1514
+ case 5: return [2 /*return*/];
1515
+ }
1516
+ });
1491
1517
  });
1492
1518
  };
1493
- RetryRequest.prototype.log = function (message) {
1494
- if (this.options.log && console && console.log) {
1495
- console.log(message);
1496
- }
1497
- };
1498
1519
  return RetryRequest;
1499
1520
  }());
1500
1521
 
@@ -1508,8 +1529,9 @@ function log() {
1508
1529
  for (var _i = 0; _i < arguments.length; _i++) {
1509
1530
  args[_i] = arguments[_i];
1510
1531
  }
1511
- // TODO: only log in debug
1512
- console.log.apply(console, __spreadArray$1([getCurrentDateString()], args, false));
1532
+ if (window.xaLogLevel === "debug") {
1533
+ console.log.apply(console, __spreadArray$1([getCurrentDateString()], args, false));
1534
+ }
1513
1535
  }
1514
1536
  function err() {
1515
1537
  var args = [];
@@ -2840,7 +2862,7 @@ dist.useJsonFetch = useJsonFetch;
2840
2862
  var useSuggestionsFetch_1 = dist.useSuggestionsFetch = useSuggestionsFetch$1;
2841
2863
  var uuid_1 = dist.uuid = uuid;
2842
2864
 
2843
- function postMessageToStentor(data, url, key) {
2865
+ function postMessageToStentor(data, url, key, signal) {
2844
2866
  return __awaiter$1(this, void 0, void 0, function () {
2845
2867
  var body, response;
2846
2868
  return __generator$1(this, function (_a) {
@@ -2857,7 +2879,8 @@ function postMessageToStentor(data, url, key) {
2857
2879
  "User-Agent": "Stentor Chat Server"
2858
2880
  },
2859
2881
  body: body,
2860
- mode: "cors"
2882
+ mode: "cors",
2883
+ signal: signal
2861
2884
  })];
2862
2885
  case 1:
2863
2886
  response = _a.sent();
@@ -3160,8 +3183,7 @@ var StentorDirectChat = /** @class */ (function () {
3160
3183
  }
3161
3184
  });
3162
3185
  };
3163
- StentorDirectChat.prototype.sendFailureMessage = function (retry, delay, text, config) {
3164
- var _this = this;
3186
+ StentorDirectChat.prototype.sendFailureMessage = function (retry, delay, text) {
3165
3187
  this.stopTyping();
3166
3188
  this.dispatch({
3167
3189
  type: "chat",
@@ -3178,11 +3200,6 @@ var StentorDirectChat = /** @class */ (function () {
3178
3200
  timestamp: +new Date()
3179
3201
  }
3180
3202
  });
3181
- if (retry !== config.length - 1) {
3182
- setTimeout(function () {
3183
- _this.typing();
3184
- }, config[retry - 1].delay * 1000);
3185
- }
3186
3203
  };
3187
3204
  // private disconnect() {
3188
3205
  // log("SERVER: disconnect");
@@ -3271,7 +3288,7 @@ var StentorDirectChat = /** @class */ (function () {
3271
3288
  StentorDirectChat.prototype.postMessage = function (message) {
3272
3289
  var _a, _b, _c, _d;
3273
3290
  return __awaiter$1(this, void 0, void 0, function () {
3274
- var request, userId, sessionId, accessToken, attributes, now, permissionRequest, expired, text, granted, userProfile, isEmail, botResponse, configurableMessages, successResult, _loop_1, this_1, i, state_1, responseMessage;
3291
+ var request, userId, sessionId, accessToken, attributes, now, permissionRequest, expired, text, granted, userProfile, isEmail, configurableMessages, botResponse, successResult, success, fail, i, timeout, responseMessage;
3275
3292
  var _this = this;
3276
3293
  return __generator$1(this, function (_e) {
3277
3294
  switch (_e.label) {
@@ -3375,56 +3392,47 @@ var StentorDirectChat = /** @class */ (function () {
3375
3392
  log("Visitor says: ".concat(JSON.stringify(request, undefined, 2)));
3376
3393
  configurableMessages = getConfigurableMessagesConfig(this.configurableMessages);
3377
3394
  successResult = false;
3378
- this.typing();
3379
- _loop_1 = function (i) {
3395
+ success = function (result) {
3396
+ botResponse = result;
3397
+ successResult = true;
3398
+ };
3399
+ fail = function (i, error) { return __awaiter$1(_this, void 0, void 0, function () {
3380
3400
  var retry, delay, text;
3381
- return __generator$1(this, function (_f) {
3382
- switch (_f.label) {
3383
- case 0:
3384
- retry = configurableMessages[i].retry;
3385
- delay = configurableMessages[i].delay;
3386
- text = configurableMessages[i].text;
3387
- //eslint-disable-next-line
3388
- return [4 /*yield*/, new RetryRequest({ retries: 1, timeout: (configurableMessages[i - 1].delay * 1000) + 3000 }).retry(postMessageToStentor(request, this_1.config.url, this_1.config.key)).then(function (result) {
3389
- //eslint-disable-next-line
3390
- //@ts-ignore
3391
- botResponse = result;
3392
- successResult = true;
3393
- //eslint-disable-next-line
3394
- }, function (error) {
3395
- err("POST failed: ".concat(error));
3396
- _this.sendFailureMessage(retry, delay, text, configurableMessages);
3397
- //eslint-disable-next-line
3398
- //@ts-ignore
3399
- botResponse = { errorText: "" };
3400
- //eslint-disable-next-line
3401
- }).catch(function (postError) {
3402
- err("POST failed: ".concat(postError.message));
3403
- _this.sendFailureMessage(retry, delay, text, configurableMessages);
3404
- //eslint-disable-next-line
3405
- //@ts-ignore
3406
- botResponse = { errorText: "" };
3407
- })];
3408
- case 1:
3409
- //eslint-disable-next-line
3410
- _f.sent();
3411
- if (successResult) {
3412
- return [2 /*return*/, "break"];
3413
- }
3414
- return [2 /*return*/];
3415
- }
3401
+ return __generator$1(this, function (_a) {
3402
+ retry = configurableMessages[i].retry;
3403
+ delay = configurableMessages[i].delay;
3404
+ text = configurableMessages[i].text;
3405
+ err("POST failed: ".concat(error));
3406
+ this.sendFailureMessage(retry, delay, text);
3407
+ botResponse = { errorText: "" };
3408
+ // We need to delay before we actually make another fetch call again
3409
+ return [2 /*return*/, new Promise(function (resolve) {
3410
+ // * 1000 to make it seconds
3411
+ setTimeout(resolve, delay * 1000);
3412
+ })];
3416
3413
  });
3417
- };
3418
- this_1 = this;
3414
+ }); };
3419
3415
  i = 1;
3420
3416
  _e.label = 1;
3421
3417
  case 1:
3422
3418
  if (!(i < configurableMessages.length)) return [3 /*break*/, 4];
3423
- return [5 /*yield**/, _loop_1(i)];
3419
+ timeout = configurableMessages[i - 1].delay * 1000;
3420
+ this.typing();
3421
+ // We don't start typing IF we have a failure message displayed and we are waiting on the timeout
3422
+ return [4 /*yield*/, new RetryRequest({ retries: 1, timeout: timeout })
3423
+ .fetch(function (signal) {
3424
+ return postMessageToStentor(request, _this.config.url, _this.config.key, signal);
3425
+ })
3426
+ .then(success)
3427
+ // bind the i, the current iteration, to the fail function for looking up the message
3428
+ .catch(fail.bind(this, i))];
3424
3429
  case 2:
3425
- state_1 = _e.sent();
3426
- if (state_1 === "break")
3430
+ // We don't start typing IF we have a failure message displayed and we are waiting on the timeout
3431
+ _e.sent();
3432
+ // If we have what we want, break the for loop
3433
+ if (successResult) {
3427
3434
  return [3 /*break*/, 4];
3435
+ }
3428
3436
  _e.label = 3;
3429
3437
  case 3:
3430
3438
  i++;
@@ -30898,7 +30906,7 @@ var ChatWidgetWrapper = function (props) {
30898
30906
  bot: {
30899
30907
  nick: "Bot",
30900
30908
  displayName: rawConfig.botName,
30901
- avatarPath: rawConfig.avatarUrl
30909
+ avatarPath: rawConfig.avatarUrl,
30902
30910
  },
30903
30911
  configurableMessages: configurableMessages
30904
30912
  };