feat(client): add rich domain classes, helpers, and refactor GitLabClient internals
This commit is contained in:
26
ts/gitlab.helpers.ts
Normal file
26
ts/gitlab.helpers.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* Auto-paginate a list endpoint.
|
||||
* If opts includes a specific page, returns just that page (no auto-pagination).
|
||||
*/
|
||||
export async function autoPaginate<T>(
|
||||
fetchPage: (page: number, perPage: number) => Promise<T[]>,
|
||||
opts?: { page?: number; perPage?: number },
|
||||
): Promise<T[]> {
|
||||
const perPage = opts?.perPage || 50;
|
||||
|
||||
// If caller requests a specific page, return just that page
|
||||
if (opts?.page) {
|
||||
return fetchPage(opts.page, perPage);
|
||||
}
|
||||
|
||||
// Otherwise auto-paginate through all pages
|
||||
const all: T[] = [];
|
||||
let page = 1;
|
||||
while (true) {
|
||||
const items = await fetchPage(page, perPage);
|
||||
all.push(...items);
|
||||
if (items.length < perPage) break;
|
||||
page++;
|
||||
}
|
||||
return all;
|
||||
}
|
||||
Reference in New Issue
Block a user