Handle paginated results in the Smartbills JavaScript SDK with SBListResponse: page and limit parameters, sorting, filters and iterating through all pages.
All list endpoints in the Smartbills API return paginated results. The SDK provides a consistent SBListResponse<T> wrapper that includes both the data and pagination metadata.
interface SBListResponse<T> { data: T[]; // Array of items on the current page pagination: SBPagination; // Pagination metadata}interface SBPagination { count: number; // Total number of items across all pages limit: number; // Number of items per page currentPage: number; // Current page number (1-based) pageCount: number; // Total number of pages filters?: Record<string, unknown>; // Applied filters sorts?: Record<string, string>; // Applied sort criteria}
For memory-efficient processing of large datasets:
async function* paginateExpenses( params?: Partial<ExpenseListRequest>) { let page = 1; let totalPages = 1; do { const response = await client.expenses.listBusiness({ ...params, page, limit: params?.limit ?? 100, }); for (const expense of response.data) { yield expense; } totalPages = response.pagination.pageCount; page++; } while (page <= totalPages);}// Process expenses one at a time without loading all into memoryfor await (const expense of paginateExpenses({ sortBy: 'amount' })) { console.log(expense.id, expense.amount);}
Filters and pagination work together. The pagination metadata reflects the filtered result set:
const { data, pagination } = await client.expenses.listBusiness({ page: 1, limit: 25, // Additional filters depend on the endpoint});// pagination.count reflects the total matching the filter, not the overall totalconsole.log(`Matched ${pagination.count} expenses`);