-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpagination.js
More file actions
49 lines (39 loc) · 1.54 KB
/
pagination.js
File metadata and controls
49 lines (39 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const { SquareClient, SquareEnvironment, SquareError } = require("square");
require("dotenv").config();
// Create a new Square client
const client = new SquareClient({
token: process.env.SQUARE_ACCESS_TOKEN,
environment: SquareEnvironment.Sandbox, // Sandbox mode for testing
});
// Function to demonstrate pagination (using Customers API)
async function listAllCustomers() {
console.log("Fetching all customers using pagination...\n");
try {
let cursor = null; // This will hold the position of the next page
let page = 1; // To keep track of which page we're on
do {
// Make the API call with the cursor (if it's null, it fetches the first page)
const response = await client.customers.list({ cursor });
// Extract the customers and the cursor for the next page
const customers = response.customers || [];
cursor = response.cursor;
console.log(`📄 Page ${page}: Found ${customers.length} customers`);
customers.forEach((c) =>
console.log(`➡️ ${c.id}: ${c.givenName || "No Name"} ${c.familyName || ""}`)
);
page++;
console.log("----------------------------------");
} while (cursor); // Continue looping if there’s another page
console.log("\n✅ Finished listing all customers!");
} catch (error) {
if (error instanceof SquareError) {
error.errors.forEach((e) =>
console.error(e.category, e.code, e.detail)
);
} else {
console.error("Unexpected error:", error);
}
}
}
// Run the pagination function
listAllCustomers();