@xapp/chat-widget 1.46.3 → 1.47.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.
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();
@@ -3056,7 +3079,8 @@ function getConfigurableMessages() {
3056
3079
  }
3057
3080
  function getConfigurableMessagesConfig(messages) {
3058
3081
  var items = messages.items;
3059
- var config = [{ retry: 0, delay: 5, text: "" }];
3082
+ // First one is silent, keeps the same message
3083
+ var config = [{ retry: 0, delay: 10, text: "" }];
3060
3084
  if (Array.isArray(items) && items.length > 0) {
3061
3085
  for (var i = 0; i < items.length; i++) {
3062
3086
  config.push({ retry: i + 1, delay: items[i].delay, text: items[i].text });
@@ -3160,8 +3184,7 @@ var StentorDirectChat = /** @class */ (function () {
3160
3184
  }
3161
3185
  });
3162
3186
  };
3163
- StentorDirectChat.prototype.sendFailureMessage = function (retry, delay, text, config) {
3164
- var _this = this;
3187
+ StentorDirectChat.prototype.sendFailureMessage = function (retry, delay, text) {
3165
3188
  this.stopTyping();
3166
3189
  this.dispatch({
3167
3190
  type: "chat",
@@ -3178,11 +3201,6 @@ var StentorDirectChat = /** @class */ (function () {
3178
3201
  timestamp: +new Date()
3179
3202
  }
3180
3203
  });
3181
- if (retry !== config.length - 1) {
3182
- setTimeout(function () {
3183
- _this.typing();
3184
- }, config[retry - 1].delay * 1000);
3185
- }
3186
3204
  };
3187
3205
  // private disconnect() {
3188
3206
  // log("SERVER: disconnect");
@@ -3271,7 +3289,7 @@ var StentorDirectChat = /** @class */ (function () {
3271
3289
  StentorDirectChat.prototype.postMessage = function (message) {
3272
3290
  var _a, _b, _c, _d;
3273
3291
  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;
3292
+ var request, userId, sessionId, accessToken, attributes, now, permissionRequest, expired, text, granted, userProfile, isEmail, configurableMessages, botResponse, successResult, success, fail, i, timeout, responseMessage;
3275
3293
  var _this = this;
3276
3294
  return __generator$1(this, function (_e) {
3277
3295
  switch (_e.label) {
@@ -3375,56 +3393,47 @@ var StentorDirectChat = /** @class */ (function () {
3375
3393
  log("Visitor says: ".concat(JSON.stringify(request, undefined, 2)));
3376
3394
  configurableMessages = getConfigurableMessagesConfig(this.configurableMessages);
3377
3395
  successResult = false;
3378
- this.typing();
3379
- _loop_1 = function (i) {
3396
+ success = function (result) {
3397
+ botResponse = result;
3398
+ successResult = true;
3399
+ };
3400
+ fail = function (i, error) { return __awaiter$1(_this, void 0, void 0, function () {
3380
3401
  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
- }
3402
+ return __generator$1(this, function (_a) {
3403
+ retry = configurableMessages[i].retry;
3404
+ delay = configurableMessages[i].delay;
3405
+ text = configurableMessages[i].text;
3406
+ err("POST failed: ".concat(error));
3407
+ this.sendFailureMessage(retry, delay, text);
3408
+ botResponse = { errorText: "" };
3409
+ // We need to delay before we actually make another fetch call again
3410
+ return [2 /*return*/, new Promise(function (resolve) {
3411
+ // * 1000 to make it seconds
3412
+ setTimeout(resolve, delay * 1000);
3413
+ })];
3416
3414
  });
3417
- };
3418
- this_1 = this;
3415
+ }); };
3419
3416
  i = 1;
3420
3417
  _e.label = 1;
3421
3418
  case 1:
3422
3419
  if (!(i < configurableMessages.length)) return [3 /*break*/, 4];
3423
- return [5 /*yield**/, _loop_1(i)];
3420
+ timeout = configurableMessages[i - 1].delay * 1000;
3421
+ this.typing();
3422
+ // We don't start typing IF we have a failure message displayed and we are waiting on the timeout
3423
+ return [4 /*yield*/, new RetryRequest({ retries: 1, timeout: timeout })
3424
+ .fetch(function (signal) {
3425
+ return postMessageToStentor(request, _this.config.url, _this.config.key, signal);
3426
+ })
3427
+ .then(success)
3428
+ // bind the i, the current iteration, to the fail function for looking up the message
3429
+ .catch(fail.bind(this, i))];
3424
3430
  case 2:
3425
- state_1 = _e.sent();
3426
- if (state_1 === "break")
3431
+ // We don't start typing IF we have a failure message displayed and we are waiting on the timeout
3432
+ _e.sent();
3433
+ // If we have what we want, break the for loop
3434
+ if (successResult) {
3427
3435
  return [3 /*break*/, 4];
3436
+ }
3428
3437
  _e.label = 3;
3429
3438
  case 3:
3430
3439
  i++;
@@ -30898,7 +30907,7 @@ var ChatWidgetWrapper = function (props) {
30898
30907
  bot: {
30899
30908
  nick: "Bot",
30900
30909
  displayName: rawConfig.botName,
30901
- avatarPath: rawConfig.avatarUrl
30910
+ avatarPath: rawConfig.avatarUrl,
30902
30911
  },
30903
30912
  configurableMessages: configurableMessages
30904
30913
  };