|
@@ -26,7 +26,14 @@ export function isApiEnvelope(x: unknown): x is ApiEnvelope {
|
|
|
/** 业务成功码:按你的后端约定改,例如 0 / 200 */
|
|
/** 业务成功码:按你的后端约定改,例如 0 / 200 */
|
|
|
export const SUCCESS_CODES = new Set([0, 200])
|
|
export const SUCCESS_CODES = new Set([0, 200])
|
|
|
|
|
|
|
|
-/** 兼容 list / records、total / totalCount 等常见分页字段 */
|
|
|
|
|
|
|
+function totalFromPageMeta(page: unknown, fallback: number): number {
|
|
|
|
|
+ if (page === null || typeof page !== 'object') return fallback
|
|
|
|
|
+ const p = page as Record<string, unknown>
|
|
|
|
|
+ const totalRaw = p.rowTotal ?? p.total ?? p.totalCount ?? p.totalElements
|
|
|
|
|
+ return typeof totalRaw === 'number' ? totalRaw : fallback
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** 兼容 list / records、total / totalCount、page.rowTotal 等常见分页字段 */
|
|
|
export function unwrapPageList<T>(res: unknown): { list: T[]; total: number } {
|
|
export function unwrapPageList<T>(res: unknown): { list: T[]; total: number } {
|
|
|
if (Array.isArray(res)) return { list: res as T[], total: res.length }
|
|
if (Array.isArray(res)) return { list: res as T[], total: res.length }
|
|
|
if (res !== null && typeof res === 'object') {
|
|
if (res !== null && typeof res === 'object') {
|
|
@@ -34,8 +41,9 @@ export function unwrapPageList<T>(res: unknown): { list: T[]; total: number } {
|
|
|
const rawList = o.list ?? o.records
|
|
const rawList = o.list ?? o.records
|
|
|
const list = Array.isArray(rawList) ? (rawList as T[]) : []
|
|
const list = Array.isArray(rawList) ? (rawList as T[]) : []
|
|
|
const totalRaw = o.total ?? o.totalCount ?? o.totalElements
|
|
const totalRaw = o.total ?? o.totalCount ?? o.totalElements
|
|
|
- const total = typeof totalRaw === 'number' ? totalRaw : list.length
|
|
|
|
|
- return { list, total }
|
|
|
|
|
|
|
+ if (typeof totalRaw === 'number') return { list, total: totalRaw }
|
|
|
|
|
+ if ('page' in o) return { list, total: totalFromPageMeta(o.page, list.length) }
|
|
|
|
|
+ return { list, total: list.length }
|
|
|
}
|
|
}
|
|
|
return { list: [], total: 0 }
|
|
return { list: [], total: 0 }
|
|
|
}
|
|
}
|