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