/** 历史生成记录列表加载模型(任务 123):解析 ImageHistoryListVo(snake) 记录行;纯逻辑。 */ import { unwrap } from '../../api/envelope.ts' import { emptyHistoryPage, type HistoryPageResult, type HistoryRecordItem } from './history-dto.ts' function text(value: unknown): string { return typeof value === 'string' ? value.trim() : '' } function numberOrNull(value: unknown): number | null { return typeof value === 'number' && Number.isFinite(value) ? Math.floor(value) : null } function strings(value: unknown): string[] { return Array.isArray(value) ? value.map((url) => text(url)).filter(Boolean) : [] } /** 解析单条历史记录;缺 id 视为无效。 */ export function toHistoryRecordItem(raw: unknown): HistoryRecordItem | null { if (!raw || typeof raw !== 'object') return null const r = raw as Record const id = numberOrNull(r.id) if (id === null) return null const item: HistoryRecordItem = { id, userId: numberOrNull(r.user_id ?? r.userId), username: text(r.username), createdAt: text(r.created_at ?? r.createdAt), panelType: text(r.panel_type ?? r.panelType), originalUrls: strings(r.original_urls ?? r.originalUrls), resultUrls: strings(r.result_urls ?? r.resultUrls), longImageUrl: text(r.long_image_url ?? r.longImageUrl), } if (r.params && typeof r.params === 'object') item.params = r.params as Record return item } /** 归一化历史分页负载为前端结果;缺省字段回默认。 */ export function parseHistoryPage(payload: unknown): HistoryPageResult { const out = emptyHistoryPage() const core = unwrap(payload) if (!core || typeof core !== 'object') return out const record = core as Record if (Array.isArray(record.items)) { out.items = record.items .map((raw) => toHistoryRecordItem(raw)) .filter((item): item is HistoryRecordItem => item !== null) } if (typeof record.total === 'number') out.total = Math.floor(record.total) if (typeof record.page === 'number' && record.page >= 1) out.page = Math.floor(record.page) const rawSize = record.page_size ?? record.pageSize if (typeof rawSize === 'number' && rawSize >= 1) out.pageSize = Math.floor(rawSize) return out }