rezo 1.0.44 → 1.0.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/entries/curl.d.ts +115 -0
- package/dist/adapters/entries/fetch.d.ts +115 -0
- package/dist/adapters/entries/http.d.ts +115 -0
- package/dist/adapters/entries/http2.d.ts +115 -0
- package/dist/adapters/entries/react-native.d.ts +115 -0
- package/dist/adapters/entries/xhr.d.ts +115 -0
- package/dist/adapters/fetch.cjs +18 -0
- package/dist/adapters/fetch.js +18 -0
- package/dist/adapters/http.cjs +22 -4
- package/dist/adapters/http.js +22 -4
- package/dist/adapters/http2.cjs +21 -0
- package/dist/adapters/http2.js +21 -0
- package/dist/adapters/index.cjs +6 -6
- package/dist/adapters/xhr.cjs +19 -0
- package/dist/adapters/xhr.js +19 -0
- package/dist/cache/index.cjs +9 -9
- package/dist/core/hooks.cjs +4 -2
- package/dist/core/hooks.js +4 -2
- package/dist/crawler/index.cjs +40 -40
- package/dist/crawler.d.ts +115 -0
- package/dist/entries/crawler.cjs +5 -5
- package/dist/index.cjs +27 -27
- package/dist/index.d.ts +115 -0
- package/dist/internal/agents/index.cjs +10 -10
- package/dist/platform/browser.d.ts +115 -0
- package/dist/platform/bun.d.ts +115 -0
- package/dist/platform/deno.d.ts +115 -0
- package/dist/platform/node.d.ts +115 -0
- package/dist/platform/react-native.d.ts +115 -0
- package/dist/platform/worker.d.ts +115 -0
- package/dist/proxy/index.cjs +5 -5
- package/dist/proxy/index.js +1 -1
- package/dist/queue/index.cjs +8 -8
- package/dist/responses/universal/index.cjs +11 -11
- package/dist/utils/rate-limit-wait.cjs +217 -0
- package/dist/utils/rate-limit-wait.js +208 -0
- package/package.json +1 -1
|
@@ -1416,6 +1416,35 @@ export type OnTimeoutHook = (event: TimeoutEvent, config: RezoConfig) => void;
|
|
|
1416
1416
|
* Use for cleanup, logging
|
|
1417
1417
|
*/
|
|
1418
1418
|
export type OnAbortHook = (event: AbortEvent, config: RezoConfig) => void;
|
|
1419
|
+
/**
|
|
1420
|
+
* Rate limit wait event data - fired when waiting due to rate limiting
|
|
1421
|
+
*/
|
|
1422
|
+
export interface RateLimitWaitEvent {
|
|
1423
|
+
/** HTTP status code that triggered the wait (e.g., 429, 503) */
|
|
1424
|
+
status: number;
|
|
1425
|
+
/** Time to wait in milliseconds */
|
|
1426
|
+
waitTime: number;
|
|
1427
|
+
/** Current wait attempt number (1-indexed) */
|
|
1428
|
+
attempt: number;
|
|
1429
|
+
/** Maximum wait attempts configured */
|
|
1430
|
+
maxAttempts: number;
|
|
1431
|
+
/** Where the wait time was extracted from */
|
|
1432
|
+
source: "header" | "body" | "function" | "default";
|
|
1433
|
+
/** The header or body path used (if applicable) */
|
|
1434
|
+
sourcePath?: string;
|
|
1435
|
+
/** URL being requested */
|
|
1436
|
+
url: string;
|
|
1437
|
+
/** HTTP method of the request */
|
|
1438
|
+
method: string;
|
|
1439
|
+
/** Timestamp when the wait started */
|
|
1440
|
+
timestamp: number;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Hook called when rate limit wait occurs
|
|
1444
|
+
* Informational only - cannot abort the wait
|
|
1445
|
+
* Use for logging, monitoring, alerting
|
|
1446
|
+
*/
|
|
1447
|
+
export type OnRateLimitWaitHook = (event: RateLimitWaitEvent, config: RezoConfig) => void | Promise<void>;
|
|
1419
1448
|
/**
|
|
1420
1449
|
* Hook called before a proxy is selected
|
|
1421
1450
|
* Can return a specific proxy to override selection
|
|
@@ -1496,6 +1525,7 @@ export interface RezoHooks {
|
|
|
1496
1525
|
onTls: OnTlsHook[];
|
|
1497
1526
|
onTimeout: OnTimeoutHook[];
|
|
1498
1527
|
onAbort: OnAbortHook[];
|
|
1528
|
+
onRateLimitWait: OnRateLimitWaitHook[];
|
|
1499
1529
|
}
|
|
1500
1530
|
/**
|
|
1501
1531
|
* Create empty hooks object with all arrays initialized
|
|
@@ -2426,6 +2456,91 @@ export interface RezoRequestConfig<D = any> {
|
|
|
2426
2456
|
/** Weather to stop or continue retry when certain condition is met*/
|
|
2427
2457
|
condition?: (error: RezoError) => boolean | Promise<boolean>;
|
|
2428
2458
|
};
|
|
2459
|
+
/**
|
|
2460
|
+
* Rate limit wait configuration - wait and retry when receiving rate limit responses.
|
|
2461
|
+
*
|
|
2462
|
+
* This feature runs BEFORE the retry system. When a rate-limiting status code is received,
|
|
2463
|
+
* the client will wait for the specified time and automatically retry the request.
|
|
2464
|
+
*
|
|
2465
|
+
* **Basic Usage:**
|
|
2466
|
+
* - `waitOnStatus: true` - Enable waiting on 429 status (default behavior)
|
|
2467
|
+
* - `waitOnStatus: [429, 503]` - Enable waiting on specific status codes
|
|
2468
|
+
*
|
|
2469
|
+
* **Wait Time Sources:**
|
|
2470
|
+
* - `'retry-after'` - Use standard Retry-After header (default)
|
|
2471
|
+
* - `{ header: 'X-RateLimit-Reset' }` - Use custom header
|
|
2472
|
+
* - `{ body: 'retry_after' }` - Extract from JSON response body
|
|
2473
|
+
* - Custom function for complex logic
|
|
2474
|
+
*
|
|
2475
|
+
* @example
|
|
2476
|
+
* ```typescript
|
|
2477
|
+
* // Wait on 429 using Retry-After header
|
|
2478
|
+
* await rezo.get(url, { waitOnStatus: true });
|
|
2479
|
+
*
|
|
2480
|
+
* // Wait on 429 using custom header
|
|
2481
|
+
* await rezo.get(url, {
|
|
2482
|
+
* waitOnStatus: true,
|
|
2483
|
+
* waitTimeSource: { header: 'X-RateLimit-Reset' }
|
|
2484
|
+
* });
|
|
2485
|
+
*
|
|
2486
|
+
* // Wait on 429 extracting time from JSON body
|
|
2487
|
+
* await rezo.get(url, {
|
|
2488
|
+
* waitOnStatus: true,
|
|
2489
|
+
* waitTimeSource: { body: 'data.retry_after' }
|
|
2490
|
+
* });
|
|
2491
|
+
*
|
|
2492
|
+
* // Custom function for complex APIs
|
|
2493
|
+
* await rezo.get(url, {
|
|
2494
|
+
* waitOnStatus: [429, 503],
|
|
2495
|
+
* waitTimeSource: (response) => {
|
|
2496
|
+
* const reset = response.headers.get('x-ratelimit-reset');
|
|
2497
|
+
* return reset ? parseInt(reset) - Math.floor(Date.now() / 1000) : null;
|
|
2498
|
+
* }
|
|
2499
|
+
* });
|
|
2500
|
+
* ```
|
|
2501
|
+
*/
|
|
2502
|
+
waitOnStatus?: boolean | number[];
|
|
2503
|
+
/**
|
|
2504
|
+
* Where to extract the wait time from when rate-limited.
|
|
2505
|
+
*
|
|
2506
|
+
* - `'retry-after'` - Standard Retry-After header (default)
|
|
2507
|
+
* - `{ header: string }` - Custom header name (e.g., 'X-RateLimit-Reset')
|
|
2508
|
+
* - `{ body: string }` - JSON path in response body (e.g., 'data.retry_after', 'wait_seconds')
|
|
2509
|
+
* - Function - Custom logic receiving the response, return seconds to wait or null
|
|
2510
|
+
*
|
|
2511
|
+
* @default 'retry-after'
|
|
2512
|
+
*/
|
|
2513
|
+
waitTimeSource?: "retry-after" | {
|
|
2514
|
+
header: string;
|
|
2515
|
+
} | {
|
|
2516
|
+
body: string;
|
|
2517
|
+
} | ((response: {
|
|
2518
|
+
status: number;
|
|
2519
|
+
headers: RezoHeaders;
|
|
2520
|
+
data?: any;
|
|
2521
|
+
}) => number | null);
|
|
2522
|
+
/**
|
|
2523
|
+
* Maximum time to wait for rate limit in milliseconds.
|
|
2524
|
+
* If the extracted wait time exceeds this, the request will fail instead of waiting.
|
|
2525
|
+
* Set to 0 for unlimited wait time.
|
|
2526
|
+
*
|
|
2527
|
+
* @default 60000 (60 seconds)
|
|
2528
|
+
*/
|
|
2529
|
+
maxWaitTime?: number;
|
|
2530
|
+
/**
|
|
2531
|
+
* Default wait time in milliseconds if the wait time source returns nothing.
|
|
2532
|
+
* Used as fallback when Retry-After header or body path is not present.
|
|
2533
|
+
*
|
|
2534
|
+
* @default 1000 (1 second)
|
|
2535
|
+
*/
|
|
2536
|
+
defaultWaitTime?: number;
|
|
2537
|
+
/**
|
|
2538
|
+
* Maximum number of wait attempts before giving up.
|
|
2539
|
+
* After this many waits, the request will proceed to retry logic or fail.
|
|
2540
|
+
*
|
|
2541
|
+
* @default 3
|
|
2542
|
+
*/
|
|
2543
|
+
maxWaitAttempts?: number;
|
|
2429
2544
|
/** Whether to use a secure context for HTTPS requests */
|
|
2430
2545
|
useSecureContext?: boolean;
|
|
2431
2546
|
/** Custom secure context for TLS connections */
|
|
@@ -1416,6 +1416,35 @@ export type OnTimeoutHook = (event: TimeoutEvent, config: RezoConfig) => void;
|
|
|
1416
1416
|
* Use for cleanup, logging
|
|
1417
1417
|
*/
|
|
1418
1418
|
export type OnAbortHook = (event: AbortEvent, config: RezoConfig) => void;
|
|
1419
|
+
/**
|
|
1420
|
+
* Rate limit wait event data - fired when waiting due to rate limiting
|
|
1421
|
+
*/
|
|
1422
|
+
export interface RateLimitWaitEvent {
|
|
1423
|
+
/** HTTP status code that triggered the wait (e.g., 429, 503) */
|
|
1424
|
+
status: number;
|
|
1425
|
+
/** Time to wait in milliseconds */
|
|
1426
|
+
waitTime: number;
|
|
1427
|
+
/** Current wait attempt number (1-indexed) */
|
|
1428
|
+
attempt: number;
|
|
1429
|
+
/** Maximum wait attempts configured */
|
|
1430
|
+
maxAttempts: number;
|
|
1431
|
+
/** Where the wait time was extracted from */
|
|
1432
|
+
source: "header" | "body" | "function" | "default";
|
|
1433
|
+
/** The header or body path used (if applicable) */
|
|
1434
|
+
sourcePath?: string;
|
|
1435
|
+
/** URL being requested */
|
|
1436
|
+
url: string;
|
|
1437
|
+
/** HTTP method of the request */
|
|
1438
|
+
method: string;
|
|
1439
|
+
/** Timestamp when the wait started */
|
|
1440
|
+
timestamp: number;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Hook called when rate limit wait occurs
|
|
1444
|
+
* Informational only - cannot abort the wait
|
|
1445
|
+
* Use for logging, monitoring, alerting
|
|
1446
|
+
*/
|
|
1447
|
+
export type OnRateLimitWaitHook = (event: RateLimitWaitEvent, config: RezoConfig) => void | Promise<void>;
|
|
1419
1448
|
/**
|
|
1420
1449
|
* Hook called before a proxy is selected
|
|
1421
1450
|
* Can return a specific proxy to override selection
|
|
@@ -1496,6 +1525,7 @@ export interface RezoHooks {
|
|
|
1496
1525
|
onTls: OnTlsHook[];
|
|
1497
1526
|
onTimeout: OnTimeoutHook[];
|
|
1498
1527
|
onAbort: OnAbortHook[];
|
|
1528
|
+
onRateLimitWait: OnRateLimitWaitHook[];
|
|
1499
1529
|
}
|
|
1500
1530
|
/**
|
|
1501
1531
|
* Create empty hooks object with all arrays initialized
|
|
@@ -2426,6 +2456,91 @@ export interface RezoRequestConfig<D = any> {
|
|
|
2426
2456
|
/** Weather to stop or continue retry when certain condition is met*/
|
|
2427
2457
|
condition?: (error: RezoError) => boolean | Promise<boolean>;
|
|
2428
2458
|
};
|
|
2459
|
+
/**
|
|
2460
|
+
* Rate limit wait configuration - wait and retry when receiving rate limit responses.
|
|
2461
|
+
*
|
|
2462
|
+
* This feature runs BEFORE the retry system. When a rate-limiting status code is received,
|
|
2463
|
+
* the client will wait for the specified time and automatically retry the request.
|
|
2464
|
+
*
|
|
2465
|
+
* **Basic Usage:**
|
|
2466
|
+
* - `waitOnStatus: true` - Enable waiting on 429 status (default behavior)
|
|
2467
|
+
* - `waitOnStatus: [429, 503]` - Enable waiting on specific status codes
|
|
2468
|
+
*
|
|
2469
|
+
* **Wait Time Sources:**
|
|
2470
|
+
* - `'retry-after'` - Use standard Retry-After header (default)
|
|
2471
|
+
* - `{ header: 'X-RateLimit-Reset' }` - Use custom header
|
|
2472
|
+
* - `{ body: 'retry_after' }` - Extract from JSON response body
|
|
2473
|
+
* - Custom function for complex logic
|
|
2474
|
+
*
|
|
2475
|
+
* @example
|
|
2476
|
+
* ```typescript
|
|
2477
|
+
* // Wait on 429 using Retry-After header
|
|
2478
|
+
* await rezo.get(url, { waitOnStatus: true });
|
|
2479
|
+
*
|
|
2480
|
+
* // Wait on 429 using custom header
|
|
2481
|
+
* await rezo.get(url, {
|
|
2482
|
+
* waitOnStatus: true,
|
|
2483
|
+
* waitTimeSource: { header: 'X-RateLimit-Reset' }
|
|
2484
|
+
* });
|
|
2485
|
+
*
|
|
2486
|
+
* // Wait on 429 extracting time from JSON body
|
|
2487
|
+
* await rezo.get(url, {
|
|
2488
|
+
* waitOnStatus: true,
|
|
2489
|
+
* waitTimeSource: { body: 'data.retry_after' }
|
|
2490
|
+
* });
|
|
2491
|
+
*
|
|
2492
|
+
* // Custom function for complex APIs
|
|
2493
|
+
* await rezo.get(url, {
|
|
2494
|
+
* waitOnStatus: [429, 503],
|
|
2495
|
+
* waitTimeSource: (response) => {
|
|
2496
|
+
* const reset = response.headers.get('x-ratelimit-reset');
|
|
2497
|
+
* return reset ? parseInt(reset) - Math.floor(Date.now() / 1000) : null;
|
|
2498
|
+
* }
|
|
2499
|
+
* });
|
|
2500
|
+
* ```
|
|
2501
|
+
*/
|
|
2502
|
+
waitOnStatus?: boolean | number[];
|
|
2503
|
+
/**
|
|
2504
|
+
* Where to extract the wait time from when rate-limited.
|
|
2505
|
+
*
|
|
2506
|
+
* - `'retry-after'` - Standard Retry-After header (default)
|
|
2507
|
+
* - `{ header: string }` - Custom header name (e.g., 'X-RateLimit-Reset')
|
|
2508
|
+
* - `{ body: string }` - JSON path in response body (e.g., 'data.retry_after', 'wait_seconds')
|
|
2509
|
+
* - Function - Custom logic receiving the response, return seconds to wait or null
|
|
2510
|
+
*
|
|
2511
|
+
* @default 'retry-after'
|
|
2512
|
+
*/
|
|
2513
|
+
waitTimeSource?: "retry-after" | {
|
|
2514
|
+
header: string;
|
|
2515
|
+
} | {
|
|
2516
|
+
body: string;
|
|
2517
|
+
} | ((response: {
|
|
2518
|
+
status: number;
|
|
2519
|
+
headers: RezoHeaders;
|
|
2520
|
+
data?: any;
|
|
2521
|
+
}) => number | null);
|
|
2522
|
+
/**
|
|
2523
|
+
* Maximum time to wait for rate limit in milliseconds.
|
|
2524
|
+
* If the extracted wait time exceeds this, the request will fail instead of waiting.
|
|
2525
|
+
* Set to 0 for unlimited wait time.
|
|
2526
|
+
*
|
|
2527
|
+
* @default 60000 (60 seconds)
|
|
2528
|
+
*/
|
|
2529
|
+
maxWaitTime?: number;
|
|
2530
|
+
/**
|
|
2531
|
+
* Default wait time in milliseconds if the wait time source returns nothing.
|
|
2532
|
+
* Used as fallback when Retry-After header or body path is not present.
|
|
2533
|
+
*
|
|
2534
|
+
* @default 1000 (1 second)
|
|
2535
|
+
*/
|
|
2536
|
+
defaultWaitTime?: number;
|
|
2537
|
+
/**
|
|
2538
|
+
* Maximum number of wait attempts before giving up.
|
|
2539
|
+
* After this many waits, the request will proceed to retry logic or fail.
|
|
2540
|
+
*
|
|
2541
|
+
* @default 3
|
|
2542
|
+
*/
|
|
2543
|
+
maxWaitAttempts?: number;
|
|
2429
2544
|
/** Whether to use a secure context for HTTPS requests */
|
|
2430
2545
|
useSecureContext?: boolean;
|
|
2431
2546
|
/** Custom secure context for TLS connections */
|
|
@@ -1416,6 +1416,35 @@ export type OnTimeoutHook = (event: TimeoutEvent, config: RezoConfig) => void;
|
|
|
1416
1416
|
* Use for cleanup, logging
|
|
1417
1417
|
*/
|
|
1418
1418
|
export type OnAbortHook = (event: AbortEvent, config: RezoConfig) => void;
|
|
1419
|
+
/**
|
|
1420
|
+
* Rate limit wait event data - fired when waiting due to rate limiting
|
|
1421
|
+
*/
|
|
1422
|
+
export interface RateLimitWaitEvent {
|
|
1423
|
+
/** HTTP status code that triggered the wait (e.g., 429, 503) */
|
|
1424
|
+
status: number;
|
|
1425
|
+
/** Time to wait in milliseconds */
|
|
1426
|
+
waitTime: number;
|
|
1427
|
+
/** Current wait attempt number (1-indexed) */
|
|
1428
|
+
attempt: number;
|
|
1429
|
+
/** Maximum wait attempts configured */
|
|
1430
|
+
maxAttempts: number;
|
|
1431
|
+
/** Where the wait time was extracted from */
|
|
1432
|
+
source: "header" | "body" | "function" | "default";
|
|
1433
|
+
/** The header or body path used (if applicable) */
|
|
1434
|
+
sourcePath?: string;
|
|
1435
|
+
/** URL being requested */
|
|
1436
|
+
url: string;
|
|
1437
|
+
/** HTTP method of the request */
|
|
1438
|
+
method: string;
|
|
1439
|
+
/** Timestamp when the wait started */
|
|
1440
|
+
timestamp: number;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Hook called when rate limit wait occurs
|
|
1444
|
+
* Informational only - cannot abort the wait
|
|
1445
|
+
* Use for logging, monitoring, alerting
|
|
1446
|
+
*/
|
|
1447
|
+
export type OnRateLimitWaitHook = (event: RateLimitWaitEvent, config: RezoConfig) => void | Promise<void>;
|
|
1419
1448
|
/**
|
|
1420
1449
|
* Hook called before a proxy is selected
|
|
1421
1450
|
* Can return a specific proxy to override selection
|
|
@@ -1496,6 +1525,7 @@ export interface RezoHooks {
|
|
|
1496
1525
|
onTls: OnTlsHook[];
|
|
1497
1526
|
onTimeout: OnTimeoutHook[];
|
|
1498
1527
|
onAbort: OnAbortHook[];
|
|
1528
|
+
onRateLimitWait: OnRateLimitWaitHook[];
|
|
1499
1529
|
}
|
|
1500
1530
|
/**
|
|
1501
1531
|
* Create empty hooks object with all arrays initialized
|
|
@@ -2426,6 +2456,91 @@ export interface RezoRequestConfig<D = any> {
|
|
|
2426
2456
|
/** Weather to stop or continue retry when certain condition is met*/
|
|
2427
2457
|
condition?: (error: RezoError) => boolean | Promise<boolean>;
|
|
2428
2458
|
};
|
|
2459
|
+
/**
|
|
2460
|
+
* Rate limit wait configuration - wait and retry when receiving rate limit responses.
|
|
2461
|
+
*
|
|
2462
|
+
* This feature runs BEFORE the retry system. When a rate-limiting status code is received,
|
|
2463
|
+
* the client will wait for the specified time and automatically retry the request.
|
|
2464
|
+
*
|
|
2465
|
+
* **Basic Usage:**
|
|
2466
|
+
* - `waitOnStatus: true` - Enable waiting on 429 status (default behavior)
|
|
2467
|
+
* - `waitOnStatus: [429, 503]` - Enable waiting on specific status codes
|
|
2468
|
+
*
|
|
2469
|
+
* **Wait Time Sources:**
|
|
2470
|
+
* - `'retry-after'` - Use standard Retry-After header (default)
|
|
2471
|
+
* - `{ header: 'X-RateLimit-Reset' }` - Use custom header
|
|
2472
|
+
* - `{ body: 'retry_after' }` - Extract from JSON response body
|
|
2473
|
+
* - Custom function for complex logic
|
|
2474
|
+
*
|
|
2475
|
+
* @example
|
|
2476
|
+
* ```typescript
|
|
2477
|
+
* // Wait on 429 using Retry-After header
|
|
2478
|
+
* await rezo.get(url, { waitOnStatus: true });
|
|
2479
|
+
*
|
|
2480
|
+
* // Wait on 429 using custom header
|
|
2481
|
+
* await rezo.get(url, {
|
|
2482
|
+
* waitOnStatus: true,
|
|
2483
|
+
* waitTimeSource: { header: 'X-RateLimit-Reset' }
|
|
2484
|
+
* });
|
|
2485
|
+
*
|
|
2486
|
+
* // Wait on 429 extracting time from JSON body
|
|
2487
|
+
* await rezo.get(url, {
|
|
2488
|
+
* waitOnStatus: true,
|
|
2489
|
+
* waitTimeSource: { body: 'data.retry_after' }
|
|
2490
|
+
* });
|
|
2491
|
+
*
|
|
2492
|
+
* // Custom function for complex APIs
|
|
2493
|
+
* await rezo.get(url, {
|
|
2494
|
+
* waitOnStatus: [429, 503],
|
|
2495
|
+
* waitTimeSource: (response) => {
|
|
2496
|
+
* const reset = response.headers.get('x-ratelimit-reset');
|
|
2497
|
+
* return reset ? parseInt(reset) - Math.floor(Date.now() / 1000) : null;
|
|
2498
|
+
* }
|
|
2499
|
+
* });
|
|
2500
|
+
* ```
|
|
2501
|
+
*/
|
|
2502
|
+
waitOnStatus?: boolean | number[];
|
|
2503
|
+
/**
|
|
2504
|
+
* Where to extract the wait time from when rate-limited.
|
|
2505
|
+
*
|
|
2506
|
+
* - `'retry-after'` - Standard Retry-After header (default)
|
|
2507
|
+
* - `{ header: string }` - Custom header name (e.g., 'X-RateLimit-Reset')
|
|
2508
|
+
* - `{ body: string }` - JSON path in response body (e.g., 'data.retry_after', 'wait_seconds')
|
|
2509
|
+
* - Function - Custom logic receiving the response, return seconds to wait or null
|
|
2510
|
+
*
|
|
2511
|
+
* @default 'retry-after'
|
|
2512
|
+
*/
|
|
2513
|
+
waitTimeSource?: "retry-after" | {
|
|
2514
|
+
header: string;
|
|
2515
|
+
} | {
|
|
2516
|
+
body: string;
|
|
2517
|
+
} | ((response: {
|
|
2518
|
+
status: number;
|
|
2519
|
+
headers: RezoHeaders;
|
|
2520
|
+
data?: any;
|
|
2521
|
+
}) => number | null);
|
|
2522
|
+
/**
|
|
2523
|
+
* Maximum time to wait for rate limit in milliseconds.
|
|
2524
|
+
* If the extracted wait time exceeds this, the request will fail instead of waiting.
|
|
2525
|
+
* Set to 0 for unlimited wait time.
|
|
2526
|
+
*
|
|
2527
|
+
* @default 60000 (60 seconds)
|
|
2528
|
+
*/
|
|
2529
|
+
maxWaitTime?: number;
|
|
2530
|
+
/**
|
|
2531
|
+
* Default wait time in milliseconds if the wait time source returns nothing.
|
|
2532
|
+
* Used as fallback when Retry-After header or body path is not present.
|
|
2533
|
+
*
|
|
2534
|
+
* @default 1000 (1 second)
|
|
2535
|
+
*/
|
|
2536
|
+
defaultWaitTime?: number;
|
|
2537
|
+
/**
|
|
2538
|
+
* Maximum number of wait attempts before giving up.
|
|
2539
|
+
* After this many waits, the request will proceed to retry logic or fail.
|
|
2540
|
+
*
|
|
2541
|
+
* @default 3
|
|
2542
|
+
*/
|
|
2543
|
+
maxWaitAttempts?: number;
|
|
2429
2544
|
/** Whether to use a secure context for HTTPS requests */
|
|
2430
2545
|
useSecureContext?: boolean;
|
|
2431
2546
|
/** Custom secure context for TLS connections */
|
|
@@ -1416,6 +1416,35 @@ export type OnTimeoutHook = (event: TimeoutEvent, config: RezoConfig) => void;
|
|
|
1416
1416
|
* Use for cleanup, logging
|
|
1417
1417
|
*/
|
|
1418
1418
|
export type OnAbortHook = (event: AbortEvent, config: RezoConfig) => void;
|
|
1419
|
+
/**
|
|
1420
|
+
* Rate limit wait event data - fired when waiting due to rate limiting
|
|
1421
|
+
*/
|
|
1422
|
+
export interface RateLimitWaitEvent {
|
|
1423
|
+
/** HTTP status code that triggered the wait (e.g., 429, 503) */
|
|
1424
|
+
status: number;
|
|
1425
|
+
/** Time to wait in milliseconds */
|
|
1426
|
+
waitTime: number;
|
|
1427
|
+
/** Current wait attempt number (1-indexed) */
|
|
1428
|
+
attempt: number;
|
|
1429
|
+
/** Maximum wait attempts configured */
|
|
1430
|
+
maxAttempts: number;
|
|
1431
|
+
/** Where the wait time was extracted from */
|
|
1432
|
+
source: "header" | "body" | "function" | "default";
|
|
1433
|
+
/** The header or body path used (if applicable) */
|
|
1434
|
+
sourcePath?: string;
|
|
1435
|
+
/** URL being requested */
|
|
1436
|
+
url: string;
|
|
1437
|
+
/** HTTP method of the request */
|
|
1438
|
+
method: string;
|
|
1439
|
+
/** Timestamp when the wait started */
|
|
1440
|
+
timestamp: number;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Hook called when rate limit wait occurs
|
|
1444
|
+
* Informational only - cannot abort the wait
|
|
1445
|
+
* Use for logging, monitoring, alerting
|
|
1446
|
+
*/
|
|
1447
|
+
export type OnRateLimitWaitHook = (event: RateLimitWaitEvent, config: RezoConfig) => void | Promise<void>;
|
|
1419
1448
|
/**
|
|
1420
1449
|
* Hook called before a proxy is selected
|
|
1421
1450
|
* Can return a specific proxy to override selection
|
|
@@ -1496,6 +1525,7 @@ export interface RezoHooks {
|
|
|
1496
1525
|
onTls: OnTlsHook[];
|
|
1497
1526
|
onTimeout: OnTimeoutHook[];
|
|
1498
1527
|
onAbort: OnAbortHook[];
|
|
1528
|
+
onRateLimitWait: OnRateLimitWaitHook[];
|
|
1499
1529
|
}
|
|
1500
1530
|
/**
|
|
1501
1531
|
* Create empty hooks object with all arrays initialized
|
|
@@ -2426,6 +2456,91 @@ export interface RezoRequestConfig<D = any> {
|
|
|
2426
2456
|
/** Weather to stop or continue retry when certain condition is met*/
|
|
2427
2457
|
condition?: (error: RezoError) => boolean | Promise<boolean>;
|
|
2428
2458
|
};
|
|
2459
|
+
/**
|
|
2460
|
+
* Rate limit wait configuration - wait and retry when receiving rate limit responses.
|
|
2461
|
+
*
|
|
2462
|
+
* This feature runs BEFORE the retry system. When a rate-limiting status code is received,
|
|
2463
|
+
* the client will wait for the specified time and automatically retry the request.
|
|
2464
|
+
*
|
|
2465
|
+
* **Basic Usage:**
|
|
2466
|
+
* - `waitOnStatus: true` - Enable waiting on 429 status (default behavior)
|
|
2467
|
+
* - `waitOnStatus: [429, 503]` - Enable waiting on specific status codes
|
|
2468
|
+
*
|
|
2469
|
+
* **Wait Time Sources:**
|
|
2470
|
+
* - `'retry-after'` - Use standard Retry-After header (default)
|
|
2471
|
+
* - `{ header: 'X-RateLimit-Reset' }` - Use custom header
|
|
2472
|
+
* - `{ body: 'retry_after' }` - Extract from JSON response body
|
|
2473
|
+
* - Custom function for complex logic
|
|
2474
|
+
*
|
|
2475
|
+
* @example
|
|
2476
|
+
* ```typescript
|
|
2477
|
+
* // Wait on 429 using Retry-After header
|
|
2478
|
+
* await rezo.get(url, { waitOnStatus: true });
|
|
2479
|
+
*
|
|
2480
|
+
* // Wait on 429 using custom header
|
|
2481
|
+
* await rezo.get(url, {
|
|
2482
|
+
* waitOnStatus: true,
|
|
2483
|
+
* waitTimeSource: { header: 'X-RateLimit-Reset' }
|
|
2484
|
+
* });
|
|
2485
|
+
*
|
|
2486
|
+
* // Wait on 429 extracting time from JSON body
|
|
2487
|
+
* await rezo.get(url, {
|
|
2488
|
+
* waitOnStatus: true,
|
|
2489
|
+
* waitTimeSource: { body: 'data.retry_after' }
|
|
2490
|
+
* });
|
|
2491
|
+
*
|
|
2492
|
+
* // Custom function for complex APIs
|
|
2493
|
+
* await rezo.get(url, {
|
|
2494
|
+
* waitOnStatus: [429, 503],
|
|
2495
|
+
* waitTimeSource: (response) => {
|
|
2496
|
+
* const reset = response.headers.get('x-ratelimit-reset');
|
|
2497
|
+
* return reset ? parseInt(reset) - Math.floor(Date.now() / 1000) : null;
|
|
2498
|
+
* }
|
|
2499
|
+
* });
|
|
2500
|
+
* ```
|
|
2501
|
+
*/
|
|
2502
|
+
waitOnStatus?: boolean | number[];
|
|
2503
|
+
/**
|
|
2504
|
+
* Where to extract the wait time from when rate-limited.
|
|
2505
|
+
*
|
|
2506
|
+
* - `'retry-after'` - Standard Retry-After header (default)
|
|
2507
|
+
* - `{ header: string }` - Custom header name (e.g., 'X-RateLimit-Reset')
|
|
2508
|
+
* - `{ body: string }` - JSON path in response body (e.g., 'data.retry_after', 'wait_seconds')
|
|
2509
|
+
* - Function - Custom logic receiving the response, return seconds to wait or null
|
|
2510
|
+
*
|
|
2511
|
+
* @default 'retry-after'
|
|
2512
|
+
*/
|
|
2513
|
+
waitTimeSource?: "retry-after" | {
|
|
2514
|
+
header: string;
|
|
2515
|
+
} | {
|
|
2516
|
+
body: string;
|
|
2517
|
+
} | ((response: {
|
|
2518
|
+
status: number;
|
|
2519
|
+
headers: RezoHeaders;
|
|
2520
|
+
data?: any;
|
|
2521
|
+
}) => number | null);
|
|
2522
|
+
/**
|
|
2523
|
+
* Maximum time to wait for rate limit in milliseconds.
|
|
2524
|
+
* If the extracted wait time exceeds this, the request will fail instead of waiting.
|
|
2525
|
+
* Set to 0 for unlimited wait time.
|
|
2526
|
+
*
|
|
2527
|
+
* @default 60000 (60 seconds)
|
|
2528
|
+
*/
|
|
2529
|
+
maxWaitTime?: number;
|
|
2530
|
+
/**
|
|
2531
|
+
* Default wait time in milliseconds if the wait time source returns nothing.
|
|
2532
|
+
* Used as fallback when Retry-After header or body path is not present.
|
|
2533
|
+
*
|
|
2534
|
+
* @default 1000 (1 second)
|
|
2535
|
+
*/
|
|
2536
|
+
defaultWaitTime?: number;
|
|
2537
|
+
/**
|
|
2538
|
+
* Maximum number of wait attempts before giving up.
|
|
2539
|
+
* After this many waits, the request will proceed to retry logic or fail.
|
|
2540
|
+
*
|
|
2541
|
+
* @default 3
|
|
2542
|
+
*/
|
|
2543
|
+
maxWaitAttempts?: number;
|
|
2429
2544
|
/** Whether to use a secure context for HTTPS requests */
|
|
2430
2545
|
useSecureContext?: boolean;
|
|
2431
2546
|
/** Custom secure context for TLS connections */
|
|
@@ -1416,6 +1416,35 @@ export type OnTimeoutHook = (event: TimeoutEvent, config: RezoConfig) => void;
|
|
|
1416
1416
|
* Use for cleanup, logging
|
|
1417
1417
|
*/
|
|
1418
1418
|
export type OnAbortHook = (event: AbortEvent, config: RezoConfig) => void;
|
|
1419
|
+
/**
|
|
1420
|
+
* Rate limit wait event data - fired when waiting due to rate limiting
|
|
1421
|
+
*/
|
|
1422
|
+
export interface RateLimitWaitEvent {
|
|
1423
|
+
/** HTTP status code that triggered the wait (e.g., 429, 503) */
|
|
1424
|
+
status: number;
|
|
1425
|
+
/** Time to wait in milliseconds */
|
|
1426
|
+
waitTime: number;
|
|
1427
|
+
/** Current wait attempt number (1-indexed) */
|
|
1428
|
+
attempt: number;
|
|
1429
|
+
/** Maximum wait attempts configured */
|
|
1430
|
+
maxAttempts: number;
|
|
1431
|
+
/** Where the wait time was extracted from */
|
|
1432
|
+
source: "header" | "body" | "function" | "default";
|
|
1433
|
+
/** The header or body path used (if applicable) */
|
|
1434
|
+
sourcePath?: string;
|
|
1435
|
+
/** URL being requested */
|
|
1436
|
+
url: string;
|
|
1437
|
+
/** HTTP method of the request */
|
|
1438
|
+
method: string;
|
|
1439
|
+
/** Timestamp when the wait started */
|
|
1440
|
+
timestamp: number;
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Hook called when rate limit wait occurs
|
|
1444
|
+
* Informational only - cannot abort the wait
|
|
1445
|
+
* Use for logging, monitoring, alerting
|
|
1446
|
+
*/
|
|
1447
|
+
export type OnRateLimitWaitHook = (event: RateLimitWaitEvent, config: RezoConfig) => void | Promise<void>;
|
|
1419
1448
|
/**
|
|
1420
1449
|
* Hook called before a proxy is selected
|
|
1421
1450
|
* Can return a specific proxy to override selection
|
|
@@ -1496,6 +1525,7 @@ export interface RezoHooks {
|
|
|
1496
1525
|
onTls: OnTlsHook[];
|
|
1497
1526
|
onTimeout: OnTimeoutHook[];
|
|
1498
1527
|
onAbort: OnAbortHook[];
|
|
1528
|
+
onRateLimitWait: OnRateLimitWaitHook[];
|
|
1499
1529
|
}
|
|
1500
1530
|
/**
|
|
1501
1531
|
* Create empty hooks object with all arrays initialized
|
|
@@ -2426,6 +2456,91 @@ export interface RezoRequestConfig<D = any> {
|
|
|
2426
2456
|
/** Weather to stop or continue retry when certain condition is met*/
|
|
2427
2457
|
condition?: (error: RezoError) => boolean | Promise<boolean>;
|
|
2428
2458
|
};
|
|
2459
|
+
/**
|
|
2460
|
+
* Rate limit wait configuration - wait and retry when receiving rate limit responses.
|
|
2461
|
+
*
|
|
2462
|
+
* This feature runs BEFORE the retry system. When a rate-limiting status code is received,
|
|
2463
|
+
* the client will wait for the specified time and automatically retry the request.
|
|
2464
|
+
*
|
|
2465
|
+
* **Basic Usage:**
|
|
2466
|
+
* - `waitOnStatus: true` - Enable waiting on 429 status (default behavior)
|
|
2467
|
+
* - `waitOnStatus: [429, 503]` - Enable waiting on specific status codes
|
|
2468
|
+
*
|
|
2469
|
+
* **Wait Time Sources:**
|
|
2470
|
+
* - `'retry-after'` - Use standard Retry-After header (default)
|
|
2471
|
+
* - `{ header: 'X-RateLimit-Reset' }` - Use custom header
|
|
2472
|
+
* - `{ body: 'retry_after' }` - Extract from JSON response body
|
|
2473
|
+
* - Custom function for complex logic
|
|
2474
|
+
*
|
|
2475
|
+
* @example
|
|
2476
|
+
* ```typescript
|
|
2477
|
+
* // Wait on 429 using Retry-After header
|
|
2478
|
+
* await rezo.get(url, { waitOnStatus: true });
|
|
2479
|
+
*
|
|
2480
|
+
* // Wait on 429 using custom header
|
|
2481
|
+
* await rezo.get(url, {
|
|
2482
|
+
* waitOnStatus: true,
|
|
2483
|
+
* waitTimeSource: { header: 'X-RateLimit-Reset' }
|
|
2484
|
+
* });
|
|
2485
|
+
*
|
|
2486
|
+
* // Wait on 429 extracting time from JSON body
|
|
2487
|
+
* await rezo.get(url, {
|
|
2488
|
+
* waitOnStatus: true,
|
|
2489
|
+
* waitTimeSource: { body: 'data.retry_after' }
|
|
2490
|
+
* });
|
|
2491
|
+
*
|
|
2492
|
+
* // Custom function for complex APIs
|
|
2493
|
+
* await rezo.get(url, {
|
|
2494
|
+
* waitOnStatus: [429, 503],
|
|
2495
|
+
* waitTimeSource: (response) => {
|
|
2496
|
+
* const reset = response.headers.get('x-ratelimit-reset');
|
|
2497
|
+
* return reset ? parseInt(reset) - Math.floor(Date.now() / 1000) : null;
|
|
2498
|
+
* }
|
|
2499
|
+
* });
|
|
2500
|
+
* ```
|
|
2501
|
+
*/
|
|
2502
|
+
waitOnStatus?: boolean | number[];
|
|
2503
|
+
/**
|
|
2504
|
+
* Where to extract the wait time from when rate-limited.
|
|
2505
|
+
*
|
|
2506
|
+
* - `'retry-after'` - Standard Retry-After header (default)
|
|
2507
|
+
* - `{ header: string }` - Custom header name (e.g., 'X-RateLimit-Reset')
|
|
2508
|
+
* - `{ body: string }` - JSON path in response body (e.g., 'data.retry_after', 'wait_seconds')
|
|
2509
|
+
* - Function - Custom logic receiving the response, return seconds to wait or null
|
|
2510
|
+
*
|
|
2511
|
+
* @default 'retry-after'
|
|
2512
|
+
*/
|
|
2513
|
+
waitTimeSource?: "retry-after" | {
|
|
2514
|
+
header: string;
|
|
2515
|
+
} | {
|
|
2516
|
+
body: string;
|
|
2517
|
+
} | ((response: {
|
|
2518
|
+
status: number;
|
|
2519
|
+
headers: RezoHeaders;
|
|
2520
|
+
data?: any;
|
|
2521
|
+
}) => number | null);
|
|
2522
|
+
/**
|
|
2523
|
+
* Maximum time to wait for rate limit in milliseconds.
|
|
2524
|
+
* If the extracted wait time exceeds this, the request will fail instead of waiting.
|
|
2525
|
+
* Set to 0 for unlimited wait time.
|
|
2526
|
+
*
|
|
2527
|
+
* @default 60000 (60 seconds)
|
|
2528
|
+
*/
|
|
2529
|
+
maxWaitTime?: number;
|
|
2530
|
+
/**
|
|
2531
|
+
* Default wait time in milliseconds if the wait time source returns nothing.
|
|
2532
|
+
* Used as fallback when Retry-After header or body path is not present.
|
|
2533
|
+
*
|
|
2534
|
+
* @default 1000 (1 second)
|
|
2535
|
+
*/
|
|
2536
|
+
defaultWaitTime?: number;
|
|
2537
|
+
/**
|
|
2538
|
+
* Maximum number of wait attempts before giving up.
|
|
2539
|
+
* After this many waits, the request will proceed to retry logic or fail.
|
|
2540
|
+
*
|
|
2541
|
+
* @default 3
|
|
2542
|
+
*/
|
|
2543
|
+
maxWaitAttempts?: number;
|
|
2429
2544
|
/** Whether to use a secure context for HTTPS requests */
|
|
2430
2545
|
useSecureContext?: boolean;
|
|
2431
2546
|
/** Custom secure context for TLS connections */
|