task-123(记录与版本中心): 实现历史记录分页加载
新增 history-model.ts(历史行 snake 解析) 与 history-api.ts(GET /api/admin/history)。 TDD: task-123.test.ts 8 用例 RED→GREEN。
This commit is contained in:
@@ -0,0 +1,12 @@
|
||||
/** 历史记录列表加载适配(任务 123):GET /api/admin/history + 归一与解析。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseHistoryPage } from './history-model.ts'
|
||||
import { normalizeHistoryParams, toHistoryListQuery, type HistoryListParams, type HistoryPageResult } from './history-dto.ts'
|
||||
|
||||
export const HISTORY_ENDPOINT = '/api/admin/history'
|
||||
|
||||
export async function fetchHistoryList(params: Partial<HistoryListParams> = {}): Promise<HistoryPageResult> {
|
||||
const normalized = normalizeHistoryParams(params)
|
||||
const { data } = await http.get<unknown>(HISTORY_ENDPOINT, { params: toHistoryListQuery(normalized) })
|
||||
return parseHistoryPage(data)
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/** 历史生成记录列表加载模型(任务 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<string, unknown>
|
||||
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<string, unknown>
|
||||
return item
|
||||
}
|
||||
|
||||
/** 归一化历史分页负载为前端结果;缺省字段回默认。 */
|
||||
export function parseHistoryPage(payload: unknown): HistoryPageResult {
|
||||
const out = emptyHistoryPage()
|
||||
const core = unwrap<unknown>(payload)
|
||||
if (!core || typeof core !== 'object') return out
|
||||
const record = core as Record<string, unknown>
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user