UNPKG

@scalar/api-client

Version:

the open source API testing client

157 lines (156 loc) 7.82 kB
import { mergeObjects as x } from "@scalar/workspace-store/helpers/merge-object"; import { updateActiveProxy as g, updateColorMode as y, updateTheme as T, updateSelectedClient as w, updateDocumentIcon as R, toggleSecurity as O, updateWatchMode as N, createEmptyDocument as C, deleteDocument as E, upsertEnvironment as q, upsertEnvironmentVariable as P, upsertCookie as I, deleteCookie as k, deleteSecurityScheme as A, updateSelectedAuthTab as M, updateSecurityScheme as D, updateSelectedScopes as F, updateSelectedSecuritySchemes as U, addServer as V, updateServer as K, deleteServer as L, updateServerVariables as W, updateSelectedServer as j, createOperation as z, updateOperationPathMethod as G, updateOperationSummary as H, deleteOperation as J, deleteOperationExample as Q, addOperationParameter as S, updateOperationParameter as X, deleteOperationParameter as Y, deleteAllOperationParameters as Z, updateOperationRequestBodyContentType as _, updateOperationRequestBodyExample as $, addOperationRequestBodyFormRow as B, updateOperationRequestBodyFormRow as ee, deleteOperationRequestBodyFormRow as ae, createTag as te, deleteTag as oe, addTab as re, closeTab as ne, closeOtherTabs as le, focusTab as ie, focusLastTab as ue, navigatePreviousTab as ce, navigateNextTab as pe, updateTabs as me } from "@scalar/workspace-store/mutators"; import { toValue as f } from "vue"; import { useRouter as de, useRoute as se } from "vue-router"; const xe = ({ eventBus: a, document: t, workspaceStore: o, isSidebarOpen: d, commandPaletteState: s, sidebarState: v }) => { const c = de(), i = se(), p = (e, r) => { const n = f(o); return n ? r === "document" ? e.value : n.workspace : null; }, l = async () => { const e = f(o); if (!e) return; const r = e.workspace["x-scalar-active-tab"] ?? 0, n = e.workspace["x-scalar-tabs"]?.[r]; n && await c.replace(n.path); }, u = (e) => { e && o.value?.buildSidebar(e); }, m = (e) => { const r = t.value?.["x-scalar-navigation"]?.name; if (!r) return; const n = v.getEntryByLocation({ document: r, path: e.path, method: e.method, example: e.exampleKey }); (!n || n.type !== "example") && u(r); }, h = ({ documentName: e, path: r, method: n, exampleName: b }) => !(e !== void 0 && e !== i.params.documentSlug || r !== void 0 && encodeURIComponent(r) !== i.params.pathEncoded || n !== void 0 && n !== i.params.method || b !== void 0 && b !== i.params.exampleName); a.on("scroll-to:nav-item", ({ id: e }) => v.handleSelectItem(e)), a.on( "workspace:update:active-proxy", (e) => g(o.value?.workspace ?? null, e) ), a.on( "workspace:update:color-mode", (e) => y(o.value?.workspace ?? null, e) ), a.on("workspace:update:theme", (e) => T(o.value?.workspace ?? null, e)), a.on( "workspace:update:selected-client", (e) => w(o.value?.workspace, e) ), a.on("document:update:icon", (e) => R(t.value, e)), a.on("document:update:info", (e) => t.value && x(t.value.info, e)), a.on("document:toggle:security", () => O(t.value)), a.on("document:update:watch-mode", (e) => N(t.value, e)), a.on("document:create:empty-document", (e) => C(o.value, e)), a.on("document:delete:document", async (e) => { E(o.value, e), i.params.documentSlug === e.name && await c.push({ name: "workspace.environment" }); }), a.on("environment:upsert:environment", (e) => { o.value && q(t.value, o.value.workspace, e); }), a.on( "environment:delete:environment", ({ environmentName: e, collectionType: r }) => delete p(t, r)?.["x-scalar-environments"]?.[e] ), a.on("environment:upsert:environment-variable", (e) => { const r = p(t, e.collectionType); P(r, e); }), a.on( "environment:delete:environment-variable", ({ environmentName: e, index: r, collectionType: n }) => p(t, n)?.["x-scalar-environments"]?.[e]?.variables?.splice(r, 1) ), a.on("cookie:upsert:cookie", (e) => { const r = p(t, e.collectionType); I(r, e); }), a.on("cookie:delete:cookie", (e) => { const r = p(t, e.collectionType); k(r, e); }), a.on("auth:delete:security-scheme", (e) => A(t.value, e)), a.on("auth:update:active-index", (e) => M(t.value, e)), a.on("auth:update:security-scheme", (e) => D(t.value, e)), a.on("auth:update:selected-scopes", (e) => F(t.value, e)), a.on( "auth:update:selected-security-schemes", async (e) => await U(t.value, e) ), a.on("server:add:server", () => V(t.value)), a.on("server:update:server", (e) => K(t.value, e)), a.on("server:delete:server", (e) => L(t.value, e)), a.on("server:update:variables", (e) => W(t.value, e)), a.on("server:update:selected", (e) => j(t.value, e)), a.on("operation:create:operation", (e) => z(o.value, e)), a.on( "operation:update:pathMethod", (e) => G(t.value, o.value, e, async (r) => { r === "success" && (await c.replace({ name: "example", params: { method: e.payload.method, pathEncoded: encodeURIComponent(e.payload.path), exampleName: i.params.exampleName } }), u(t.value?.["x-scalar-navigation"]?.name)), e.callback(r); }) ), a.on("operation:update:summary", (e) => H(t.value, e)), a.on("operation:delete:operation", async (e) => { J(o.value, e), u(e.documentName), h({ documentName: e.documentName, path: e.meta.path, method: e.meta.method }) && await c.replace({ name: "document.overview", params: { documentSlug: e.documentName } }); }), a.on("operation:delete:example", async (e) => { Q(o.value, e), u(e.documentName), h({ documentName: e.documentName, path: e.meta.path, method: e.meta.method, exampleName: e.meta.exampleKey }) && await c.replace({ name: "example", params: { pathEncoded: encodeURIComponent(e.meta.path), method: e.meta.method, documentSlug: e.documentName, exampleName: "default" } }); }), a.on("operation:add:parameter", (e) => { S(t.value, e), m(e.meta); }), a.on("operation:update:parameter", (e) => { X(t.value, e), m(e.meta); }), a.on("operation:delete:parameter", (e) => Y(t.value, e)), a.on("operation:delete-all:parameters", (e) => Z(t.value, e)), a.on( "operation:update:requestBody:contentType", (e) => _(t.value, e) ), a.on("operation:update:requestBody:value", (e) => { $(t.value, e), m(e.meta); }), a.on("operation:add:requestBody:formRow", (e) => { B(t.value, e), m(e.meta); }), a.on( "operation:update:requestBody:formRow", (e) => ee(t.value, e) ), a.on( "operation:delete:requestBody:formRow", (e) => ae(t.value, e) ), a.on("tag:create:tag", (e) => { te(o.value, e), u(e.documentName); }), a.on("tag:delete:tag", (e) => { oe(o.value, e), u(e.documentName); }), a.on("ui:toggle:sidebar", () => d.value = !d.value), a.on( "ui:open:command-palette", (e) => { e ? s.open(e.action, e.payload) : s.open(); } ), a.on("tabs:add:tab", async (e) => { re(o.value?.workspace ?? null, e), await l(); }), a.on("tabs:close:tab", async (e) => { ne(o.value?.workspace ?? null, e), await l(); }), a.on("tabs:close:other-tabs", (e) => le(o.value?.workspace ?? null, e)), a.on("tabs:focus:tab", async (e) => { ie(o.value?.workspace ?? null, e), await l(); }), a.on("tabs:focus:tab-last", async (e) => { ue(o.value?.workspace ?? null, e), await l(); }), a.on("tabs:navigate:previous", async (e) => { ce(o.value?.workspace ?? null, e), await l(); }), a.on("tabs:navigate:next", async (e) => { pe(o.value?.workspace ?? null, e), await l(); }), a.on("tabs:update:tabs", async (e) => { me(o.value?.workspace ?? null, e), await l(); }); }; export { xe as useWorkspaceClientAppEvents };