mitra-interactions-sdk 1.0.58 → 1.0.59

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
@@ -369,7 +369,7 @@ function getFetch2() {
369
369
  throw new Error("fetch n\xE3o dispon\xEDvel. Use Node.js 18+ ou instale node-fetch.");
370
370
  }
371
371
  function formatToken(token) {
372
- return token.startsWith("Bearer ") ? token : `Bearer ${token}`;
372
+ return `Bearer ${stripBearer(token)}`;
373
373
  }
374
374
  function buildHeaders() {
375
375
  const config = getConfig();
@@ -399,7 +399,7 @@ async function tryRefreshToken() {
399
399
  if (resp.ok) {
400
400
  const data = await resp.json();
401
401
  if (data.token) {
402
- const newToken = data.token.startsWith("Bearer ") ? data.token : `Bearer ${data.token}`;
402
+ const newToken = `Bearer ${stripBearer(data.token)}`;
403
403
  const session = {
404
404
  token: newToken,
405
405
  baseURL: ((_a = data.merge) == null ? void 0 : _a.backURL) || config.baseURL,
@@ -1344,7 +1344,7 @@ function getRawToken() {
1344
1344
  if (!config.token) {
1345
1345
  throw new Error("Agent Chat: token n\xE3o configurado. Fa\xE7a login primeiro ou passe token em configureSdkMitra.");
1346
1346
  }
1347
- return config.token.startsWith("Bearer ") ? config.token.slice(7) : config.token;
1347
+ return stripBearer(config.token);
1348
1348
  }
1349
1349
  function resolveProjectId3(override) {
1350
1350
  const config = getConfig();
@@ -1652,7 +1652,7 @@ function sleep(ms) {
1652
1652
 
1653
1653
  // src/instance.ts
1654
1654
  function formatToken2(token) {
1655
- return token.startsWith("Bearer ") ? token : `Bearer ${token}`;
1655
+ return `Bearer ${stripBearer(token)}`;
1656
1656
  }
1657
1657
  function getFetch3() {
1658
1658
  if (typeof globalThis.fetch === "function") return globalThis.fetch;
@@ -1665,7 +1665,7 @@ function createMitraInstance(initialConfig) {
1665
1665
  var _a;
1666
1666
  let _config = {
1667
1667
  baseURL: ((_a = initialConfig.baseURL) == null ? void 0 : _a.replace(/\/+$/, "")) || "",
1668
- token: initialConfig.token || "",
1668
+ token: initialConfig.token ? stripBearer(initialConfig.token) : "",
1669
1669
  integrationURL: initialConfig.integrationURL,
1670
1670
  authUrl: initialConfig.authUrl,
1671
1671
  projectId: initialConfig.projectId
@@ -1702,13 +1702,13 @@ function createMitraInstance(initialConfig) {
1702
1702
  if (resp.ok) {
1703
1703
  const data = await resp.json();
1704
1704
  if (data.token) {
1705
- const newToken = data.token.startsWith("Bearer ") ? data.token : `Bearer ${data.token}`;
1705
+ const newToken = `Bearer ${stripBearer(data.token)}`;
1706
1706
  const session = {
1707
1707
  token: newToken,
1708
1708
  baseURL: ((_a2 = data.merge) == null ? void 0 : _a2.backURL) || _config.baseURL,
1709
1709
  ...((_b = data.merge) == null ? void 0 : _b.integrationURL) ? { integrationURL: data.merge.integrationURL } : {}
1710
1710
  };
1711
- _config = { ..._config, token: session.token };
1711
+ _config = { ..._config, token: stripBearer(session.token) };
1712
1712
  if (_config.onTokenRefresh) _config.onTokenRefresh(session);
1713
1713
  return true;
1714
1714
  }
@@ -1718,7 +1718,7 @@ function createMitraInstance(initialConfig) {
1718
1718
  if (!_config.authUrl || typeof window === "undefined") return false;
1719
1719
  try {
1720
1720
  const session = await refreshTokenSilently(_config.authUrl, _config.projectId);
1721
- _config = { ..._config, token: session.token };
1721
+ _config = { ..._config, token: stripBearer(session.token) };
1722
1722
  if (_config.onTokenRefresh) _config.onTokenRefresh(session);
1723
1723
  return true;
1724
1724
  } catch (e) {
@@ -2020,6 +2020,13 @@ function createMitraInstance(initialConfig) {
2020
2020
 
2021
2021
  // src/config.ts
2022
2022
  var globalConfig = null;
2023
+ function stripBearer(token) {
2024
+ let t = token.trim();
2025
+ while (/^bearer\s+/i.test(t)) {
2026
+ t = t.replace(/^bearer\s+/i, "").trim();
2027
+ }
2028
+ return t;
2029
+ }
2023
2030
  function readInjectedEnv() {
2024
2031
  if (typeof window === "undefined") return {};
2025
2032
  const env = window.__mitraEnv;
@@ -2036,6 +2043,7 @@ function configureSdkMitra(config = {}) {
2036
2043
  if (!merged.baseURL) {
2037
2044
  throw new Error("baseURL \xE9 obrigat\xF3rio. Passe em configureSdkMitra({ baseURL }), fa\xE7a login para receber via sdk-auth, ou rode em uma app publicada pelo Mitra (window.__mitraEnv).");
2038
2045
  }
2046
+ if (merged.token != null) merged.token = stripBearer(merged.token);
2039
2047
  globalConfig = {
2040
2048
  ...merged,
2041
2049
  baseURL: merged.baseURL.replace(/\/+$/, "")
@@ -2057,7 +2065,7 @@ function isConfigured() {
2057
2065
  }
2058
2066
  function updateGlobalToken(token) {
2059
2067
  if (globalConfig) {
2060
- globalConfig = { ...globalConfig, token };
2068
+ globalConfig = { ...globalConfig, token: stripBearer(token) };
2061
2069
  }
2062
2070
  }
2063
2071
  function resolveProjectId2(projectId) {