@scalar/api-reference
Version:
Generate beautiful API references from OpenAPI documents
70 lines (69 loc) • 2 kB
JavaScript
import { createWorkspaceStore as v, createActiveEntitiesStore as y } from "@scalar/api-client/store";
import { measureAsync as P } from "@scalar/helpers/testing/measure";
import { normalize as w, dereference as O } from "@scalar/openapi-parser";
import { upgrade as V } from "@scalar/openapi-upgrader";
import { apiReferenceConfigurationSchema as D } from "@scalar/types/api-reference";
import { computed as f, toValue as r, ref as l, watch as F } from "vue";
import { useDocumentFetcher as I } from "./useDocumentFetcher.js";
function L({
originalDocument: c,
dereferencedDocument: t,
configuration: i
}) {
const { originalDocument: d } = I({
configuration: i
}), s = f(() => c ? r(c) : r(d)), o = l(""), h = f(() => t ? r(t) : r(u)), u = l({
openapi: "3.1.0",
info: {
title: "",
version: ""
},
paths: {}
});
F(
() => r(s),
async (n) => {
if (!n)
return;
const e = w(n);
if (e === void 0) {
console.error("Failed to parse the OpenAPI document:", n);
return;
}
if (o.value = typeof e == "object" && (e.openapi || e.swagger) || "", t)
return;
const S = !o.value.startsWith("3.1") ? (
// Upgrade needed
V(e, "3.1")
) : (
// Skip the upgrade
e
), A = await P("dereference", async () => {
const { schema: p, errors: a } = await O(S);
return a?.length && console.warn(
`Please open an issue on https://github.com/scalar/scalar
`,
`Scalar OpenAPI Parser Warning:
`,
a
), p === void 0 && console.error("Failed to dereference the OpenAPI document", a), p;
});
u.value = A;
},
{ immediate: !0 }
);
const m = v({
useLocalStorage: !1,
...r(i) ?? D.parse({})
}), g = y(m);
return {
originalDocument: s,
originalOpenApiVersion: o,
dereferencedDocument: h,
workspaceStore: m,
activeEntitiesStore: g
};
}
export {
L as useDocumentSource
};