@scalar/api-client
Version:
the open source API testing client
71 lines (70 loc) • 1.99 kB
JavaScript
import { parseCurlCommand as f } from "../../../../libs/parse-curl.js";
const b = (t) => {
try {
return JSON.parse(t);
} catch {
const e = {};
return t.split("&").forEach((r) => {
const [n, o] = r.split("=");
n && o && (e[decodeURIComponent(n)] = decodeURIComponent(o));
}), e;
}
}, j = (t, e) => {
if (e["Content-Type"])
return e["Content-Type"];
const r = t.trim();
return r.startsWith("{") || r.startsWith("[") ? "application/json" : t.includes("=") && !r.startsWith("{") ? "application/x-www-form-urlencoded" : "";
}, a = (t) => {
const e = typeof t;
return e === "string" ? "string" : e === "number" ? "number" : e === "boolean" ? "boolean" : t === null ? "null" : Array.isArray(t) ? "array" : e === "object" ? "object" : "string";
}, C = (t, e, r) => {
const n = Array.isArray(t) ? t.map(({ key: c, value: s }) => ({
name: c,
in: "query",
schema: { type: a(s) },
examples: {
[r]: { value: s }
}
})) : [], o = Object.entries(e || {}).map(([c, s]) => ({
name: c,
in: "header",
schema: { type: a(s) },
examples: {
[r]: { value: s }
}
}));
return [...n, ...o];
}, g = (t) => ({
type: "object",
properties: Object.fromEntries(
Object.entries(t).map(([r, n]) => [r, { type: a(n) }])
)
}), O = (t, e = "curl") => {
const r = f(t), { method: n = "get", url: o, body: c = "", headers: s = {}, servers: u = [], queryParameters: d = [] } = r, y = new URL(o).pathname, i = j(c, s), p = c ? b(c) : {}, l = C(d, s, e), m = u.map((h) => ({
url: h
}));
return {
path: y,
method: n,
operation: {
parameters: l,
requestBody: {
content: {
[i]: {
schema: g(p),
examples: {
[e]: { value: p }
}
}
},
"x-scalar-selected-content-type": {
[e]: i
}
},
servers: m.length ? m : void 0
}
};
};
export {
O as getOperationFromCurl
};