UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

76 lines (75 loc) 2.06 kB
import { isDereferenced as u } from "@scalar/openapi-types/helpers"; import { getResolvedRef as s } from "@scalar/workspace-store/helpers/get-resolved-ref"; function p(t, o) { let i = t; const e = o.required?.includes(t); i += e ? " REQUIRED " : " optional "; const r = s(o.properties?.[t]); return r && (i += r.type, r.description && (i += " " + r.description)), i; } function d(t) { const o = ["Body"], i = s(t?.schema), e = i?.properties; return e && Object.keys(e).forEach((r) => { if (!t.schema) return; o.push(p(r, i)); const n = s(e[r]); n.type === "object" && !!n.properties && n.properties && Object.keys(n.properties).forEach((c) => { o.push(`${c} ${s(n.properties?.[c])?.type}`); }); }), o; } function a(t) { try { const o = s(t?.requestBody)?.content?.["application/json"]; if (!o) throw new Error("Body not found"); return d(o); } catch { return !1; } } function f(t, o) { for (const [i, e] of Object.entries(t)) e !== null && typeof e == "object" ? (o[i] ??= new e.__proto__.constructor(), f(e, o[i])) : typeof e < "u" && (o[i] = e); return o; } function b(t) { return f(t ?? {}, { info: { title: "", description: "", termsOfService: "", version: "", license: { name: "", url: "" }, contact: { email: "" } }, servers: [], tags: [] }); } function j(t) { const o = { path: [], query: [], header: [], cookie: [], body: [], formData: [] }, i = t.parameters ?? []; return i && i.forEach((e) => { u(e) && (typeof e == "object" && e !== null && "$ref" in e || (e.in === "path" ? o.path.push(e) : e.in === "query" ? o.query.push(e) : e.in === "header" ? o.header.push(e) : e.in === "cookie" ? o.cookie.push(e) : e.in === "body" ? o.body.push(e) : e.in === "formData" && o.formData.push(e))); }), o; } export { b as createEmptySpecification, j as createParameterMap, f as deepMerge, a as extractRequestBody, p as formatProperty };