UNPKG

@adyen/adyen-platform-experience-web

Version:

![Platform Experience header](https://github.com/Adyen/adyen-platform-experience-web/assets/7926613/18094965-9e01-450e-8dc9-ea84e6b22c2b)

51 lines (50 loc) 1.8 kB
import { getRequestObject as h, getResponseContentType as p, getResponseDownloadFilename as y, getErrorType as g, isAdyenErrorResponse as R, ErrorTypes as w, handleFetchError as E } from "./utils.js"; import { API_VERSION as C } from "./constants.js"; import { normalizeLoadingContext as T, normalizeUrl as b } from "../utils.js"; const c = (r, o) => { try { r == null || r(o); } catch { throw o; } }; async function I(r) { const { errorLevel: o, loadingContext: i = "", path: d } = r, u = h(r), s = new URL(`${T(i)}${C}${b(d)}`); return r.params && r.params.forEach((n, t) => { const e = decodeURIComponent(n); e && s.searchParams.append(t, e); }), (async () => { let n = !1; const t = { level: o }; try { const e = await fetch(s, u); if (e.ok) { if (e.status === 204) return null; try { switch (p(e)) { case "application/json": return await e.json(); // (!!) default: const f = await e.blob(), m = y(e); return { blob: f, filename: m }; } } catch (l) { throw n = !0, l; } } t.type = g(e.status); const a = await e.json(); t.message = r.errorMessage || `Service at ${s} not available`, t.errorCode = (a == null ? void 0 : a.status) == null ? void 0 : String(a.status), t.requestId = a == null ? void 0 : a.requestId, R(a) && (t.message = a.detail, t.errorCode = a.errorCode, t.status = a.status), c(r.errorHandler, t); } catch (e) { if (n) throw c(r.errorHandler, e), e; t.type || (t.type = w.NETWORK_ERROR), c(r.errorHandler, e), t.message = r.errorMessage || `Call to ${s} failed. Error: ${e}`; } E(t); })(); } export { I as http };