UNPKG

@scalar/api-client

Version:

the open source API testing client

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