@scalar/api-client
Version:
the open source API testing client
119 lines (118 loc) • 3.78 kB
JavaScript
import { environmentSchema as S } from "@scalar/oas-utils/entities/environment";
import { isDefined as U } from "@scalar/oas-utils/helpers";
import { inject as q, computed as r } from "vue";
import { flattenEnvVars as I } from "../libs/string-template.js";
import { PathId as l } from "../routes.js";
import { getRouterParams as A } from "./router-params.js";
const T = ({
collections: a,
requestExamples: b,
requests: u,
router: y,
servers: m,
workspaces: k
}) => {
const i = r(A(y)), t = r(
() => k[i.value[l.Workspace]] ?? Object.values(k)[0]
), s = r(
() => t.value?.collections.map((e) => a[e]).filter(U).sort((e, n) => e.info?.title === "Drafts" ? 1 : n.info?.title === "Drafts" ? -1 : 0) ?? []
), g = r(
() => s.value?.flatMap((e) => e.servers.map((n) => m[n]))
), x = r(
() => s.value?.flatMap((e) => e.requests) ?? []
), f = r(() => {
if (!t.value?.activeEnvironmentId)
return S.parse({
uid: "default",
color: "#FFFFFF",
name: "No Environment",
value: JSON.stringify(t.value?.environments, null, 2)
});
const e = s.value.find(
(n) => n["x-scalar-environments"]?.[t.value?.activeEnvironmentId ?? ""]
);
return e && t.value?.activeEnvironmentId ? S.parse({
uid: t.value.activeEnvironmentId,
name: t.value.activeEnvironmentId,
value: JSON.stringify(
e["x-scalar-environments"]?.[t.value?.activeEnvironmentId]?.variables,
null,
2
),
color: e["x-scalar-environments"]?.[t.value?.activeEnvironmentId]?.color || "#FFFFFF",
isDefault: !1
}) : S.parse({
uid: "default",
color: "#FFFFFF",
name: "No Environment",
value: JSON.stringify(t.value.environments, null, 2)
});
}), v = r(() => {
const e = i.value[l.Request], n = a[i.value.collection] || a[t.value?.collections[0] ?? ""];
return u[e] || u[n?.requests[0] ?? ""] || Object.values(u)[0];
}), C = r(() => {
const e = i.value[l.Examples] === "default" ? v.value?.examples[0] || "" : i.value[l.Examples];
return b[e];
}), d = r(() => {
const e = a[i.value[l.Collection]];
if (e)
return e;
const n = v.value?.uid;
if (n)
return Object.values(a).find((p) => p.requests?.includes(n));
const E = t.value?.collections[0] ?? a[0]?.uid ?? "";
return a[E];
}), O = r(() => {
if (v.value?.selectedServerUid) {
const e = m[v.value.selectedServerUid];
if (e)
return e;
}
if (d.value?.selectedServerUid) {
const e = m[d.value.selectedServerUid];
if (e)
return e;
}
}), N = r(
() => i.value[l.Cookies] === "default" ? t.value?.cookies[0] ?? "default" : i.value[l.Cookies]
), h = r(() => {
const e = t.value?.environments ?? {}, n = f.value.uid ? JSON.parse(f.value.value) : {}, E = I(e).map(([o, F]) => ({
key: o,
value: F,
source: "global"
})), p = I(n).map(([o, F]) => ({
key: o,
value: F,
source: "collection"
})), c = /* @__PURE__ */ new Map();
return p.forEach((o) => {
c.set(o.key, o);
}), E.forEach((o) => {
c.has(o.key) || c.set(o.key, o);
}), Array.from(c.values());
});
return {
activeCollection: d,
activeCookieId: N,
activeExample: C,
activeRequest: v,
activeRouterParams: i,
activeEnvironment: f,
activeServer: O,
activeWorkspace: t,
activeWorkspaceCollections: s,
activeWorkspaceServers: g,
activeEnvVariables: h,
activeWorkspaceRequests: x
};
}, R = Symbol(), V = () => {
const a = q(R);
if (!a)
throw new Error("Active entities store not provided");
return a;
};
export {
R as ACTIVE_ENTITIES_SYMBOL,
T as createActiveEntitiesStore,
V as useActiveEntities
};