UNPKG

@scalar/api-client

Version:

the open source API testing client

88 lines (87 loc) 2.62 kB
import { createWorkspaceStore as f } from "@scalar/workspace-store/client"; import { generateUniqueValue as d } from "@scalar/workspace-store/helpers/generate-unique-value"; import { createWorkspaceStorePersistence as W } from "@scalar/workspace-store/persistence"; import { persistencePlugin as g } from "@scalar/workspace-store/plugins/client"; import { ref as u, watch as h, toValue as v } from "vue"; import { useRouter as y } from "vue-router"; import { slugify as D } from "../helpers/slugify.js"; import { workspaceStorage as S } from "../helpers/storage.js"; const A = 1e3, s = { name: "Default Workspace", id: "default" }, E = { openapi: "3.1.0", info: { title: "Drafts", version: "1.0.0" }, paths: { "/": { get: {} } }, "x-scalar-original-document-hash": "drafts", "x-scalar-icon": "interface-edit-tool-pencil" }, I = async ({ workspaceId: c }) => f({ plugins: [await g({ workspaceId: c, debounceDelay: A })] }), F = ({ workspaceId: c }) => { const l = u(null), n = u([]), w = u(null), o = W(), k = y(), i = async (e) => { const t = await (await o).workspace.getItem(e); if (!t) return !1; const a = await I({ workspaceId: e }); return a.loadWorkspace(t.workspace), S.setActiveWorkspaceId(e), l.value = { id: e, name: t.name }, w.value = a, !0; }, m = async ({ id: e, name: r }) => { const t = f(); await t.addDocument({ name: "drafts", document: E }), await (await o).workspace.setItem(e, { name: r, workspace: t.exportWorkspace() }); }; h( () => v(c), async (e) => { const r = e, a = await (await o).workspace.getAll(); if (n.value = a, !!r && !await i(r)) { if (r === s.id) { if (a[0] && await i(a[0].id)) return; if (await m(s), await i(s.id)) { n.value.push(s); return; } console.error("[ERROR]: something went wrong when trying to create the default workspace"); return; } return p(s.id); } }, { immediate: !0 } ); const p = (e) => { k.push({ name: "workspace.environment", params: { workspaceSlug: e } }); }; return { store: w, activeWorkspace: l, workspaces: n, setWorkspaceId: p, createWorkspace: async ({ name: e }) => { const r = await o, t = await d({ defaultValue: e, validation: async (a) => !await r.workspace.has(a), maxRetries: 100, transformation: D }); t && (await m({ id: t, name: e }), p(t)); } }; }; export { F as useWorkspaceSelector };