@scalar/api-client
Version:
the open source API testing client
228 lines (227 loc) • 8.22 kB
JavaScript
import { defineComponent as K, computed as a, ref as c, onMounted as O, onBeforeUnmount as g, watch as V, createElementBlock as w, openBlock as T, createElementVNode as I, createVNode as l, withCtx as x, unref as B } from "vue";
import { AVAILABLE_CLIENTS as M } from "@scalar/types/snippetz";
import { useToasts as A } from "@scalar/use-toasts";
import L from "../../../components/ViewLayout/ViewLayout.vue.js";
import N from "../../../components/ViewLayout/ViewLayoutContent.vue.js";
import { ERRORS as P } from "../../../libs/errors.js";
import { buildRequest as H } from "./helpers/build-request.js";
import { getSecuritySchemes as F } from "./helpers/build-request-security.js";
import { harToFetchRequest as $ } from "./helpers/har-to-fetch-request.js";
import { harToFetchResponse as j } from "./helpers/har-to-fetch-response.js";
import { sendRequest as D } from "./helpers/send-request.js";
import Q from "./components/Header.vue.js";
import { generateClientOptions as z } from "../operation-code-sample/helpers/generate-client-options.js";
import { getSecurityRequirements as G } from "../../features/operation/helpers/get-security-requirements.js";
import { getSelectedSecurity as J } from "../../features/operation/helpers/get-selected-security.js";
import W from "../request-block/RequestBlock.vue.js";
import X from "../response-block/ResponseBlock.vue.js";
const Y = { class: "bg-b-1 flex h-full flex-col" }, Z = { class: "lg:min-h-header flex w-full flex-wrap items-center justify-center p-2 lg:p-0" }, p = {
name: "OperationBlock"
}, fe = /* @__PURE__ */ K({
...p,
props: {
eventBus: {},
documentSecurity: {},
documentSelectedSecurity: {},
appVersion: {},
globalCookies: { default: () => [] },
path: {},
method: {},
httpClients: { default: () => M },
history: { default: () => [] },
layout: {},
server: {},
selectedClient: {},
servers: {},
hideClientButton: { type: Boolean },
integration: {},
documentUrl: {},
source: {},
operation: {},
operationSelectedSecurity: {},
setOperationSecurity: { type: Boolean },
exampleKey: {},
authMeta: {},
securitySchemes: {},
plugins: { default: () => [] },
environments: {},
activeEnvironment: {},
environment: {},
proxyUrl: {}
},
emits: ["update:servers"],
setup(e, { emit: q }) {
const R = q, C = a(() => z(e.httpClients)), d = a(
() => G(e.documentSecurity, e.operation.security)
), h = a(
() => J(
e.documentSelectedSecurity,
e.operationSelectedSecurity,
d.value,
e.setOperationSecurity
)
), v = a(
() => F(e.securitySchemes, h.value.selectedSchemes)
), { toast: y } = A(), f = c(null), i = c(null), u = c(null), s = () => f.value?.abort(P.REQUEST_ABORTED), m = async () => {
const [t, n] = H({
environment: e.environment,
exampleKey: e.exampleKey,
globalCookies: e.globalCookies,
method: e.method,
operation: e.operation,
path: e.path,
selectedSecuritySchemes: v.value,
server: e.server,
proxyUrl: e.proxyUrl
});
if (t) {
y(t.message, "error");
return;
}
f.value = n.controller, i.value && "reader" in i.value && i.value.reader.cancel(), e.eventBus.emit("hooks:on:request:sent", {
meta: {
method: e.method,
path: e.path,
exampleKey: e.exampleKey
}
});
const [o, r] = await D({
isUsingProxy: n.isUsingProxy,
operation: e.operation,
plugins: e.plugins,
request: n.request
});
if (e.eventBus.emit("hooks:on:request:complete", {
payload: r ? {
response: r.originalResponse,
request: r.request.clone(),
duration: r.response.duration,
timestamp: r.timestamp
} : void 0,
meta: {
method: e.method,
path: e.path,
exampleKey: e.exampleKey
}
}), o) {
y(o.message, "error");
return;
}
i.value = r.response, u.value = r.request;
};
O(() => {
e.eventBus.on("operation:send:request:hotkey", m), e.eventBus.on("operation:cancel:request", s);
}), g(() => {
e.eventBus.off("operation:send:request:hotkey", m), e.eventBus.off("operation:cancel:request", s);
});
const S = a(
() => e.history.map((t) => ({
method: t.request.method,
path: t.request.url,
duration: t.time,
status: t.response.status
})).reverse()
), k = ({ index: t }) => {
const n = (e.history.length ?? 0) - t - 1, o = e.history[n];
if (!o)
return;
const r = () => e.eventBus.emit("ui:navigate", {
page: "example",
method: e.method,
path: e.path,
exampleName: "draft",
callback: (E) => {
if (E !== "success")
return;
const U = j({
harResponse: o.response,
url: o.request.url,
method: e.method,
path: e.path,
duration: o.time
}), b = $({
harRequest: o.request
});
i.value = U, u.value = b;
}
});
e.eventBus.emit("operation:reload:history", {
meta: {
path: e.path,
method: e.method
},
index: n,
callback: r
});
};
return V([() => e.path, () => e.method, () => e.exampleKey], () => {
i.value = null, u.value = null, s();
}), g(() => {
s();
}), (t, n) => (T(), w("div", Y, [
I("div", Z, [
l(Q, {
activeEnvironment: e.activeEnvironment,
documentUrl: e.documentUrl,
environment: e.environment,
environments: e.environments,
eventBus: e.eventBus,
hideClientButton: e.hideClientButton,
history: S.value,
integration: e.integration,
layout: e.layout,
method: e.method,
path: e.path,
server: e.server,
servers: e.servers,
source: e.source,
onExecute: m,
"onSelect:history:item": k,
"onUpdate:servers": n[0] || (n[0] = (o) => R("update:servers"))
}, null, 8, ["activeEnvironment", "documentUrl", "environment", "environments", "eventBus", "hideClientButton", "history", "integration", "layout", "method", "path", "server", "servers", "source"])
]),
l(L, { class: "border-t" }, {
default: x(() => [
l(N, { class: "flex-1" }, {
default: x(() => [
l(B(W), {
authMeta: e.authMeta,
clientOptions: C.value,
environment: e.environment,
eventBus: e.eventBus,
exampleKey: e.exampleKey,
globalCookies: e.globalCookies,
layout: e.layout,
method: e.method,
operation: e.operation,
path: e.path,
plugins: e.plugins,
proxyUrl: e.proxyUrl,
securityRequirements: d.value,
securitySchemes: e.securitySchemes,
selectedClient: e.selectedClient,
selectedSecurity: h.value,
selectedSecuritySchemes: v.value,
server: e.server
}, null, 8, ["authMeta", "clientOptions", "environment", "eventBus", "exampleKey", "globalCookies", "layout", "method", "operation", "path", "plugins", "proxyUrl", "securityRequirements", "securitySchemes", "selectedClient", "selectedSecurity", "selectedSecuritySchemes", "server"]),
l(B(X), {
appVersion: e.appVersion,
eventBus: e.eventBus,
layout: e.layout,
plugins: e.plugins,
request: u.value,
response: i.value,
totalPerformedRequests: S.value.length
}, null, 8, ["appVersion", "eventBus", "layout", "plugins", "request", "response", "totalPerformedRequests"])
]),
_: 1
})
]),
_: 1
})
]));
}
});
export {
fe as default
};