@@ -87,6 +87,58 @@ function determineHasMorePages<T>(
8787 return pageItemsCount >= DEFAULT_PAGE_LIMIT ;
8888}
8989
90+ /**
91+ * Fetches all pages in parallel (after fetching page 1 to determine total)
92+ * Much faster than sequential pagination - eliminates artificial delays and waits
93+ * @param fetchPage - Function that fetches a single page of data
94+ * @param extractData - Function that extracts the data array from the response
95+ * @returns Array of all items across all pages
96+ */
97+ export async function fetchAllPagesParallel < TResponse , TItem > (
98+ fetchPage : ( page : number ) => Promise < TResponse > ,
99+ extractData : ( response : TResponse ) => TItem [ ]
100+ ) : Promise < TItem [ ] > {
101+ // Step 1: Fetch first page to get metadata
102+ const firstPageResponse = await fetchPage ( 1 ) ;
103+ const firstPageItems = extractData ( firstPageResponse ) ;
104+
105+ // Step 2: Determine how many total pages exist
106+ const resp = firstPageResponse as PaginationResponse < unknown > ;
107+ let totalPages = 1 ;
108+
109+ if ( resp . pagination ) {
110+ totalPages = resp . pagination . totalPages ?? resp . pagination . pages ?? 1 ;
111+ } else if ( resp . total !== undefined && resp . limit !== undefined ) {
112+ totalPages = Math . ceil ( resp . total / resp . limit ) ;
113+ } else if ( firstPageItems . length >= DEFAULT_PAGE_LIMIT ) {
114+ // Got a full page, assume there might be more
115+ // But we can't know for sure, so we'll just return what we got
116+ return firstPageItems ;
117+ }
118+
119+ // Step 3: If only one page, return immediately
120+ if ( totalPages <= 1 ) {
121+ return firstPageItems ;
122+ }
123+
124+ // Step 4: Create promises for ALL remaining pages (in parallel!)
125+ const remainingPagePromises : Promise < TItem [ ] > [ ] = [ ] ;
126+ for ( let page = 2 ; page <= Math . min ( totalPages , MAX_PAGES ) ; page ++ ) {
127+ remainingPagePromises . push (
128+ fetchPage ( page ) . then ( response => extractData ( response ) )
129+ ) ;
130+ }
131+
132+ // Step 5: Execute ALL requests in parallel
133+ const remainingPages = await Promise . all ( remainingPagePromises ) ;
134+
135+ // Step 6: Flatten and combine with first page
136+ return [
137+ ...firstPageItems ,
138+ ...remainingPages . flat ( )
139+ ] ;
140+ }
141+
90142/**
91143 * Creates URL search params with pagination
92144 */
0 commit comments