@scalar/api-client
Version:
the open source API testing client
176 lines (175 loc) • 7.02 kB
JavaScript
import { defineComponent as z, ref as L, computed as k, watch as J, createBlock as S, openBlock as a, unref as l, withCtx as m, createElementBlock as d, Fragment as p, createElementVNode as C, createCommentVNode as X, createVNode as v, createTextVNode as r, toDisplayString as P } from "vue";
import { useLoadingState as Y, ScalarButton as R, ScalarCodeBlock as G, ScalarIcon as H, ScalarTooltip as K } from "@scalar/components";
import { normalize as Q } from "@scalar/openapi-parser";
import { generateUniqueValue as Z } from "@scalar/workspace-store/helpers/generate-unique-value";
import { useRouter as ee } from "vue-router";
import { getOpenApiDocumentDetails as te } from "../helpers/get-openapi-document-details.js";
import { getOpenApiFromPostman as oe } from "../helpers/get-openapi-from-postman.js";
import { getPostmanDocumentDetails as ae } from "../helpers/get-postman-document-details.js";
import { isPostmanCollection as V } from "../helpers/is-postman-collection.js";
import { isUrl as F } from "../../../helpers/is-url.js";
import { slugify as ne } from "../../../helpers/slugify.js";
import se from "./CommandActionForm.vue.js";
import le from "./CommandActionInput.vue.js";
import re from "./WatchModeToggle.vue.js";
import { useFileDialog as ie } from "../../../../hooks/useFileDialog.js";
const ce = { class: "flex justify-between" }, ue = { class: "flex w-full flex-row items-center justify-between gap-3" }, me = {
name: "CommandPaletteImport"
}, Ie = /* @__PURE__ */ z({
...me,
props: {
workspaceStore: {}
},
emits: ["close", "back", "open-command"],
setup(g, { emit: T }) {
const w = T, E = 100, b = "document", O = ee(), y = Y(), o = L(""), x = L(!0), i = k(() => F(o.value)), n = k(() => V(o.value) ? ae(o.value) : te(o.value)), U = k(
() => n.value ? n.value.type : "json"
), _ = k(() => !o.value.trim());
J(i, (e) => {
x.value = e;
});
const j = (e) => !Object.keys(g.workspaceStore.workspace.documents).includes(e), h = async (e) => await Z({
defaultValue: e,
validation: j,
maxRetries: E,
transformation: ne
}), q = (e) => typeof e.info == "object" && e.info !== null && "title" in e.info && typeof e.info.title == "string" ? e.info.title : b, A = async (e) => {
if (F(e)) {
const s = await h(b);
return s ? { success: await g.workspaceStore.addDocument({
name: s,
url: e,
meta: {
"x-scalar-watch-mode": x.value
}
}), name: s } : (console.error("Failed to generate a unique name"), { success: !1 });
}
if (V(e)) {
const s = oe(e), f = s.info?.title ?? b, D = await h(f);
return D ? { success: await g.workspaceStore.addDocument({
name: D,
document: s
}), name: D } : (console.error("Failed to generate a unique name"), { success: !1 });
}
const t = Q(e), c = q(t), u = await h(c);
return u ? { success: await g.workspaceStore.addDocument({
name: u,
document: t
}), name: u } : (console.error("Failed to generate a unique name"), { success: !1 });
}, I = (e) => {
O.push({
name: "document.overview",
params: { documentSlug: e }
});
}, { open: B } = ie({
onChange: (e) => {
const [t] = e ?? [];
if (!t)
return;
const c = async (N) => {
const s = N.target?.result, f = await A(s);
f.success && I(f.name), w("close");
}, u = new FileReader();
u.onload = c, u.readAsText(t);
},
multiple: !1,
accept: ".json,.yaml,.yml"
}), M = async () => {
y.start();
const e = await A(o.value);
e.success ? (await y.clear(), I(e.name)) : await y.invalidate(), w("close");
}, W = (e) => {
if (e.trim().toLowerCase().startsWith("curl"))
return w("open-command", "import-curl-command", { curl: e });
o.value = e;
}, $ = (e) => {
w("back", e);
};
return (e, t) => (a(), S(se, {
disabled: _.value,
loader: l(y),
onSubmit: M
}, {
options: m(() => [
C("div", ue, [
v(l(R), {
class: "hover:bg-b-2 relative max-h-8 gap-1.5 p-2 text-xs",
variant: "outlined",
onClick: l(B)
}, {
default: m(() => [
t[4] || (t[4] = r(" JSON, or YAML File ", -1)),
v(l(H), {
class: "text-c-3",
icon: "Upload",
size: "md"
})
]),
_: 1
}, 8, ["onClick"]),
v(l(K), {
content: i.value ? "Watch mode automatically updates the API client when the OpenAPI URL content changes, ensuring your client remains up-to-date." : "Watch mode is only available for URL imports. When enabled it automatically updates the API client when the OpenAPI URL content changes.",
placement: "bottom"
}, {
default: m(() => [
v(re, {
modelValue: x.value,
"onUpdate:modelValue": t[1] || (t[1] = (c) => x.value = c),
disabled: !i.value
}, null, 8, ["modelValue", "disabled"])
]),
_: 1
}, 8, ["content"])
])
]),
submit: m(() => [
t[5] || (t[5] = r(" Import ", -1)),
i.value ? (a(), d(p, { key: 0 }, [
r("from URL")
], 64)) : n.value && U.value ? (a(), d(p, { key: 1 }, [
n.value.title ? (a(), d(p, { key: 0 }, [
r(' "' + P(n.value.title) + '" ', 1)
], 64)) : (a(), d(p, { key: 1 }, [
r(P(n.value.version), 1)
], 64))
], 64)) : (a(), d(p, { key: 2 }, [
r("Collection")
], 64))
]),
default: m(() => [
!n.value || i.value ? (a(), S(le, {
key: 0,
modelValue: o.value,
placeholder: "OpenAPI/Swagger/Postman URL or cURL",
onDelete: $,
"onUpdate:modelValue": W
}, null, 8, ["modelValue"])) : (a(), d(p, { key: 1 }, [
C("div", ce, [
t[3] || (t[3] = C("div", { class: "text-c-2 min-h-8 w-full py-2 pl-12 text-center text-xs" }, " Preview ", -1)),
v(l(R), {
class: "hover:bg-b-2 relative ml-auto max-h-8 gap-1.5 p-2 text-xs",
variant: "ghost",
onClick: t[0] || (t[0] = (c) => o.value = "")
}, {
default: m(() => [...t[2] || (t[2] = [
r(" Clear ", -1)
])]),
_: 1
})
]),
n.value && !i.value ? (a(), S(l(G), {
key: 0,
class: "bg-b-2 mt-1 max-h-[40dvh] rounded border px-2 py-1 text-sm",
content: o.value,
copy: !1,
lang: U.value
}, null, 8, ["content", "lang"])) : X("", !0)
], 64))
]),
_: 1
}, 8, ["disabled", "loader"]));
}
});
export {
Ie as default
};