@scalar/api-client
Version:
the open source API testing client
135 lines (134 loc) • 3.57 kB
JavaScript
import { isDefined as l } from "@scalar/helpers/array/is-defined";
import { sortByOrder as w } from "@scalar/helpers/array/sort-by-order";
import { createSidebarState as b, generateReverseIndex as k } from "@scalar/sidebar";
import { getParentEntry as o } from "@scalar/workspace-store/navigation";
import { computed as h, toValue as x, watch as f } from "vue";
import { useRouter as O } from "vue-router";
const A = ({
workspaceStore: d,
documentSlug: y,
path: E,
method: g,
exampleName: S
}) => {
const m = O(), c = h(() => {
const e = x(d);
if (!e)
return [];
const t = e.workspace["x-scalar-order"] ?? Object.keys(e.workspace.documents);
return w(Object.keys(e.workspace.documents), t, (r) => r).map((r) => e.workspace.documents[r]?.["x-scalar-navigation"]).filter(l);
}), n = b(c);
f(
() => x(d),
() => {
n.reset();
},
{
immediate: !0
}
);
const p = ({
document: e,
path: t,
method: r,
example: a
}) => JSON.stringify([e, t, r, a].filter(l)), u = h(
() => k({
items: c.value,
nestedKey: "children",
filter: (e) => e.type === "document" || e.type === "operation" || e.type === "example",
getId: (e) => {
const t = o("document", e), r = o("operation", e);
return p({
document: t?.name ?? "",
path: r?.path,
method: r?.method,
example: e.type === "example" ? e.name : void 0
});
}
})
), i = (e) => {
const t = u.value.get(p(e));
return t || u.value.get(
p({
document: e.document,
path: e.path,
method: e.method
})
);
}, v = (e) => {
const t = n.getEntryById(e);
if (!t) {
console.warn(`Could not find sidebar entry with id ${e} to select`);
return;
}
if (t.type === "document")
return n.setSelected(e), n.setExpanded(e, !n.isExpanded(e)), m.push({
name: "document.overview",
params: { documentSlug: t.name }
});
if (t.type === "operation") {
if (n.isSelected(e)) {
n.setExpanded(e, !n.isExpanded(e));
return;
}
const r = t.children?.find((a) => a.type === "example");
return r ? (n.setSelected(r.id), n.setExpanded(r.id, !0)) : n.setSelected(e), m.push({
name: "example",
params: {
documentSlug: o("document", t)?.name,
pathEncoded: encodeURIComponent(t.path),
method: t.method,
exampleName: r?.name ?? "default"
}
});
}
if (t.type === "example") {
n.setSelected(e), n.setExpanded(e, !0);
const r = o("operation", t);
return m.push({
name: "example",
params: {
documentSlug: o("document", t)?.name,
pathEncoded: encodeURIComponent(r?.path ?? ""),
method: r?.method,
exampleName: t.name
}
});
}
if (t.type === "text")
return m.push({
name: "document.overview",
params: {
documentSlug: o("document", t)?.name
}
});
n.setExpanded(e, !n.isExpanded(e));
};
return f(
[d, y, E, g, S],
([e, t, r, a, I]) => {
if (!t) {
n.setSelected(null);
return;
}
const s = i({
document: t,
path: r,
method: a,
example: I
});
s && (n.setSelected(s.id), n.setExpanded(s.id, !0));
},
{
immediate: !0
}
), {
handleSelectItem: v,
state: n,
getEntryByLocation: i
};
};
export {
A as useAppSidebar
};