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 memory
for await (const expense of paginateExpenses({ sortBy: 'amount' })) {
console.log(expense.id, expense.amount);
}