Files
bunq/example.pagination.ts

128 lines
3.8 KiB
TypeScript

import { BunqAccount, IBunqPaginationOptions } from './ts/index.js';
// Example demonstrating the enhanced pagination support in getTransactions
async function demonstratePagination() {
const bunq = new BunqAccount({
apiKey: 'your-api-key',
deviceName: 'Pagination Demo',
environment: 'PRODUCTION',
});
// Initialize and get session
const sessionData = await bunq.init();
// Get accounts
const { accounts } = await bunq.getAccounts();
const account = accounts[0];
// Example 1: Get most recent transactions (default behavior)
const recentTransactions = await account.getTransactions();
console.log(`Got ${recentTransactions.length} recent transactions`);
// Example 2: Get transactions with custom count
const smallBatch = await account.getTransactions({ count: 10 });
console.log(`Got ${smallBatch.length} transactions with custom count`);
// Example 3: Get older transactions using older_id
if (recentTransactions.length > 0) {
const oldestTransaction = recentTransactions[recentTransactions.length - 1];
const olderTransactions = await account.getTransactions({
count: 50,
older_id: oldestTransaction.id
});
console.log(`Got ${olderTransactions.length} older transactions`);
}
// Example 4: Get newer transactions using newer_id
if (recentTransactions.length > 0) {
const newestTransaction = recentTransactions[0];
const newerTransactions = await account.getTransactions({
count: 20,
newer_id: newestTransaction.id
});
console.log(`Got ${newerTransactions.length} newer transactions`);
}
// Example 5: Backward compatibility - using number as newer_id
const backwardCompatible = await account.getTransactions(12345);
console.log(`Backward compatible call returned ${backwardCompatible.length} transactions`);
// Example 6: Paginating through all historical transactions
async function getAllTransactions(account: any): Promise<any[]> {
const allTransactions: any[] = [];
let lastTransactionId: number | false = false;
let hasMore = true;
while (hasMore) {
const options: IBunqPaginationOptions = {
count: 200,
older_id: lastTransactionId
};
const batch = await account.getTransactions(options);
if (batch.length === 0) {
hasMore = false;
} else {
allTransactions.push(...batch);
lastTransactionId = batch[batch.length - 1].id;
console.log(`Fetched ${batch.length} transactions, total: ${allTransactions.length}`);
}
}
return allTransactions;
}
// Example 7: Getting transactions between two dates
async function getTransactionsBetweenDates(
account: any,
startDate: Date,
endDate: Date
): Promise<any[]> {
const transactions: any[] = [];
let olderId: number | false = false;
let keepFetching = true;
while (keepFetching) {
const batch = await account.getTransactions({
count: 200,
older_id: olderId
});
if (batch.length === 0) {
keepFetching = false;
break;
}
for (const transaction of batch) {
const transactionDate = new Date(transaction.created);
if (transactionDate >= startDate && transactionDate <= endDate) {
transactions.push(transaction);
} else if (transactionDate < startDate) {
// We've gone past our date range
keepFetching = false;
break;
}
}
olderId = batch[batch.length - 1].id;
}
return transactions;
}
// Usage
const lastMonth = new Date();
lastMonth.setMonth(lastMonth.getMonth() - 1);
const transactionsLastMonth = await getTransactionsBetweenDates(
account,
lastMonth,
new Date()
);
console.log(`Found ${transactionsLastMonth.length} transactions in the last month`);
}
// Run the demo
demonstratePagination().catch(console.error);