PicoBot/web/src/api/config.ts
oudecheng 7de9a8a054 feat(web): 顶栏显示后端版本号
复用现有 /health 端点返回的 CARGO_PKG_VERSION,避免在 package.json 重复维护版本号;挂载时拉取一次,失败则不显示徽标。
2026-08-07 14:52:11 +08:00

52 lines
1.5 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 { API, apiFetch, getAuthToken } from './client';
import type { AppConfig } from '../components/Settings/types';
export interface RestartResponse {
success: boolean;
message?: string;
}
export async function getAppConfig(): Promise<[AppConfig | null, string | null]> {
const [data, err] = await apiFetch<AppConfig>(API.config);
return [data, err?.message ?? null];
}
export async function updateAppConfig(config: AppConfig): Promise<[true, null] | [false, string]> {
const [, err] = await apiFetch<{ success: boolean }>(API.config, {
method: 'PUT',
body: { config },
});
return err ? [false, err.message] : [true, null];
}
export async function restartGateway(): Promise<{ status: number; data: RestartResponse }> {
const token = getAuthToken();
const resp = await fetch(API.restart, {
method: 'POST',
headers: token ? { Authorization: `Bearer ${token}` } : undefined,
});
const data = await resp.json().catch(() => ({ success: false }));
return { status: resp.status, data };
}
export async function checkHealth(): Promise<boolean> {
try {
const resp = await fetch(API.health);
return resp.ok;
} catch {
return false;
}
}
/** 从 /health 端点获取后端版本号(源自 Cargo.toml 的 CARGO_PKG_VERSION。 */
export async function getVersion(): Promise<string | null> {
try {
const resp = await fetch(API.health);
if (!resp.ok) return null;
const data = (await resp.json()) as { status?: string; version?: string };
return data.version ?? null;
} catch {
return null;
}
}