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