@adyen/adyen-platform-experience-web
Version:

93 lines (92 loc) • 2.85 kB
JavaScript
import { API_VERSION as i } from "./constants.js";
import a from "../Errors/AdyenPlatformExperienceError.js";
import { isNullish as c } from "../../utils/value/is.js";
const R = /^[^]*?filename[^;\n]*=\s*(?:UTF-\d['"]*)?(?:(['"])([^]*?)\1|([^;\n]*))?[^]*?$/;
var E = /* @__PURE__ */ ((e) => (e.HTTP_ERROR = "HTTP_ERROR", e.NETWORK_ERROR = "NETWORK_ERROR", e.CANCEL = "CANCEL", e.IMPLEMENTATION_ERROR = "IMPLEMENTATION_ERROR", e.ERROR = "ERROR", e.EXPIRED_TOKEN = "EXPIRED_TOKEN", e))(E || {});
const u = (e) => {
switch (e) {
case 401:
return "EXPIRED_TOKEN";
default:
return "HTTP_ERROR";
}
}, O = (e) => {
const [, n] = String(e.apiVersion).match(/^v?([1-9]\d*)$/i) ?? [];
return n ? `v${n}` : i;
}, T = (e) => e.headers.get("Content-Type")?.split(";", 1)[0], m = (e) => {
const t = (e.headers.get("Content-Disposition") ?? "").replace(R, "$2$3");
return decodeURIComponent(t);
}, l = (e, n) => {
switch (n) {
case "application/json":
return JSON.stringify(e);
case "multipart/form-data":
return e instanceof FormData ? e : new FormData();
default:
return String(e);
}
}, h = (e) => {
const { headers: n = [], method: t = "GET" } = e, r = !e.versionless && "1.11.0", o = e.skipContentType ? void 0 : e.contentType?.toLowerCase() ?? "application/json";
return {
method: t,
mode: "cors",
cache: "default",
credentials: "same-origin",
headers: {
Accept: "application/json, text/plain, */*",
...n,
// Skip Content-Type header for multipart/form-data requests
// The browser will automatically set the content-type for such requests
...o && o !== "multipart/form-data" && { "Content-Type": o },
...r && { "SDK-Version": r }
},
redirect: "follow",
signal: e.signal,
keepalive: e.keepalive,
referrerPolicy: "no-referrer-when-downgrade",
...t === "POST" && e.body && { body: l(e.body, o) }
};
};
function N({
message: e,
level: n,
errorCode: t,
type: r = "NETWORK_ERROR",
requestId: o,
invalidFields: s
}) {
switch (n) {
case "silent":
break;
case "info":
case "warn":
console[n](e);
break;
case "error":
default:
throw new a(r, o, e, t, s);
}
}
function g(e) {
return e && e.errorCode && e.type && (e.detail || e.invalidFields) && e.status;
}
function C(e) {
const n = new URLSearchParams();
for (const t of Object.keys(e)) {
const r = e[t];
c(r) || (Array.isArray(r) ? r.forEach((o) => n.append(t, o)) : n.set(t, String(r)));
}
return n;
}
export {
E as ErrorTypes,
O as getApiVersion,
u as getErrorType,
l as getRequestBodyForContentType,
h as getRequestObject,
T as getResponseContentType,
m as getResponseDownloadFilename,
N as handleFetchError,
g as isAdyenErrorResponse,
C as parseSearchParams
};