Retry Beds24 data calls on 401 by invalidating the cached access token.
When a data endpoint rejects the token with 401, the cached token is now cleared and one retry is attempted with a fresh token. This prevents a stale token from causing a cascade of 401s while still failing cleanly if refresh itself is rate-limited. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
parent
48b44f6ccd
commit
57832d5e86
+9
-1
@@ -119,7 +119,8 @@ async function beds24Get<T>(
|
||||
houseId: string,
|
||||
path: string,
|
||||
searchParams: Record<string, string | number | boolean | undefined>,
|
||||
revalidateSeconds = 300
|
||||
revalidateSeconds = 300,
|
||||
retryOnAuthFailure = true
|
||||
): Promise<T> {
|
||||
const token = await getAccessToken(houseId);
|
||||
|
||||
@@ -137,6 +138,13 @@ async function beds24Get<T>(
|
||||
next: { revalidate: revalidateSeconds },
|
||||
});
|
||||
|
||||
if (response.status === 401 && retryOnAuthFailure) {
|
||||
// Access token may be stale/revoked. Clear it and retry once.
|
||||
accessTokenCache.delete(houseId);
|
||||
pendingTokenRequests.delete(houseId);
|
||||
return beds24Get(houseId, path, searchParams, revalidateSeconds, false);
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Beds24 API error: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user