mitra-interactions-sdk 1.0.60-beta.10 → 1.0.60-beta.11

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
@@ -290,18 +290,14 @@ async function postBffAuth(url, body) {
290
290
  headers: { "Content-Type": "application/json" },
291
291
  body: JSON.stringify(body)
292
292
  });
293
+ const text = await response.text().catch(() => "");
294
+ const { data, isJson } = parseBody(text);
293
295
  if (!response.ok) {
294
- const text2 = await response.text().catch(() => "");
295
- let parsed = null;
296
- try {
297
- parsed = text2 ? JSON.parse(text2) : null;
298
- } catch (e) {
299
- }
300
- const msg = (parsed == null ? void 0 : parsed.message) || (parsed == null ? void 0 : parsed.error) || `HTTP ${response.status}`;
301
- throw { message: msg, status: response.status, details: parsed };
296
+ const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
297
+ const msg = isJson && ((data == null ? void 0 : data.message) || (data == null ? void 0 : data.error)) || fallback;
298
+ throw { message: msg, status: response.status, details: data };
302
299
  }
303
- const text = await response.text().catch(() => "");
304
- return text ? JSON.parse(text) : null;
300
+ return data;
305
301
  }
306
302
  function normalizeResponse(tokens, authUrl) {
307
303
  return {
@@ -743,10 +739,11 @@ async function tryRefreshToken() {
743
739
  }
744
740
  async function handleResponse(response) {
745
741
  const text = await response.text();
746
- const data = text ? JSON.parse(text) : null;
742
+ const { data, isJson } = parseBody(text);
747
743
  if (!response.ok) {
748
- const base = (data == null ? void 0 : data.message) || (data == null ? void 0 : data.error) || `HTTP ${response.status}`;
749
- const msg = (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
744
+ const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
745
+ const base = isJson && ((data == null ? void 0 : data.message) || (data == null ? void 0 : data.error)) || fallback;
746
+ const msg = isJson && (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
750
747
  throw {
751
748
  message: msg,
752
749
  status: response.status,
@@ -2006,10 +2003,11 @@ function createMitraInstance(initialConfig) {
2006
2003
  }
2007
2004
  async function handleResponse2(response) {
2008
2005
  const text = await response.text();
2009
- const data = text ? JSON.parse(text) : null;
2006
+ const { data, isJson } = parseBody(text);
2010
2007
  if (!response.ok) {
2011
- const base = (data == null ? void 0 : data.message) || (data == null ? void 0 : data.error) || `HTTP ${response.status}`;
2012
- const msg = (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
2008
+ const fallback = !isJson && text ? `HTTP ${response.status} \u2014 ${text}` : `HTTP ${response.status}`;
2009
+ const base = isJson && ((data == null ? void 0 : data.message) || (data == null ? void 0 : data.error)) || fallback;
2010
+ const msg = isJson && (data == null ? void 0 : data.hint) ? `${base} \u2014 ${data.hint}` : base;
2013
2011
  throw { message: msg, status: response.status, details: data };
2014
2012
  }
2015
2013
  return data;
@@ -2355,6 +2353,14 @@ function stripBearer(token) {
2355
2353
  }
2356
2354
  return t;
2357
2355
  }
2356
+ function parseBody(text) {
2357
+ if (!text) return { data: null, isJson: true };
2358
+ try {
2359
+ return { data: JSON.parse(text), isJson: true };
2360
+ } catch (e) {
2361
+ return { data: text, isJson: false };
2362
+ }
2363
+ }
2358
2364
  var SESSION_STORE_KEY = "mitra-session";
2359
2365
  function safeLocalStorage() {
2360
2366
  try {