UNPKG

@scalar/api-client

Version:

the open source API testing client

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