feat(utils): add withRetry utility for automatic retry with exponenti…#15
Conversation
…al backoff
Add withRetry() higher-order function that wraps async operations with
automatic retry logic, exponential backoff, and rate limit awareness.
Features:
- Configurable maxRetries, initialDelayMs, backoffMultiplier, maxDelayMs
- Respects Figma API Retry-After header for rate limit errors (429)
- Optional retryOnlyRateLimits flag (default: true)
- onRetry callback for logging/UI updates
- Exponential backoff with configurable multiplier
- Delay capped at maxDelayMs to prevent excessive waits
Usage:
import { withRetry, fetcher } from '@figma-vars/hooks';
const fetchWithRetry = withRetry(
() => fetcher(url, token),
{
maxRetries: 3,
onRetry: (attempt, delay) => console.log(`Retry ${attempt} in ${delay}ms`)
}
);
const data = await fetchWithRetry();
Changes:
- src/utils/retry.ts: New withRetry function with RetryOptions interface
- src/utils/index.ts: Export withRetry and RetryOptions
- tests/utils/retry.test.ts: 16 tests covering all retry scenarios
Test coverage: 100%
Refs: Codex Audit Item #16
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #15 +/- ##
==========================================
+ Coverage 90.98% 91.28% +0.30%
==========================================
Files 35 36 +1
Lines 943 987 +44
Branches 267 284 +17
==========================================
+ Hits 858 901 +43
- Misses 84 85 +1
Partials 1 1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR adds a withRetry utility function that wraps async operations with automatic retry logic, exponential backoff, and rate limit awareness. The implementation provides a robust solution for handling transient failures, particularly Figma API rate limiting (HTTP 429 errors).
Key Changes:
- New
withRetryhigher-order function with configurable retry behavior including exponential backoff, max retries, and delay limits - Respects Retry-After headers from rate limit errors and provides optional callback for retry events
- Comprehensive test suite with 16 test cases achieving 100% code coverage
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/utils/retry.ts | Core implementation of withRetry function and RetryOptions interface with exponential backoff logic and rate limit handling |
| src/utils/index.ts | Exports withRetry function and RetryOptions type for public API access |
| tests/utils/retry.test.ts | Comprehensive test suite covering successful execution, rate limit handling, retry options, callbacks, and edge cases |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…al backoff
Add withRetry() higher-order function that wraps async operations with automatic retry logic, exponential backoff, and rate limit awareness.
Features:
Usage:
import { withRetry, fetcher } from '@figma-vars/hooks';
const fetchWithRetry = withRetry( () => fetcher(url, token), { maxRetries: 3, onRetry: (attempt, delay) => console.log(
Retry ${attempt} in ${delay}ms) } );const data = await fetchWithRetry();
Changes:
Test coverage: 100%
Refs: Codex Audit Item #16