Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✅ Add fetch retry logic to helper module #12

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,36 @@ const getColorFallback = (color: NoteColor) => {
return Object.values(NoteColor).includes(color) ? color : NoteColor.Yellow;
};

async function fetchWithRetry(
url: string,
options: RequestInit = {},
retries = 3,
delay = 1000,
): Promise<Response> {
for (let attempt = 1; attempt <= retries; attempt++) {
try {
const res = await fetch(url, options);
if (!res.ok) {
throw new Error(`HTTP error! status: ${res.status}`);
}
return res;
} catch (error: unknown) {
console.error(`Attempt ${attempt} failed. ${(error as Error).message}`);
if (attempt < retries) {
await new Promise((resolve) => setTimeout(resolve, delay));
} else {
throw error;
}
}
}
throw new Error('This should never be reached');
}

export {
isTextValid,
containsUrl,
filterText,
getFormattedDate,
getColorFallback,
fetchWithRetry,
};