@scalar/api-client
Version:
the open source API testing client
58 lines (57 loc) • 1.72 kB
JavaScript
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 { getExampleValue as u } from "./get-example-value.js";
const p = (i) => {
const n = [];
for (const [a, e] of Object.entries(i))
if (e != null)
if (Array.isArray(e))
for (const m of e)
n.push({ name: a, value: String(m) });
else if (typeof e == "object") {
const m = p(e);
for (const r of m)
n.push({ name: `${a}.${r.name}`, value: r.value });
} else
n.push({ name: a, value: String(e) });
return n;
}, b = ({ requestBody: i, contentType: n, example: a }) => {
const e = n || Object.keys(i.content)[0] || "", m = e === "multipart/form-data" || e === "application/x-www-form-urlencoded", r = e === "application/xml", t = u(i, a, n);
if (typeof t < "u")
return m && typeof t == "object" && t !== null ? {
mimeType: e,
params: p(t)
} : r && typeof t == "object" && t !== null ? {
mimeType: e,
text: s(t)
} : {
mimeType: e,
text: JSON.stringify(t)
};
const f = c(i.content[e]?.schema);
if (typeof f < "u") {
const o = l(f, {
mode: "write",
xml: r
});
if (o !== void 0)
return m && typeof o == "object" && o !== null ? {
mimeType: e,
params: p(o)
} : r && typeof o == "object" && o !== null ? {
mimeType: e,
text: s(o)
} : {
mimeType: e,
text: JSON.stringify(o)
};
}
return {
mimeType: e,
text: "null"
};
};
export {
b as processBody
};