UNPKG

@scalar/api-client

Version:

the open source API testing client

124 lines (123 loc) 5.17 kB
import { defineComponent as H, ref as m, computed as N, watch as Q, onMounted as F, onBeforeUnmount as G, createElementBlock as K, openBlock as f, normalizeClass as U, unref as r, createBlock as _, createCommentVNode as O, createElementVNode as V, isRef as X, createVNode as Y } from "vue"; import { isDefined as Z } from "@scalar/helpers/array/is-defined"; import { safeJSON as ee } from "@scalar/object-utils/parse"; import { useToasts as te } from "@scalar/use-toasts"; import { RouterView as oe } from "vue-router"; import re from "../../components/Sidebar/SidebarToggle.vue.js"; import { useAnalytics as se } from "../../hooks/useAnalytics.js"; import { useClientConfig as ae } from "../../hooks/useClientConfig.js"; import { useSidebar as ne } from "../../hooks/useSidebar.js"; import { validateParameters as le } from "../../libs/validate-parameters.js"; import { useActiveEntities as ue } from "../../store/active-entities.js"; import { useOpenApiWatcher as ie } from "./hooks/useOpenApiWatcher.js"; import ce from "./RequestSidebar.vue.js"; import { usePluginManager as me } from "../../plugins/hooks/usePluginManager.js"; import { createRequestOperation as fe } from "../../libs/send-request/create-request-operation.js"; import { ERRORS as de } from "../../libs/errors.js"; import { useWorkspace as pe } from "../../store/store.js"; import { useLayout as ve } from "../../hooks/useLayout.js"; const Re = { class: "flex h-full" }, Se = { class: "flex h-full flex-1 flex-col" }, De = /* @__PURE__ */ H({ __name: "RequestRoot", emits: ["newTab"], setup(qe) { const w = pe(), { toast: u } = te(), { layout: d } = ve(), B = ae(), { isSidebarOpen: s } = ne(), A = se(), { activeCollection: n, activeExample: i, activeEnvironment: p, activeRequest: l, activeWorkspace: v, activeServer: T } = ue(), { cookies: P, requestHistory: D, showSidebar: R, securitySchemes: M, modalState: z, events: o } = w, I = me(), J = m(), S = m(), q = N( () => le(i.value ?? null) ), y = m(null), x = N( () => (n.value?.useCollectionSecurity ? n.value?.selectedSecuritySchemeUids : l.value?.selectedSecuritySchemeUids) ?? [] ), h = async () => { if (!l.value || !i.value || !n.value) return; if (q.value.hasBlockingErrors) { u("Path parameters must have values.", "error"), o.requestStatus.emit("abort"); return; } const e = typeof p.value == "object" ? p.value.value : "{}", t = ee.parse(e); t.error && console.error("INVALID ENVIRONMENT!"); const a = t.error || typeof t.data != "object" ? {} : t.data ?? {}, j = v.value?.cookies.map(($) => P[$]).filter(Z) ?? [], L = n.value?.info?.title === "Drafts" ? void 0 : T.value, [g, k] = fe({ request: l.value, example: i.value, selectedSecuritySchemeUids: x.value, proxyUrl: v.value?.proxyUrl ?? "", environment: a, globalCookies: j, status: o.requestStatus, securitySchemes: M, server: L, pluginManager: I }); if (B.value?.onRequestSent?.(l.value.path ?? ""), g) { u(g.message, "error"); return; } S.value = k.controller; const [E, C] = await k.sendRequest(); y.value = C, E ? u(E.message, "error") : D.push(W(C)); }, c = async () => S.value?.abort(de.REQUEST_ABORTED); function b() { A?.capture("client-send-request"); } Q(z, ({ open: e }) => { e || c(); }), F(() => { o.executeRequest.on(h), o.executeRequest.on(b), o.cancelRequest.on(c); }), ie(), G(() => { o.executeRequest.off(h), o.executeRequest.off(b), o.cancelRequest.off(c); }); const W = (e) => { try { return structuredClone(e); } catch { const t = { ...e }; return e.response?.data && (e.response.data instanceof Blob || e.response.data instanceof ArrayBuffer ? t.response.data = e.response.data : t.response.data = JSON.parse(JSON.stringify(e.response.data))), t; } }; return (e, t) => (f(), K("div", { ref_key: "element", ref: J, class: U(["bg-b-1 relative z-0 flex h-full flex-1 flex-col overflow-hidden pt-0", { "!mr-0 !mb-0 !border-0": r(d) === "modal" }]) }, [ r(R) ? (f(), _(re, { key: 0, modelValue: r(s), "onUpdate:modelValue": t[0] || (t[0] = (a) => X(s) ? s.value = a : null), class: U(["absolute top-2 left-3 z-50", [ { hidden: r(s) }, { "xl:!flex": !r(s) }, { "!flex": r(d) === "modal" } ]]) }, null, 8, ["modelValue", "class"])) : O("", !0), V("div", Re, [ r(R) ? (f(), _(ce, { key: 0, onNewTab: t[1] || (t[1] = (a) => e.$emit("newTab", a)) })) : O("", !0), V("div", Se, [ Y(r(oe), { invalidParams: q.value.invalidParams, requestResult: y.value, selectedSecuritySchemeUids: x.value }, null, 8, ["invalidParams", "requestResult", "selectedSecuritySchemeUids"]) ]) ]) ], 2)); } }); export { De as default };