Files
FastapiAdmin/frontend/web/src/utils/fetchAllPages.ts
T
zhangtao 80263e98e1 feat: 重构前端项目结构并优化代码
refactor: 迁移前端资源文件至web目录
feat: 新增多种图标资源
style: 统一代码风格和格式化配置
docs: 更新README和文档说明
chore: 更新依赖和配置文件
fix: 修复部分类型定义和枚举
perf: 优化路由和组件加载逻辑
2026-05-01 01:01:17 +08:00

31 lines
1.1 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.
/**
* 按分页拉取全量列表(用于 ExportModal 等「全量数据」场景)
*/
export async function fetchAllPages<T>(options: {
/** 每页条数,默认 1000 */
pageSize?: number;
/** 初始查询条件(会拷贝后写入 page_no / page_size) */
initialQuery: Record<string, unknown>;
/** 页码字段名,默认 page_no */
pageNoKey?: string;
/** 每页条数字段名,默认 page_size */
pageSizeKey?: string;
/** 拉取一页,返回 total 与 list */
fetchPage: (query: Record<string, unknown>) => Promise<{ total: number; list: T[] }>;
}): Promise<T[]> {
const pageSize = options.pageSize ?? 1000;
const pageNoKey = options.pageNoKey ?? 'page_no';
const pageSizeKey = options.pageSizeKey ?? 'page_size';
const query = { ...options.initialQuery };
query[pageNoKey] = 1;
query[pageSizeKey] = pageSize;
const all: T[] = [];
while (true) {
const { total, list } = await options.fetchPage(query);
all.push(...list);
if (all.length >= total || list.length === 0) break;
query[pageNoKey] = (query[pageNoKey] as number) + 1;
}
return all;
}