feat: Retry msgraph API calls

Signed-off-by: Gustaf Räntilä <g.rantila@gmail.com>
This commit is contained in:
Gustaf Räntilä
2024-04-05 08:24:32 +02:00
parent dc7ce9894c
commit 916411ca25
@@ -216,6 +216,7 @@ export class MicrosoftGraphClient {
async requestRaw(
url: string,
headers?: Record<string, string>,
retryCount = 2,
): Promise<Response> {
// Make sure that we always have a valid access token (might be cached)
const urlObj = new URL(url);
@@ -227,12 +228,19 @@ export class MicrosoftGraphClient {
throw new Error('Failed to obtain token from Azure Identity');
}
return await fetch(url, {
headers: {
...headers,
Authorization: `Bearer ${token.token}`,
},
});
try {
return await fetch(url, {
headers: {
...headers,
Authorization: `Bearer ${token.token}`,
},
});
} catch (e: any) {
if (e?.code === 'ETIMEDOUT' && retryCount > 0) {
return this.requestRaw(url, headers, retryCount - 1);
}
throw e;
}
}
/**