update
This commit is contained in:
@@ -24,9 +24,46 @@ export class BunqHttpClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* Make an API request to bunq
|
||||
* Make an API request to bunq with automatic retry on rate limit
|
||||
*/
|
||||
public async request<T = any>(options: IBunqRequestOptions): Promise<T> {
|
||||
const maxRetries = 3;
|
||||
let lastError: Error;
|
||||
|
||||
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
||||
try {
|
||||
return await this.makeRequest<T>(options);
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
// Check if it's a rate limit error
|
||||
if (error instanceof BunqApiError) {
|
||||
const isRateLimitError = error.errors.some(e =>
|
||||
e.error_description.includes('Too many requests') ||
|
||||
e.error_description.includes('rate limit')
|
||||
);
|
||||
|
||||
if (isRateLimitError && attempt < maxRetries) {
|
||||
// Exponential backoff: 1s, 2s, 4s
|
||||
const backoffMs = Math.pow(2, attempt) * 1000;
|
||||
console.log(`Rate limit hit, backing off for ${backoffMs}ms (attempt ${attempt + 1}/${maxRetries + 1})`);
|
||||
await new Promise(resolve => setTimeout(resolve, backoffMs));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// For non-rate-limit errors or if we've exhausted retries, throw immediately
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
throw lastError!;
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method to make the actual request
|
||||
*/
|
||||
private async makeRequest<T = any>(options: IBunqRequestOptions): Promise<T> {
|
||||
let url = `${this.context.baseUrl}${options.endpoint}`;
|
||||
|
||||
// Prepare headers
|
||||
|
Reference in New Issue
Block a user