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.mjs CHANGED
@@ -367,7 +367,7 @@ function getFetch2() {
367
367
  throw new Error("fetch n\xE3o dispon\xEDvel. Use Node.js 18+ ou instale node-fetch.");
368
368
  }
369
369
  function formatToken(token) {
370
- return token.startsWith("Bearer ") ? token : `Bearer ${token}`;
370
+ return `Bearer ${stripBearer(token)}`;
371
371
  }
372
372
  function buildHeaders() {
373
373
  const config = getConfig();
@@ -397,7 +397,7 @@ async function tryRefreshToken() {
397
397
  if (resp.ok) {
398
398
  const data = await resp.json();
399
399
  if (data.token) {
400
- const newToken = data.token.startsWith("Bearer ") ? data.token : `Bearer ${data.token}`;
400
+ const newToken = `Bearer ${stripBearer(data.token)}`;
401
401
  const session = {
402
402
  token: newToken,
403
403
  baseURL: ((_a = data.merge) == null ? void 0 : _a.backURL) || config.baseURL,
@@ -1342,7 +1342,7 @@ function getRawToken() {
1342
1342
  if (!config.token) {
1343
1343
  throw new Error("Agent Chat: token n\xE3o configurado. Fa\xE7a login primeiro ou passe token em configureSdkMitra.");
1344
1344
  }
1345
- return config.token.startsWith("Bearer ") ? config.token.slice(7) : config.token;
1345
+ return stripBearer(config.token);
1346
1346
  }
1347
1347
  function resolveProjectId3(override) {
1348
1348
  const config = getConfig();
@@ -1650,7 +1650,7 @@ function sleep(ms) {
1650
1650
 
1651
1651
  // src/instance.ts
1652
1652
  function formatToken2(token) {
1653
- return token.startsWith("Bearer ") ? token : `Bearer ${token}`;
1653
+ return `Bearer ${stripBearer(token)}`;
1654
1654
  }
1655
1655
  function getFetch3() {
1656
1656
  if (typeof globalThis.fetch === "function") return globalThis.fetch;
@@ -1663,7 +1663,7 @@ function createMitraInstance(initialConfig) {
1663
1663
  var _a;
1664
1664
  let _config = {
1665
1665
  baseURL: ((_a = initialConfig.baseURL) == null ? void 0 : _a.replace(/\/+$/, "")) || "",
1666
- token: initialConfig.token || "",
1666
+ token: initialConfig.token ? stripBearer(initialConfig.token) : "",
1667
1667
  integrationURL: initialConfig.integrationURL,
1668
1668
  authUrl: initialConfig.authUrl,
1669
1669
  projectId: initialConfig.projectId
@@ -1700,13 +1700,13 @@ function createMitraInstance(initialConfig) {
1700
1700
  if (resp.ok) {
1701
1701
  const data = await resp.json();
1702
1702
  if (data.token) {
1703
- const newToken = data.token.startsWith("Bearer ") ? data.token : `Bearer ${data.token}`;
1703
+ const newToken = `Bearer ${stripBearer(data.token)}`;
1704
1704
  const session = {
1705
1705
  token: newToken,
1706
1706
  baseURL: ((_a2 = data.merge) == null ? void 0 : _a2.backURL) || _config.baseURL,
1707
1707
  ...((_b = data.merge) == null ? void 0 : _b.integrationURL) ? { integrationURL: data.merge.integrationURL } : {}
1708
1708
  };
1709
- _config = { ..._config, token: session.token };
1709
+ _config = { ..._config, token: stripBearer(session.token) };
1710
1710
  if (_config.onTokenRefresh) _config.onTokenRefresh(session);
1711
1711
  return true;
1712
1712
  }
@@ -1716,7 +1716,7 @@ function createMitraInstance(initialConfig) {
1716
1716
  if (!_config.authUrl || typeof window === "undefined") return false;
1717
1717
  try {
1718
1718
  const session = await refreshTokenSilently(_config.authUrl, _config.projectId);
1719
- _config = { ..._config, token: session.token };
1719
+ _config = { ..._config, token: stripBearer(session.token) };
1720
1720
  if (_config.onTokenRefresh) _config.onTokenRefresh(session);
1721
1721
  return true;
1722
1722
  } catch (e) {
@@ -2018,6 +2018,13 @@ function createMitraInstance(initialConfig) {
2018
2018
 
2019
2019
  // src/config.ts
2020
2020
  var globalConfig = null;
2021
+ function stripBearer(token) {
2022
+ let t = token.trim();
2023
+ while (/^bearer\s+/i.test(t)) {
2024
+ t = t.replace(/^bearer\s+/i, "").trim();
2025
+ }
2026
+ return t;
2027
+ }
2021
2028
  function readInjectedEnv() {
2022
2029
  if (typeof window === "undefined") return {};
2023
2030
  const env = window.__mitraEnv;
@@ -2034,6 +2041,7 @@ function configureSdkMitra(config = {}) {
2034
2041
  if (!merged.baseURL) {
2035
2042
  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).");
2036
2043
  }
2044
+ if (merged.token != null) merged.token = stripBearer(merged.token);
2037
2045
  globalConfig = {
2038
2046
  ...merged,
2039
2047
  baseURL: merged.baseURL.replace(/\/+$/, "")
@@ -2055,7 +2063,7 @@ function isConfigured() {
2055
2063
  }
2056
2064
  function updateGlobalToken(token) {
2057
2065
  if (globalConfig) {
2058
- globalConfig = { ...globalConfig, token };
2066
+ globalConfig = { ...globalConfig, token: stripBearer(token) };
2059
2067
  }
2060
2068
  }
2061
2069
  function resolveProjectId2(projectId) {