@scalar/api-client
Version:
the open source API testing client
41 lines (40 loc) • 1.68 kB
JavaScript
import { replaceEnvVariables as i } from "@scalar/helpers/regex/replace-variables";
import { unpackProxyObject as m } from "@scalar/workspace-store/helpers/unpack-proxy";
import { getSelectedBodyContentType as d } from "./get-selected-body-content-type.js";
import { getExampleFromBody as y } from "../../request-block/helpers/get-request-body-example.js";
const j = (u, o = {}, c = "default") => {
if (!u)
return null;
const n = d(u, c);
if (!n)
return null;
const e = y(u, n, c);
if (!e)
return null;
if ((n === "multipart/form-data" || n === "application/x-www-form-urlencoded") && Array.isArray(e.value)) {
const f = e.value.filter((r) => !r.isDisabled), a = n === "multipart/form-data" ? new FormData() : new URLSearchParams();
return f.forEach(({ name: r, value: l }) => {
if (!r)
return;
const p = i(r, o);
if (l instanceof File && a instanceof FormData) {
const s = m(l);
a.append(p, s, s.name);
} else typeof l == "string" && a.append(p, i(l, o));
}), a;
}
if (n === "application/x-www-form-urlencoded" && e.value !== null && typeof e.value == "object" && !Array.isArray(e.value)) {
const f = new URLSearchParams();
for (const [a, r] of Object.entries(e.value))
if (a && r !== void 0 && r !== null) {
const l = i(a, o), p = typeof r == "string" ? r : String(r);
f.append(l, i(p, o));
}
return f;
}
const t = e.value !== null && typeof e.value == "object" ? m(e.value) : e.value;
return t instanceof File ? t : typeof t == "object" ? i(JSON.stringify(t), o) : typeof t == "string" ? i(t, o) : t;
};
export {
j as buildRequestBody
};