For large datasets, use fetchApiPagesGenerator to process pages one at a time:
import { fetchApiPagesGenerator } from 'rozod';import { getGroupsGroupidWallPosts } from 'rozod/lib/endpoints/groupsv2';// Process pages as they arriveconst pages = fetchApiPagesGenerator( getGroupsGroupidWallPosts, { groupId: 11479637 });for await (const page of pages) { console.log(`Processing page with ${page.data.length} posts`); // Process this page's data page.data.forEach(post => { console.log(`Post by ${post.poster.username}: ${post.body}`); });}
The generator approach is memory-efficient for large datasets since it only keeps one page in memory at a time.
import { fetchApiPages, isAnyErrorResponse } from 'rozod';import { getGroupsGroupidWallPosts } from 'rozod/lib/endpoints/groupsv2';async function getAllGroupPosts(groupId: number) { const posts = await fetchApiPages( getGroupsGroupidWallPosts, { groupId } ); if (isAnyErrorResponse(posts)) { console.error('Failed to fetch posts:', posts.message); return []; } // Flatten all pages into a single array return posts.flatMap(page => page.data);}const allPosts = await getAllGroupPosts(11479637);console.log(`Total posts: ${allPosts.length}`);
import { fetchApiPagesGenerator } from 'rozod';import { getGroupsGroupidWallPosts } from 'rozod/lib/endpoints/groupsv2';async function processGroupPosts(groupId: number) { const pages = fetchApiPagesGenerator( getGroupsGroupidWallPosts, { groupId } ); let totalProcessed = 0; for await (const page of pages) { // Process each post in this page for (const post of page.data) { await processPost(post); totalProcessed++; } console.log(`Processed ${totalProcessed} posts so far...`); } return totalProcessed;}async function processPost(post: any) { // Your processing logic here console.log(`Processing post by ${post.poster.username}`);}await processGroupPosts(11479637);
import { fetchApiPagesGenerator, isAnyErrorResponse} from 'rozod';import { getGroupsGroupidWallPosts } from 'rozod/lib/endpoints/groupsv2';async function safelyFetchAllPosts(groupId: number) { const pages = fetchApiPagesGenerator( getGroupsGroupidWallPosts, { groupId } ); const allPosts = []; for await (const page of pages) { // Check for errors on each page if (isAnyErrorResponse(page)) { console.error('Error fetching page:', page.message); break; // Stop on error } allPosts.push(...page.data); } return allPosts;}const posts = await safelyFetchAllPosts(11479637);
With the generator, you can stop fetching at any time:
import { fetchApiPagesGenerator } from 'rozod';import { getGroupsGroupidWallPosts } from 'rozod/lib/endpoints/groupsv2';const pages = fetchApiPagesGenerator( getGroupsGroupidWallPosts, { groupId: 11479637 });let foundTarget = false;for await (const page of pages) { for (const post of page.data) { if (post.body.includes('important keyword')) { console.log('Found the post we were looking for!'); foundTarget = true; break; } } if (foundTarget) break; // Stop fetching more pages}
Always handle potential errors when using pagination. Network issues or API changes can cause failures mid-pagination.