This commit is contained in:
2026-03-28 09:16:54 +00:00
commit 692e45286e
18 changed files with 12509 additions and 0 deletions

29
ts/bookstack.helpers.ts Normal file
View File

@@ -0,0 +1,29 @@
import type { IBookStackListResponse } from './bookstack.interfaces.js';
/**
* Auto-paginate a BookStack list endpoint using offset/count.
* If opts includes a specific offset, returns just that single page (no auto-pagination).
*/
export async function autoPaginate<T>(
fetchPage: (offset: number, count: number) => Promise<IBookStackListResponse<T>>,
opts?: { offset?: number; count?: number },
): Promise<T[]> {
const count = opts?.count || 100;
// If caller requests a specific offset, return just that page
if (opts?.offset !== undefined) {
const result = await fetchPage(opts.offset, count);
return result.data;
}
// Otherwise auto-paginate through all pages
const all: T[] = [];
let offset = 0;
while (true) {
const result = await fetchPage(offset, count);
all.push(...result.data);
offset += count;
if (offset >= result.total) break;
}
return all;
}