-
Notifications
You must be signed in to change notification settings - Fork 2
feat(js client): add retry mechanism #353
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
Merged
GuillaumeDecMeetsMore
merged 5 commits into
master
from
guillaume/feat/js-client-add-retry-mechanism
Jul 3, 2025
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e2822e3
feat(js client): add retry mechanism
GuillaumeDecMeetsMore a1279b3
chore: enable by default the retry mechanism
GuillaumeDecMeetsMore 87eb292
refactor: use axios-retry
GuillaumeDecMeetsMore 5fd0143
chore: fix comment + doc
GuillaumeDecMeetsMore fe86490
chore: comment
GuillaumeDecMeetsMore File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,96 @@ | ||
| # Retry Mechanism for GET Requests | ||
|
|
||
| The Nittei JavaScript client supports automatic retry for GET requests when encountering connection errors like timeouts or connection resets. | ||
|
|
||
| ## Configuration | ||
|
|
||
| You can configure the retry mechanism when creating a client: | ||
|
|
||
| ```typescript | ||
| import { NitteiUserClient, type RetryConfig } from "@meetsmore/nittei"; | ||
|
|
||
| const retryConfig: RetryConfig = { | ||
| enabled: true, | ||
| maxRetries: 3, // Maximum number of retry attempts (default: 3) | ||
| baseDelay: 300, // Base delay in milliseconds (default: 300) | ||
| maxDelay: 5000, // Maximum delay in milliseconds (default: 5000) | ||
| }; | ||
|
|
||
| const client = NitteiUserClient({ | ||
| apiKey: "your-api-key", | ||
| retry: retryConfig, | ||
| }); | ||
| ``` | ||
|
|
||
| ## How It Works | ||
|
|
||
| The retry mechanism uses exponential backoff: | ||
|
|
||
| - **Attempt 1**: Immediate request | ||
| - **Attempt 2**: Wait 1 second (baseDelay) | ||
| - **Attempt 3**: Wait 2 seconds (baseDelay \* 2^1) | ||
| - **Attempt 4**: Wait 4 seconds (baseDelay \* 2^2) | ||
|
|
||
| The delay is capped at `maxDelay` to prevent excessive waiting times. | ||
|
|
||
| ## Retryable Errors | ||
|
|
||
| The following errors will trigger a retry: | ||
|
|
||
| - `ECONNRESET` - Connection reset | ||
| - `ECONNABORTED` - Connection aborted | ||
| - `ETIMEDOUT` - Request timeout | ||
| - `ENOTFOUND` - DNS lookup failed | ||
| - `ENETUNREACH` - Network unreachable | ||
| - Any error message containing "timeout", "network", or "connection" | ||
|
|
||
| ## Example Usage | ||
|
|
||
| ```typescript | ||
| // Enable retry for all GET requests | ||
| const client = NitteiUserClient({ | ||
| apiKey: "your-api-key", | ||
| retry: { | ||
| enabled: true, | ||
| maxRetries: 3, | ||
| baseDelay: 1000, | ||
| }, | ||
| }); | ||
|
|
||
| // This GET request will automatically retry on connection errors | ||
| try { | ||
| const user = await client.user.me(); | ||
| console.log("User:", user); | ||
| } catch (error) { | ||
| // If all retries fail, the original error is thrown | ||
| console.error("Failed after retries:", error); | ||
| } | ||
| ``` | ||
|
|
||
| ## Admin Client | ||
|
|
||
| The retry mechanism also works with the admin client: | ||
|
|
||
| ```typescript | ||
| import { NitteiClient } from "@meetsmore/nittei"; | ||
|
|
||
| const client = await NitteiClient({ | ||
| apiKey: "your-api-key", | ||
| retry: { | ||
| enabled: true, | ||
| maxRetries: 5, | ||
| baseDelay: 500, | ||
| maxDelay: 5000, | ||
| }, | ||
| }); | ||
|
|
||
| // All GET requests will retry on connection errors | ||
| const account = await client.account.me(); | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - Only GET requests are retried (POST, PUT, DELETE requests are not retried) | ||
| - HTTP status codes (4xx, 5xx) are not retried - only connection-level errors | ||
| - The retry mechanism is enabled by default (`enabled: true`) | ||
| - Each client instance can have its own retry configuration |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.