UNPKG

@scalar/api-client

Version:

the open source API testing client

54 lines (53 loc) 1.72 kB
import { json2xml as s } from "@scalar/helpers/file/json2xml"; import { getResolvedRef as c } from "@scalar/workspace-store/helpers/get-resolved-ref"; import { getExampleFromSchema as l } from "../get-example-from-schema.js"; import { getExample as u } from "../../../operation-block/helpers/get-example.js"; const p = (i) => { const n = []; for (const [a, e] of Object.entries(i)) if (e != null) if (Array.isArray(e)) for (const r of e) n.push({ name: a, value: String(r) }); else if (typeof e == "object") { const r = p(e); for (const m of r) n.push({ name: `${a}.${m.name}`, value: m.value }); } else n.push({ name: a, value: String(e) }); return n; }, j = ({ requestBody: i, contentType: n, example: a }) => { const e = n || Object.keys(i.content)[0] || "", r = e === "multipart/form-data" || e === "application/x-www-form-urlencoded", m = e === "application/xml", t = u(i, a, n)?.value; if (typeof t < "u") return r && typeof t == "object" && t !== null ? { mimeType: e, params: p(t) } : m && typeof t == "object" && t !== null ? { mimeType: e, text: s(t) } : { mimeType: e, text: typeof t == "string" ? t : JSON.stringify(t) }; const f = c(i.content[e]?.schema); if (typeof f < "u") { const o = l(f, { mode: "write", xml: m }); if (o !== void 0) return r && typeof o == "object" && o !== null ? { mimeType: e, params: p(o) } : m && typeof o == "object" && o !== null ? { mimeType: e, text: s(o) } : { mimeType: e, text: JSON.stringify(o) }; } }; export { j as processBody };