feat(429 handling): now handles 429 correctly
This commit is contained in:
@@ -5,7 +5,7 @@ export { SmartRequest } from './smartrequest.js';
|
||||
export { CoreResponse } from '../core/index.js';
|
||||
|
||||
// Export types
|
||||
export type { HttpMethod, ResponseType, FormField, RetryConfig, TimeoutConfig } from './types/common.js';
|
||||
export type { HttpMethod, ResponseType, FormField, RetryConfig, TimeoutConfig, RateLimitConfig } from './types/common.js';
|
||||
export {
|
||||
PaginationStrategy,
|
||||
type TPaginationConfig as PaginationConfig,
|
||||
|
@@ -3,7 +3,7 @@ import type { ICoreResponse } from '../core_base/types.js';
|
||||
import * as plugins from './plugins.js';
|
||||
import type { ICoreRequestOptions } from '../core_base/types.js';
|
||||
|
||||
import type { HttpMethod, ResponseType, FormField } from './types/common.js';
|
||||
import type { HttpMethod, ResponseType, FormField, RateLimitConfig } from './types/common.js';
|
||||
import {
|
||||
type TPaginationConfig,
|
||||
PaginationStrategy,
|
||||
@@ -14,6 +14,32 @@ import {
|
||||
} from './types/pagination.js';
|
||||
import { createPaginatedResponse } from './features/pagination.js';
|
||||
|
||||
/**
|
||||
* Parse Retry-After header value to milliseconds
|
||||
* @param retryAfter - The Retry-After header value (seconds or HTTP date)
|
||||
* @returns Delay in milliseconds
|
||||
*/
|
||||
function parseRetryAfter(retryAfter: string | string[]): number {
|
||||
// Handle array of values (take first)
|
||||
const value = Array.isArray(retryAfter) ? retryAfter[0] : retryAfter;
|
||||
|
||||
if (!value) return 0;
|
||||
|
||||
// Try to parse as seconds (number)
|
||||
const seconds = parseInt(value, 10);
|
||||
if (!isNaN(seconds)) {
|
||||
return seconds * 1000;
|
||||
}
|
||||
|
||||
// Try to parse as HTTP date
|
||||
const retryDate = new Date(value);
|
||||
if (!isNaN(retryDate.getTime())) {
|
||||
return Math.max(0, retryDate.getTime() - Date.now());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Modern fluent client for making HTTP requests
|
||||
*/
|
||||
@@ -23,6 +49,7 @@ export class SmartRequest<T = any> {
|
||||
private _retries: number = 0;
|
||||
private _queryParams: Record<string, string> = {};
|
||||
private _paginationConfig?: TPaginationConfig;
|
||||
private _rateLimitConfig?: RateLimitConfig;
|
||||
|
||||
/**
|
||||
* Create a new SmartRequest instance
|
||||
@@ -106,6 +133,21 @@ export class SmartRequest<T = any> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable automatic 429 (Too Many Requests) handling with configurable backoff
|
||||
*/
|
||||
handle429Backoff(config?: RateLimitConfig): this {
|
||||
this._rateLimitConfig = {
|
||||
maxRetries: config?.maxRetries ?? 3,
|
||||
respectRetryAfter: config?.respectRetryAfter ?? true,
|
||||
maxWaitTime: config?.maxWaitTime ?? 60000,
|
||||
fallbackDelay: config?.fallbackDelay ?? 1000,
|
||||
backoffFactor: config?.backoffFactor ?? 2,
|
||||
onRateLimit: config?.onRateLimit
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set HTTP headers
|
||||
*/
|
||||
@@ -142,6 +184,17 @@ export class SmartRequest<T = any> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set additional request options
|
||||
*/
|
||||
options(options: Partial<ICoreRequestOptions>): this {
|
||||
this._options = {
|
||||
...this._options,
|
||||
...options
|
||||
};
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Accept header to indicate what content type is expected
|
||||
*/
|
||||
@@ -305,14 +358,55 @@ export class SmartRequest<T = any> {
|
||||
|
||||
this._options.queryParams = this._queryParams;
|
||||
|
||||
// Handle retry logic
|
||||
// Track rate limit attempts separately
|
||||
let rateLimitAttempt = 0;
|
||||
let lastError: Error;
|
||||
|
||||
// Main retry loop
|
||||
for (let attempt = 0; attempt <= this._retries; attempt++) {
|
||||
try {
|
||||
const request = new CoreRequest(this._url, this._options as any);
|
||||
const response = await request.fire();
|
||||
return response as ICoreResponse<R>;
|
||||
const response = await request.fire() as ICoreResponse<R>;
|
||||
|
||||
// Check for 429 status if rate limit handling is enabled
|
||||
if (this._rateLimitConfig && response.status === 429) {
|
||||
if (rateLimitAttempt >= this._rateLimitConfig.maxRetries) {
|
||||
// Max rate limit retries reached, return the 429 response
|
||||
return response;
|
||||
}
|
||||
|
||||
let waitTime: number;
|
||||
|
||||
if (this._rateLimitConfig.respectRetryAfter && response.headers['retry-after']) {
|
||||
// Parse Retry-After header
|
||||
waitTime = parseRetryAfter(response.headers['retry-after']);
|
||||
|
||||
// Cap wait time to maxWaitTime
|
||||
waitTime = Math.min(waitTime, this._rateLimitConfig.maxWaitTime);
|
||||
} else {
|
||||
// Use exponential backoff
|
||||
waitTime = Math.min(
|
||||
this._rateLimitConfig.fallbackDelay * Math.pow(this._rateLimitConfig.backoffFactor, rateLimitAttempt),
|
||||
this._rateLimitConfig.maxWaitTime
|
||||
);
|
||||
}
|
||||
|
||||
// Call rate limit callback if provided
|
||||
if (this._rateLimitConfig.onRateLimit) {
|
||||
this._rateLimitConfig.onRateLimit(rateLimitAttempt + 1, waitTime);
|
||||
}
|
||||
|
||||
// Wait before retrying
|
||||
await new Promise(resolve => setTimeout(resolve, waitTime));
|
||||
|
||||
rateLimitAttempt++;
|
||||
// Decrement attempt to retry this attempt
|
||||
attempt--;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Success or non-429 error response
|
||||
return response;
|
||||
} catch (error) {
|
||||
lastError = error as Error;
|
||||
|
||||
|
@@ -46,4 +46,16 @@ export interface TimeoutConfig {
|
||||
connection?: number; // Connection timeout in ms
|
||||
socket?: number; // Socket idle timeout in ms
|
||||
response?: number; // Response timeout in ms
|
||||
}
|
||||
|
||||
/**
|
||||
* Rate limit configuration for handling 429 responses
|
||||
*/
|
||||
export interface RateLimitConfig {
|
||||
maxRetries?: number; // Maximum number of retries (default: 3)
|
||||
respectRetryAfter?: boolean; // Respect Retry-After header (default: true)
|
||||
maxWaitTime?: number; // Max wait time in ms (default: 60000)
|
||||
fallbackDelay?: number; // Delay when no Retry-After header (default: 1000)
|
||||
backoffFactor?: number; // Exponential backoff factor (default: 2)
|
||||
onRateLimit?: (attempt: number, waitTime: number) => void; // Callback for rate limit events
|
||||
}
|
Reference in New Issue
Block a user