@scalar/api-client
Version:
the open source API testing client
58 lines (57 loc) • 2.86 kB
JavaScript
import { useActiveEntities as A } from "../../../store/active-entities.js";
import { specDictionary as f } from "../../../store/import-spec.js";
import { combineRenameDiffs as y, mutateCollectionDiff as E, mutateSecuritySchemeDiff as D, mutateServerDiff as R, mutateTagDiff as g, mutateRequestDiff as S } from "../libs/watch-mode.js";
import { fetchDocument as T, createHash as M } from "@scalar/oas-utils/helpers";
import { parseSchema as l } from "@scalar/oas-utils/transforms";
import { useToasts as I } from "@scalar/use-toasts";
import { useTimeoutPoll as C } from "@vueuse/core";
import L from "microdiff";
import { watch as N } from "vue";
import { useWorkspace as U } from "../../../store/store.js";
const b = 5 * 1e3, P = 60 * 1e3, B = () => {
const { toast: h } = I(), c = A(), o = U(), { activeCollection: s, activeWorkspace: v } = c, { collectionMutators: n } = o, i = (e) => h(`[useOpenApiWatcher] Changes to the ${e} were not applied`, "error"), O = (e) => {
e.path[0] === "info" || e.path[0] === "security" ? E(e, c, o) || i("collection") : e.path[0] === "components" && e.path[1] === "securitySchemes" ? D(e, c, o) || i("securitySchemes") : e.path[0] === "servers" ? R(e, c, o) || i("servers") : e.path[0] === "tags" ? g(e, c, o) || i("tags") : e.path[0] === "paths" && (S(e, c, o) || i("requests"));
}, { pause: p, resume: m } = C(async () => {
const e = s.value?.documentUrl;
if (!e)
return;
const t = f[e];
try {
const a = await T(e, v.value?.proxyUrl, void 0, !1), u = M(a);
if (n.edit(s.value.uid, "watchModeStatus", "WATCHING"), t?.hash)
if (t.hash && t.hash !== u) {
const { schema: r } = await l(a), W = L(t.schema, r), d = y(W);
try {
d.forEach(O), f[e] = {
hash: u,
schema: r
};
} catch (w) {
console.error("[useOpenApiWatcher] Error:", w);
}
} else
console.log("[useOpenApiWatcher] No changes detected yet…");
else {
const { schema: r } = await l(a);
r && (f[e] = {
hash: u,
schema: r
});
}
} catch (a) {
console.error("[useOpenApiWatcher] Error:", a), console.info("[useOpenApiWatcher] Pausing watcher for 60 seconds"), p(), n.edit(s.value.uid, "watchModeStatus", "ERROR"), h("[useOpenApiWatcher] Unable to fetch the spec file, paused the watcher for 60 seconds", "error"), setTimeout(() => {
console.info("[useOpenApiWatcher] Resuming watcher"), m();
}, P);
}
}, b);
N(
[() => s.value?.documentUrl, () => s.value?.watchMode],
([e, t]) => {
e && t ? (console.info(`[useOpenApiWatcher] Watching ${e} …`), m()) : s.value && (p(), n.edit(s.value.uid, "watchModeStatus", "IDLE"));
},
{ immediate: !0 }
);
};
export {
B as useOpenApiWatcher
};