UNPKG

@braze/web-sdk

Version:

Braze SDK for web sites and other JS platforms.

71 lines (70 loc) 2.12 kB
import { logger as N } from "../../shared-lib/index.js"; import Xt from "../models/backend-errors.js"; import { getErrorMessage as si } from "./error-utils.js"; const l = { Y: (t) => { let e, o; try { const r = () => { N.error("This browser does not have any supported ajax options!"); }; let n = !1; if ((window.XMLHttpRequest && (n = !0), !n)) return void r(); e = new XMLHttpRequest(); const s = (o) => { "function" == typeof t.error && t.error(e.status), "function" == typeof t.nt && t.nt(!1, o); }; (e.onload = () => { let o = !1; if (4 !== e.readyState) return; o = (e.status >= 200 && e.status < 300) || 304 === e.status; const r = e.getAllResponseHeaders(); if (o) { if ("function" == typeof t.tt) { let o; try { o = JSON.parse(e.responseText); } catch (o) { const n = { error: "" === e.responseText ? Xt.hl : Xt.cl, response: e.responseText, }; (0, t.tt)(n, r); } o && t.tt(o, r); } "function" == typeof t.nt && t.nt(!0, r); } else s(r); }), (e.onerror = () => { s(e.getAllResponseHeaders()); }), (e.ontimeout = () => { s(); }), (o = JSON.stringify(t.data)), e.open("POST", t.url, !0), e.setRequestHeader("Content-type", "application/json"), e.setRequestHeader("X-Requested-With", "XMLHttpRequest"); const i = t.headers || []; for (const t of i) e.setRequestHeader(t[0], t[1]); e.send(o); } catch (t) { N.error(`Network request error: ${si(t)}`); } }, }; export const readResponseHeaders = (t) => { const e = {}, o = t.toString().split("\r\n"); if (!o) return e; let r, n; for (const t of o) t && ((r = t.slice(0, t.indexOf(":")).toLowerCase().trim()), (n = t.slice(t.indexOf(":") + 1).trim()), (e[r] = n)); return e; }; export default l;