UNPKG

@scalar/api-client

Version:

the open source API testing client

23 lines (22 loc) 1.01 kB
import { replaceEnvVariables as i } from "@scalar/helpers/regex/replace-variables"; import { getExampleFromBody as c } from "../../request-block/helpers/get-request-body-example.js"; const u = (r, e = {}, l = "default") => { if (!r) return null; const a = r?.["x-scalar-selected-content-type"]?.[l] ?? Object.keys(r?.content ?? {})[0] ?? "application/json", t = c(r, a, l); if (!t) return null; if ((a === "multipart/form-data" || a === "application/x-www-form-urlencoded") && Array.isArray(t.value)) { const n = a === "multipart/form-data" ? new FormData() : new URLSearchParams(); return t.value.forEach(({ name: f, value: o }) => { if (!f) return; const p = i(f, e); o instanceof File && n instanceof FormData ? n.append(p, o) : typeof o == "string" && n.append(p, i(o, e)); }), n; } return typeof t.value == "object" ? i(JSON.stringify(t.value), e) : typeof t.value == "string" ? i(t.value, e) : t.value; }; export { u as buildRequestBody };