UNPKG

@scalar/api-client

Version:

the open source API testing client

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