UNPKG

@scalar/api-client

Version:

the open source API testing client

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