Skip to content

Conversation

@jeanpunt
Copy link
Contributor

Summary

This PR consolidates the duplicate fetchWithRetries utility function into a single shared location, following the DRY (Don't Repeat Yourself) principle.

Problem

The codebase had two identical copies of fetchWithRetries.ts:

  • src/app/employer/documents/fetchWithRetries.ts
  • src/app/employee/documents/fetchWithRetries.ts

Both files contained the exact same code (57 lines each), which:

  • Violates the DRY principle
  • Makes maintenance harder (changes need to be made in multiple places)
  • Increases risk of divergence over time

Solution

Created a new shared utility at src/lib/fetchWithRetries.ts with:

Enhanced Features:

  • Configurable retry options via FetchWithRetriesOptions interface
  • Exponential backoff support for retry delays
  • Custom retry condition callback for flexible error handling
  • Comprehensive TypeScript types and JSDoc documentation
  • Backward-compatible API

New Configuration Options:

interface FetchWithRetriesOptions {
  maxRetries?: number;           // Default: 5
  baseDelayMs?: number;          // Default: 1000
  useExponentialBackoff?: boolean; // Default: false
  shouldRetry?: (error: Error, attempt: number) => boolean;
}

Changes

  • Added: src/lib/fetchWithRetries.ts - New shared utility with enhanced features
  • Modified: src/app/employer/documents/page.tsx - Updated import path
  • Modified: src/app/employee/documents/page.tsx - Updated import path
  • Removed: src/app/employer/documents/fetchWithRetries.ts - Duplicate file
  • Removed: src/app/employee/documents/fetchWithRetries.ts - Duplicate file

Usage Example

// Basic usage (same as before)
const data = await fetchWithRetries('/api/data');

// With new options
const data = await fetchWithRetries('/api/data', {
  method: 'POST',
  body: JSON.stringify({ key: 'value' }),
}, {
  maxRetries: 3,
  useExponentialBackoff: true,
});

Breaking Changes

None - The existing API is preserved and works exactly as before.

Testing

  • Verified imports resolve correctly
  • Verified existing functionality works as expected
  • No TypeScript errors

Move fetchWithRetries function from app-specific locations to shared
lib directory to follow DRY principle.

Changes:
- Create src/lib/fetchWithRetries.ts with enhanced features:
  - Configurable retry options (maxRetries, baseDelay)
  - Exponential backoff support
  - Custom retry condition callback
  - Better TypeScript types and documentation
- Update imports in employer/documents/page.tsx
- Update imports in employee/documents/page.tsx
- Remove duplicate files from both locations

The new implementation maintains backward compatibility while providing
more flexibility for future use cases.
@vercel
Copy link

vercel bot commented Dec 16, 2025

@jeanpunt is attempting to deploy a commit to the Timothy Lin's projects Team on Vercel.

A member of the Team first needs to authorize it.

@vercel
Copy link

vercel bot commented Dec 16, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
pdr-ai-v2 Error Error Dec 16, 2025 10:09pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant