OAuth token expiry mid-batch in Node.js script

Running into a weird issue with our nightly data sync job. It starts fine, pulls the first few thousand records, then suddenly chokes with a 401 Unauthorized. The access token expires mid-batch and the script doesn’t catch it until it’s too late.

I’m using the @genesyscloud/genesyscloud-node-sdk to handle auth. Here’s the setup:

const client = new PlatformClient();
client.init({
 client_id: process.env.GENESYS_CLIENT_ID,
 client_secret: process.env.GENESYS_SECRET,
 base_url: process.env.GENESYS_BASE_URL
});

// Loop through queue IDs
for (const queueId of queueIds) {
 await client.Analytics().getQueuesStatsInterval({
 queueIds: [queueId],
 interval: '30 minutes'
 });
}

The SDK docs mention it handles token refresh automatically, but that seems to only work if the request fails. Once the token is expired, the first request in the loop fails, and subsequent requests in the same loop iteration (if I were batching them) or the next loop iteration fail immediately because the client isn’t refreshing in time.

I tried adding a manual refresh check before the loop, but that just gives me a fresh token that expires again halfway through. Is there a way to hook into the SDK’s refresh logic or should I be implementing a retry mechanism with exponential backoff for 401s specifically? The job runs for about 45 minutes, which is longer than the standard 1-hour token life, but the overhead of multiple requests seems to push it over.