Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 73 lines 2.15 KB
Raw
1 /**
2 * Pagination parameters that getAllPages will pass to the fetch function.
3 */
4 type PaginationParams = {
5 cursor?: string;
6 limit: number;
7 };
8
9 /**
10 * Extracts the item type from a paginated response.
11 */
12 type ExtractItemType<T> = T extends { items: Array<infer R> } ? R : never;
13
14 /**
15 * Generic, type-safe utility for loading all pages from paginated API responses.
16 *
17 * This utility automatically handles pagination by:
18 * - Invoking the provided method with `limit: 100`
19 * - Traversing all pages using the `next_cursor` field
20 * - Collecting and returning all items from all pages
21 *
22 * Types are automatically inferred from the function signature, so you don't need to specify them manually.
23 *
24 * @param fetchFn - A function that accepts pagination parameters and returns a Promise of a paginated response.
25 * Base parameters should be captured in the function closure.
26 * @returns A Promise that resolves to an array of all items from all pages
27 *
28 * @example
29 * ```typescript
30 * import { getAllPages } from './utils/pagination';
31 *
32 * // Fetch all tools - types are automatically inferred!
33 * const allTools = await getAllPages((params) =>
34 * client.tools.list({
35 * ...params,
36 * tool_slugs: 'tool1,tool2',
37 * })
38 * );
39 * // allTools is inferred as Array<ToolListResponse.Item>
40 * ```
41 */
42 export async function getAllPages<
43 TFn extends (
44 params: PaginationParams
45 ) => Promise<{ items: Array<unknown>; next_cursor?: string | null }>,
46 >(fetchFn: TFn): Promise<Array<ExtractItemType<Awaited<ReturnType<TFn>>>>> {
47 type ResponseType = Awaited<ReturnType<TFn>>;
48 type ItemType = ExtractItemType<ResponseType>;
49
50 const allItems: Array<ItemType> = [];
51 let cursor: string | null | undefined = undefined;
52
53 const MAX_LIMIT_ALLOWED_BY_API = 100;
54
55 while (true) {
56 const params: PaginationParams = {
57 ...(cursor !== undefined && { cursor }),
58 limit: MAX_LIMIT_ALLOWED_BY_API,
59 };
60
61 const response = await fetchFn(params);
62 allItems.push(...(response.items as Array<ItemType>));
63
64 // Stop if there's no next cursor
65 if (!response.next_cursor) {
66 break;
67 }
68
69 cursor = response.next_cursor;
70 }
71
72 return allItems;
73 }