UNPKG

@scalar/api-client

Version:

the open source API testing client

54 lines (53 loc) 1.41 kB
import { parseCurlCommand as h } from "../parse-curl.js"; function l(o) { try { return JSON.parse(o); } catch { const s = {}; return o.split("&").forEach((a) => { const [r, e] = a.split("="); r && e && (s[decodeURIComponent(r)] = decodeURIComponent(e)); }), s; } } function C(o) { const s = h(o), { method: a = "get", url: r, body: e = "", headers: p = {}, servers: y, queryParameters: m = [] } = s, i = new URL(r).pathname, d = e?.includes("=") && !e.startsWith("{") ? "application/x-www-form-urlencoded" : p["Content-Type"] || "", c = e ? l(e) : {}, u = [ ...Array.isArray(m) ? m.map(({ key: n, value: t }) => ({ name: n, in: "query", schema: { type: typeof t, examples: [t] } })) : [], ...Object.entries(p || {}).map(([n, t]) => ({ name: n, in: "header", schema: { type: typeof t }, example: t })) ]; return { method: a, url: r, path: i, headers: p, servers: y ?? [], ...Object.keys(c).length > 0 && { requestBody: { content: { [d]: { schema: { type: "object", properties: Object.fromEntries( Object.entries(c).map(([n, t]) => [n, { type: typeof t }]) ) }, example: c } } } }, parameters: u }; } export { C as importCurlCommand };