UNPKG

@scalar/api-client

Version:

the open source API testing client

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