-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add retry logic to fetch for 3 retries
- Loading branch information
1 parent
7c7800f
commit 56d6acf
Showing
6 changed files
with
94 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@labdigital/apollo-trusted-documents": patch | ||
--- | ||
|
||
Add logic to retry fetching documents from remote store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
export const fetchWithRetry = async ( | ||
url: string, | ||
options: RequestInit = {}, | ||
retries = 3, | ||
initialDelay = 100, | ||
): Promise<Response> => { | ||
let delay = initialDelay; | ||
|
||
for (let attempt = 0; attempt <= retries; attempt++) { | ||
try { | ||
const response = await fetch(url, options); | ||
|
||
if (!response.ok) { | ||
console.error( | ||
`Failed to fetch data from ${url} status: ${response.status}`, | ||
); | ||
throw new Error(`HTTP error! status: ${response.status}`); | ||
} | ||
return response; | ||
} catch (err: unknown) { | ||
if (attempt >= retries) { | ||
throw err; | ||
} | ||
|
||
// Wait for the current delay before retrying | ||
await new Promise((resolve) => setTimeout(resolve, delay)); | ||
|
||
// Increase the delay (exponential backoff) | ||
delay = Math.min(delay * 2, 1000); // Cap at 1000ms | ||
} | ||
} | ||
|
||
throw new Error("Internal error"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters