@scalar/api-client
Version:
the open source API testing client
223 lines (222 loc) • 7.72 kB
JavaScript
import { computed as w } from "vue";
import { NavigationFailureType as v } from "vue-router";
import { initializeWorkspaceEventHandlers as k } from "../../workspace-events.js";
function S({
eventBus: r,
store: f,
router: m,
rebuildSidebar: u,
navigateToCurrentTab: o,
onSelectSidebarItem: d,
onAfterExampleCreation: i,
onCopyTabUrl: l,
onToggleSidebar: x,
renameWorkspace: h
}) {
const s = w(() => m.currentRoute?.value), p = ({
documentName: e,
path: c,
method: t,
exampleName: a
}) => !(e !== void 0 && e !== s.value?.params.documentSlug || c !== void 0 && encodeURIComponent(c) !== s.value?.params.pathEncoded || t !== void 0 && t !== s.value?.params.method || a !== void 0 && a !== s.value?.params.exampleName);
k({
eventBus: r,
store: f,
hooks: {
//------------------------------------------------------------------------------------
// Document Related Hooks
//------------------------------------------------------------------------------------
"document:delete:document": {
onAfterExecute: async (e) => {
s?.value?.params.documentSlug === e.name && await m.push({
name: "workspace.environment"
});
}
},
//------------------------------------------------------------------------------------
// Operation Related Hooks
//------------------------------------------------------------------------------------
"operation:update:pathMethod": {
onBeforeExecute: (e) => ({
...e,
callback: async (c) => {
c === "success" && (await m.replace({
name: "example",
params: {
method: e.payload.method,
pathEncoded: encodeURIComponent(e.payload.path),
exampleName: s.value?.params.exampleName
}
}), u(f.value?.workspace.activeDocument?.["x-scalar-navigation"]?.name)), e.callback(c);
}
})
},
"operation:upsert:parameter": {
onAfterExecute: (e) => i(e.meta)
},
"operation:update:extra-parameters": {
onAfterExecute: (e) => i(e.meta)
},
"operation:reload:history": {
onAfterExecute: (e) => i({ ...e.meta, exampleKey: "draft" })
},
"operation:delete:operation": {
onAfterExecute: async (e) => {
u(e.documentName);
const {
documentName: c,
meta: { path: t, method: a }
} = e;
p({
documentName: c,
path: t,
method: a
}) && await m.replace({
name: "document.overview",
params: {
documentSlug: c
}
});
}
},
"operation:create:draft-example": {
onAfterExecute: async (e) => {
i({ ...e.meta, exampleKey: e.exampleName }), await m.push({
name: "example",
params: {
documentSlug: e.documentName,
pathEncoded: encodeURIComponent(e.meta.path),
method: e.meta.method,
exampleName: e.exampleName
}
});
}
},
"operation:delete:example": {
onAfterExecute: async (e) => {
u(e.documentName);
const {
documentName: c,
meta: { path: t, method: a, exampleKey: n }
} = e;
p({
documentName: c,
path: t,
method: a,
exampleName: n
}) && await m.replace({
name: "example",
params: {
pathEncoded: encodeURIComponent(t),
method: a,
documentSlug: c,
exampleName: "default"
}
});
}
},
//------------------------------------------------------------------------------------
// Operation Request Body Related Hooks
//------------------------------------------------------------------------------------
"operation:update:requestBody:value": {
onAfterExecute: (e) => i(e.meta)
},
"operation:update:requestBody:formValue": {
onAfterExecute: (e) => i(e.meta)
},
//------------------------------------------------------------------------------------
// Tag Related Event Hooks
//------------------------------------------------------------------------------------
"tag:create:tag": {
onAfterExecute: (e) => u(e.documentName)
},
"tag:edit:tag": {
onAfterExecute: async (e) => {
u(e.documentName), e.tag.children?.some(
(t) => t.type === "operation" && p({
documentName: e.documentName,
path: t.path,
method: t.method
})
) && await m.replace({ ...s.value });
}
},
"tag:delete:tag": {
onAfterExecute: (e) => u(e.documentName)
},
//------------------------------------------------------------------------------------
// Tabs Related Event Hooks
//------------------------------------------------------------------------------------
"tabs:add:tab": {
onAfterExecute: o
},
"tabs:close:tab": {
onAfterExecute: o
},
"tabs:focus:tab": {
onAfterExecute: o
},
"tabs:focus:tab-last": {
onAfterExecute: o
},
"tabs:navigate:previous": {
onAfterExecute: o
},
"tabs:navigate:next": {
onAfterExecute: o
},
"tabs:update:tabs": {
onAfterExecute: o
}
}
}), r.on("workspace:update:name", (e) => h(e)), r.on("scroll-to:nav-item", ({ id: e }) => d(e)), r.on("ui:toggle:sidebar", x), r.on("ui:navigate", async (e) => {
const { replace: c = !1 } = e, t = c ? m.replace : m.push, a = (n) => {
if (!n)
return e.callback?.("success");
const g = v.duplicated;
return n.type !== g ? e.callback?.("error") : e.callback?.("success");
};
if (e.page === "document") {
const n = {
documentSlug: e.documentSlug,
workspaceSlug: e.workspaceSlug,
namespace: e.namespace
};
if (e.path === "overview")
return a(await t({ name: "document.overview", params: n }));
if (e.path === "servers")
return a(await t({ name: "document.servers", params: n }));
if (e.path === "environment")
return a(await t({ name: "document.environment", params: n }));
if (e.path === "authentication")
return a(await t({ name: "document.authentication", params: n }));
if (e.path === "cookies")
return a(await t({ name: "document.cookies", params: n }));
if (e.path === "settings")
return a(await t({ name: "document.settings", params: n }));
}
if (e.page === "workspace") {
const n = { workspaceSlug: e.workspaceSlug, namespace: e.namespace };
if (e.path === "environment")
return a(await t({ name: "workspace.environment", params: n }));
if (e.path === "cookies")
return a(await t({ name: "workspace.cookies", params: n }));
if (e.path === "settings")
return a(await t({ name: "workspace.settings", params: n }));
}
if (e.page === "example") {
const n = {
namespace: e.namespace,
workspaceSlug: e.workspaceSlug,
documentSlug: e.documentSlug,
pathEncoded: encodeURIComponent(e.path),
method: e.method,
exampleName: e.exampleName
};
return a(await t({ name: "example", params: n }));
}
}), r.on("tabs:copy:url", (e) => l(e.index));
}
export {
S as initializeAppEventHandlers
};