UNPKG

@scalar/api-reference

Version:

Generate beautiful API references from OpenAPI documents

92 lines (91 loc) 2.82 kB
import { isDefined as y } from "@scalar/oas-utils/helpers"; import { isConfigurationWithSources as h, apiReferenceConfigurationSchema as v } from "@scalar/types/api-reference"; import R from "github-slugger"; import { computed as m, ref as D, watch as b } from "vue"; const g = "api", w = new R(), A = (t) => t ? (Array.isArray(t) ? t : [t]).flatMap((u) => { if (h(u)) { const { sources: o, ...f } = u; return o?.map((n) => ({ ...f, ...n })) ?? []; } return [u]; }).map((u, o) => u && C(u, o)).filter(y) : [], C = (t, i = 0) => { const e = { ...t, // @ts-expect-error this is before parsing so we transform the old style ...t.spec ?? {} }; if (!(!e?.url && !e?.content)) return w.reset(), e.title ? { ...e, slug: e.slug || w.slug(e.title), title: e.title } : e.slug ? { ...e, title: e.slug } : { ...e, slug: `api-${i + 1}`, title: `API #${i + 1}` }; }, U = ({ configuration: t, configurationOverrides: i, initialIndex: e, isIntersectionEnabled: u, hash: o, hashPrefix: f }) => { const n = m(() => A(t.value)), s = D((() => { if (typeof window > "u") return typeof e == "number" ? e : 0; const r = new URL(window.location.href).searchParams.get(g); if (r) { const l = n.value.findIndex((S) => S.slug === r); if (l !== -1) return l; const d = Number.parseInt(r, 10); if (!isNaN(d) && d >= 0 && d < n.value.length) return d; } const p = n.value.findIndex((l) => "default" in l && l.default === !0); return p !== -1 ? p : typeof e == "number" ? e : 0; })()), c = m(() => { const a = i?.value ?? {}; if (t.value && h(t.value)) return v.parse({ ...t.value, ...t.value?.sources?.[s.value], ...n.value[s.value], ...a }); const r = [t.value].flat()[s.value] ?? {}; return v.parse({ ...r, ...n.value[s.value], ...a }); }); return b( s, (a) => { if (typeof window > "u" || n.value.length === 1) return; const r = new URL(window.location.href), p = n.value[a]; c.value.pathRouting && (r.pathname = c.value.pathRouting?.basePath ?? ""); const l = p?.slug ?? a.toString(); r.searchParams.set(g, l), r.hash = "", window.history.replaceState({}, "", r.toString()), o.value = "", f.value = "", u.value = !1, typeof window < "u" && window.scrollTo({ top: 0, behavior: "instant" }), typeof c.value.onDocumentSelect == "function" && c.value.onDocumentSelect?.(); }, { flush: "sync" } ), { selectedConfiguration: c, availableDocuments: n, selectedDocumentIndex: s, isIntersectionEnabled: u, hash: o, hashPrefix: f }; }; export { A as normalizeConfigurations, U as useMultipleDocuments };