Files
security-alert-center/frontend/src/composables/useSacVersion.ts
T
2026-06-10 17:38:54 +10:00

58 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { onMounted, ref } from "vue";
import { APP_NAME, APP_VERSION } from "../version";
export interface SacVersionInfo {
name: string;
version: string;
}
let cached: SacVersionInfo | null = null;
let inflight: Promise<SacVersionInfo> | null = null;
async function fetchSacVersion(): Promise<SacVersionInfo> {
if (cached) {
return cached;
}
if (inflight) {
return inflight;
}
inflight = (async () => {
const fallback: SacVersionInfo = { name: APP_NAME, version: APP_VERSION };
try {
const response = await fetch("/health");
if (!response.ok) {
return fallback;
}
const data = (await response.json()) as { name?: string; version?: string };
const info: SacVersionInfo = {
name: (data.name || APP_NAME).trim() || APP_NAME,
version: (data.version || APP_VERSION).trim() || APP_VERSION,
};
cached = info;
return info;
} catch {
return fallback;
} finally {
inflight = null;
}
})();
return inflight;
}
/** Версия SAC с бэкенда (/health); fallback — frontend/src/version.ts */
export function useSacVersion() {
const appName = ref(APP_NAME);
const appVersion = ref(APP_VERSION);
onMounted(() => {
void fetchSacVersion().then((info) => {
appName.value = info.name;
appVersion.value = info.version;
});
});
return { appName, appVersion };
}