Files
ln-bi/src/modules/assets/history/api.ts
T

21 lines
1.3 KiB
TypeScript

import { fetchAuthenticated } from '../../../auth/api-client';
import type { HistoryMode, HistoryTrend, HistoryPoint, HistoryDetails, HistoryCategory } from '../../../shared/assets/history';
async function get<T>(path: string, params: Record<string, string | number | null | undefined>, signal: AbortSignal): Promise<T> {
const query = new URLSearchParams();
for (const [key, value] of Object.entries(params)) if (value != null && value !== '') query.set(key, String(value));
const response = await fetchAuthenticated(`/api/asset-history/${path}?${query}`, { signal });
const body = await response.json();
if (!response.ok) throw new Error(body.error || '历史趋势加载失败,请重试');
return body as T;
}
export function fetchHistoryTrend(mode: HistoryMode, year: number, subject: string | null, signal: AbortSignal) {
return get<HistoryTrend>('trend', { mode, year, subject }, signal);
}
export function fetchHistoryLive(mode: HistoryMode, subject: string | null, signal: AbortSignal) {
return get<HistoryPoint>('live', { mode, subject }, signal);
}
export function fetchHistoryDetails(month: string, batchId: string | null, subject: string | null, category: HistoryCategory, search: string, page: number, signal: AbortSignal) {
return get<HistoryDetails>('details', { month, batchId, subject, category, search, page, pageSize: 30 }, signal);
}