UNPKG

@scalar/api-client

Version:

the open source API testing client

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