Files
solidtime/resources/js/packages/composables/src/fetchAllPages.ts
Gregor Vostrak df71051feb Add @solidtime/composables package for logic shared with desktop and the extension (#1430)
* add @solidtime/composables package shared by web, desktop and extension

* bump api to 0.0.8 and require it in composables

* drop the manual composables publish workflow
2026-12-01 17:22:44 +01:00

23 lines
698 B
TypeScript

/**
* Fetches all pages from a paginated Laravel API endpoint.
* Uses `meta.last_page` to determine the total number of pages,
* so only a single request is made when all data fits on one page.
*/
export async function fetchAllPages<T>(
fetchPage: (page: number) => Promise<{
data: T[];
meta: { per_page: number; last_page: number };
}>
): Promise<T[]> {
const firstResponse = await fetchPage(1);
const allItems: T[] = [...firstResponse.data];
const { last_page } = firstResponse.meta;
for (let page = 2; page <= last_page; page++) {
const response = await fetchPage(page);
allItems.push(...response.data);
}
return allItems;
}