mitra-interactions-sdk 1.0.71 → 1.0.72

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.mjs CHANGED
@@ -48,12 +48,15 @@ function openAuthPopup(url, expectedOrigin) {
48
48
  }
49
49
  function autoConfigureFromLogin(response, authUrl, projectId) {
50
50
  if (!response.baseURL) return;
51
+ const previous = isConfigured() ? getConfig() : void 0;
51
52
  configureSdkMitra({
52
53
  baseURL: response.baseURL,
53
54
  token: response.token,
54
55
  authUrl,
55
56
  projectId,
56
- ...response.integrationURL ? { integrationURL: response.integrationURL } : {}
57
+ ...response.integrationURL ? { integrationURL: response.integrationURL } : {},
58
+ ...(previous == null ? void 0 : previous.onTokenRefresh) ? { onTokenRefresh: previous.onTokenRefresh } : {},
59
+ ...(previous == null ? void 0 : previous.onSessionExpired) ? { onSessionExpired: previous.onSessionExpired } : {}
57
60
  });
58
61
  }
59
62
  function buildAuthUrl(authUrl, method, projectId, opts) {
@@ -391,7 +394,8 @@ function withServerFunctionProof(headers, proof = readServerFunctionProof()) {
391
394
  }
392
395
 
393
396
  // src/session-watch.ts
394
- var RENEW_BEFORE_MS = 6e4;
397
+ var RENEW_BEFORE_MIN_MS = 6e4;
398
+ var RENEW_BEFORE_MAX_MS = 10 * 6e4;
395
399
  var RETRY_MS = 1e4;
396
400
  var MAX_TIMEOUT_MS = 2147483647;
397
401
  var refresher = null;
@@ -408,6 +412,14 @@ function limitedSessionExpiry(token) {
408
412
  if (!payload || typeof payload.authTime !== "number" || typeof payload.exp !== "number") return null;
409
413
  return payload.exp * 1e3;
410
414
  }
415
+ function renewBeforeMs() {
416
+ var _a;
417
+ if (!isConfigured()) return RENEW_BEFORE_MIN_MS;
418
+ const payload = decodeJwtPayload((_a = getConfig().token) != null ? _a : "");
419
+ const life = payload && typeof payload.exp === "number" && typeof payload.iat === "number" ? (payload.exp - payload.iat) * 1e3 : 0;
420
+ if (life <= 0) return RENEW_BEFORE_MIN_MS;
421
+ return Math.min(RENEW_BEFORE_MAX_MS, Math.max(RENEW_BEFORE_MIN_MS, Math.floor(life / 3)));
422
+ }
411
423
  function currentExpiry() {
412
424
  if (typeof window === "undefined" || !isConfigured()) return null;
413
425
  const config = getConfig();
@@ -430,7 +442,7 @@ function watchSession() {
430
442
  const expiry = currentExpiry();
431
443
  if (expiry == null) return;
432
444
  listenForReturn();
433
- let delay = Math.max(expiry - Date.now() - RENEW_BEFORE_MS, 0);
445
+ let delay = Math.max(expiry - Date.now() - renewBeforeMs(), 0);
434
446
  if (expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3) {
435
447
  delay = Math.max(expiry - Date.now() + 1e3, RETRY_MS);
436
448
  }
@@ -457,7 +469,7 @@ function onReturn() {
457
469
  const expiry = currentExpiry();
458
470
  if (expiry == null) return;
459
471
  const atSessionEnd = expiryAtLastAttempt != null && expiry <= expiryAtLastAttempt + 1e3;
460
- if (expiry - Date.now() <= RENEW_BEFORE_MS && (!atSessionEnd || Date.now() >= expiry)) {
472
+ if (expiry - Date.now() <= renewBeforeMs() && (!atSessionEnd || Date.now() >= expiry)) {
461
473
  void renewNow();
462
474
  } else {
463
475
  watchSession();