Compare commits

...

2 Commits

Author SHA1 Message Date
b4769e7feb feat(client): add handle429Backoff method for intelligent rate limit handling
Some checks failed
Default (tags) / security (push) Failing after 23s
Default (tags) / test (push) Failing after 14s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped
2025-07-29 13:49:50 +00:00
4cbca08f43 feat(429 handling): now handles 429 correctly 2025-07-29 13:19:43 +00:00
8 changed files with 380 additions and 46 deletions

View File

@@ -1,5 +1,37 @@
# Changelog # Changelog
## 2025-07-29 - 4.2.0 - feat(client)
Add handle429Backoff method for intelligent rate limit handling
**Features:**
- Added `handle429Backoff()` method to SmartRequest class for automatic HTTP 429 handling
- Respects `Retry-After` headers with support for both seconds and HTTP date formats
- Configurable exponential backoff when no Retry-After header is present
- Added `RateLimitConfig` interface with customizable retry behavior
- Optional callback for monitoring rate limit events
- Maximum wait time capping to prevent excessive delays
**Improvements:**
- Updated test endpoints to use more reliable services (jsonplaceholder, echo.zuplo.io)
- Added timeout parameter to test script for better CI/CD compatibility
**Documentation:**
- Added comprehensive rate limiting section to README with examples
- Documented all configuration options for handle429Backoff
## 2025-07-29 - 4.1.0 - feat(client)
Add missing options() method to SmartRequest client
**Features:**
- Added `options()` method to SmartRequest class for setting arbitrary request options
- Enables setting keepAlive and other platform-specific options via fluent API
- Added test coverage for keepAlive functionality
**Documentation:**
- Updated README with examples of using the `options()` method
- Added specific examples for enabling keepAlive connections
- Corrected all documentation to use `options()` instead of `option()`
## 2025-07-28 - 4.0.0 - BREAKING CHANGE(core) ## 2025-07-28 - 4.0.0 - BREAKING CHANGE(core)
Complete architectural overhaul with cross-platform support Complete architectural overhaul with cross-platform support

View File

@@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartrequest", "name": "@push.rocks/smartrequest",
"version": "4.0.1", "version": "4.2.0",
"private": false, "private": false,
"description": "A module for modern HTTP/HTTPS requests with support for form data, file uploads, JSON, binary data, streams, and more.", "description": "A module for modern HTTP/HTTPS requests with support for form data, file uploads, JSON, binary data, streams, and more.",
"exports": { "exports": {
@@ -10,7 +10,7 @@
}, },
"type": "module", "type": "module",
"scripts": { "scripts": {
"test": "(tstest test/ --verbose)", "test": "(tstest test/ --verbose --timeout 60)",
"build": "(tsbuild --web)", "build": "(tsbuild --web)",
"buildDocs": "tsdoc" "buildDocs": "tsdoc"
}, },

106
readme.md
View File

@@ -125,6 +125,25 @@ async function fetchWithRetry(url: string) {
} }
``` ```
### Setting Request Options
Use the `options()` method to set any request options supported by the underlying implementation:
```typescript
import { SmartRequest } from '@push.rocks/smartrequest';
// Set various options
const response = await SmartRequest.create()
.url('https://api.example.com/data')
.options({
keepAlive: true, // Enable connection reuse (Node.js)
timeout: 10000, // 10 second timeout
hardDataCuttingTimeout: 15000, // 15 second hard timeout
// Platform-specific options are also supported
})
.get();
```
### Working with Different Response Types ### Working with Different Response Types
The API provides a fetch-like interface for handling different response types: The API provides a fetch-like interface for handling different response types:
@@ -326,20 +345,85 @@ import { SmartRequest } from '@push.rocks/smartrequest';
// Enable keep-alive for better performance with multiple requests // Enable keep-alive for better performance with multiple requests
async function performMultipleRequests() { async function performMultipleRequests() {
const client = SmartRequest.create() // Note: keepAlive is NOT enabled by default
.header('Connection', 'keep-alive'); const response1 = await SmartRequest.create()
.url('https://api.example.com/endpoint1')
.options({ keepAlive: true })
.get();
// Requests will reuse the same connection in Node.js const response2 = await SmartRequest.create()
const results = await Promise.all([ .url('https://api.example.com/endpoint2')
client.url('https://api.example.com/endpoint1').get(), .options({ keepAlive: true })
client.url('https://api.example.com/endpoint2').get(), .get();
client.url('https://api.example.com/endpoint3').get()
]);
return Promise.all(results.map(r => r.json())); // Connections are pooled and reused when keepAlive is enabled
return [await response1.json(), await response2.json()];
} }
``` ```
### Rate Limiting (429 Too Many Requests) Handling
The library includes built-in support for handling HTTP 429 (Too Many Requests) responses with intelligent backoff:
```typescript
import { SmartRequest } from '@push.rocks/smartrequest';
// Simple usage - handle 429 with defaults
async function fetchWithRateLimitHandling() {
const response = await SmartRequest.create()
.url('https://api.example.com/data')
.handle429Backoff() // Automatically retry on 429
.get();
return await response.json();
}
// Advanced usage with custom configuration
async function fetchWithCustomRateLimiting() {
const response = await SmartRequest.create()
.url('https://api.example.com/data')
.handle429Backoff({
maxRetries: 5, // Try up to 5 times (default: 3)
respectRetryAfter: true, // Honor Retry-After header (default: true)
maxWaitTime: 30000, // Max 30 seconds wait (default: 60000)
fallbackDelay: 2000, // 2s initial delay if no Retry-After (default: 1000)
backoffFactor: 2, // Exponential backoff multiplier (default: 2)
onRateLimit: (attempt, waitTime) => {
console.log(`Rate limited. Attempt ${attempt}, waiting ${waitTime}ms`);
}
})
.get();
return await response.json();
}
// Example: API client with rate limit handling
class RateLimitedApiClient {
private async request(path: string) {
return SmartRequest.create()
.url(`https://api.example.com${path}`)
.handle429Backoff({
maxRetries: 3,
onRateLimit: (attempt, waitTime) => {
console.log(`API rate limit hit. Waiting ${waitTime}ms before retry ${attempt}`);
}
});
}
async fetchData(id: string) {
const response = await this.request(`/data/${id}`).get();
return response.json();
}
}
```
The rate limiting feature:
- Automatically detects 429 responses and retries with backoff
- Respects the `Retry-After` header when present (supports both seconds and HTTP date formats)
- Uses exponential backoff when no `Retry-After` header is provided
- Allows custom callbacks for monitoring rate limit events
- Caps maximum wait time to prevent excessive delays
## Platform-Specific Features ## Platform-Specific Features
### Browser-Specific Options ### Browser-Specific Options
@@ -349,7 +433,7 @@ When running in a browser, you can use browser-specific fetch options:
```typescript ```typescript
const response = await SmartRequest.create() const response = await SmartRequest.create()
.url('https://api.example.com/data') .url('https://api.example.com/data')
.option({ .options({
credentials: 'include', // Include cookies credentials: 'include', // Include cookies
mode: 'cors', // CORS mode mode: 'cors', // CORS mode
cache: 'no-cache', // Cache mode cache: 'no-cache', // Cache mode
@@ -367,7 +451,7 @@ import { Agent } from 'https';
const response = await SmartRequest.create() const response = await SmartRequest.create()
.url('https://api.example.com/data') .url('https://api.example.com/data')
.option({ .options({
agent: new Agent({ keepAlive: true }), // Custom agent agent: new Agent({ keepAlive: true }), // Custom agent
socketPath: '/var/run/api.sock', // Unix socket socketPath: '/var/run/api.sock', // Unix socket
}) })

View File

@@ -41,15 +41,17 @@ tap.test('browser: should handle request timeouts', async () => {
let timedOut = false; let timedOut = false;
const options: ICoreRequestOptions = { const options: ICoreRequestOptions = {
timeout: 1000 timeout: 100 // Very short timeout
}; };
try { try {
const request = new CoreRequest('https://httpbin.org/delay/10', options); // Use a URL that will likely take longer than 100ms
const request = new CoreRequest('https://jsonplaceholder.typicode.com/photos', options);
await request.fire(); await request.fire();
} catch (error) { } catch (error) {
timedOut = true; timedOut = true;
expect(error.message).toContain('timed out'); // Different browsers might have different timeout error messages
expect(error.message.toLowerCase()).toMatch(/timeout|timed out|aborted/i);
} }
expect(timedOut).toEqual(true); expect(timedOut).toEqual(true);
@@ -82,21 +84,22 @@ tap.test('browser: should handle POST requests with JSON', async () => {
tap.test('browser: should handle query parameters', async () => { tap.test('browser: should handle query parameters', async () => {
const options: ICoreRequestOptions = { const options: ICoreRequestOptions = {
queryParams: { queryParams: {
foo: 'bar', userId: '2'
baz: 'qux'
} }
}; };
const request = new CoreRequest('https://httpbin.org/get', options); const request = new CoreRequest('https://jsonplaceholder.typicode.com/posts', options);
const response = await request.fire(); const response = await request.fire();
expect(response.status).toEqual(200); expect(response.status).toEqual(200);
const data = await response.json(); const data = await response.json();
expect(data.args).toHaveProperty('foo'); expect(Array.isArray(data)).toBeTrue();
expect(data.args.foo).toEqual('bar'); // Verify we got posts filtered by userId 2
expect(data.args).toHaveProperty('baz'); if (data.length > 0) {
expect(data.args.baz).toEqual('qux'); expect(data[0]).toHaveProperty('userId');
expect(data[0].userId).toEqual(2);
}
}); });
export default tap.start(); export default tap.start();

View File

@@ -25,16 +25,16 @@ tap.test('client: should request a JSON document over https', async () => {
}); });
tap.test('client: should post a JSON document over http', async () => { tap.test('client: should post a JSON document over http', async () => {
const testData = { text: 'example_text' }; const testData = { title: 'example_text', body: 'test body', userId: 1 };
const response = await SmartRequest.create() const response = await SmartRequest.create()
.url('https://httpbin.org/post') .url('https://jsonplaceholder.typicode.com/posts')
.json(testData) .json(testData)
.post(); .post();
const body = await response.json(); const body = await response.json();
expect(body).toHaveProperty('json'); expect(body).toHaveProperty('title');
expect(body.json).toHaveProperty('text'); expect(body.title).toEqual('example_text');
expect(body.json.text).toEqual('example_text'); expect(body).toHaveProperty('id'); // jsonplaceholder returns an id for created posts
}); });
tap.test('client: should set headers correctly', async () => { tap.test('client: should set headers correctly', async () => {
@@ -42,38 +42,40 @@ tap.test('client: should set headers correctly', async () => {
const headerValue = 'test-value'; const headerValue = 'test-value';
const response = await SmartRequest.create() const response = await SmartRequest.create()
.url('https://httpbin.org/headers') .url('https://echo.zuplo.io/')
.header(customHeader, headerValue) .header(customHeader, headerValue)
.get(); .get();
const body = await response.json(); const body = await response.json();
expect(body).toHaveProperty('headers'); expect(body).toHaveProperty('headers');
// Check if the header exists (case-sensitive) // Check if the header exists (headers might be lowercase)
expect(body.headers).toHaveProperty(customHeader); const headers = body.headers;
expect(body.headers[customHeader]).toEqual(headerValue); const headerFound = headers[customHeader] || headers[customHeader.toLowerCase()] || headers['x-custom-header'];
expect(headerFound).toEqual(headerValue);
}); });
tap.test('client: should handle query parameters', async () => { tap.test('client: should handle query parameters', async () => {
const params = { param1: 'value1', param2: 'value2' }; const params = { userId: '1' };
const response = await SmartRequest.create() const response = await SmartRequest.create()
.url('https://httpbin.org/get') .url('https://jsonplaceholder.typicode.com/posts')
.query(params) .query(params)
.get(); .get();
const body = await response.json(); const body = await response.json();
expect(body).toHaveProperty('args'); expect(Array.isArray(body)).toBeTrue();
expect(body.args).toHaveProperty('param1'); // Check that we got posts for userId 1
expect(body.args.param1).toEqual('value1'); if (body.length > 0) {
expect(body.args).toHaveProperty('param2'); expect(body[0]).toHaveProperty('userId');
expect(body.args.param2).toEqual('value2'); expect(body[0].userId).toEqual(1);
}
}); });
tap.test('client: should handle timeout configuration', async () => { tap.test('client: should handle timeout configuration', async () => {
// This test just verifies that the timeout method doesn't throw // This test just verifies that the timeout method doesn't throw
const client = SmartRequest.create() const client = SmartRequest.create()
.url('https://httpbin.org/get') .url('https://jsonplaceholder.typicode.com/posts/1')
.timeout(5000); .timeout(5000);
const response = await client.get(); const response = await client.get();
@@ -84,7 +86,7 @@ tap.test('client: should handle timeout configuration', async () => {
tap.test('client: should handle retry configuration', async () => { tap.test('client: should handle retry configuration', async () => {
// This test just verifies that the retry method doesn't throw // This test just verifies that the retry method doesn't throw
const client = SmartRequest.create() const client = SmartRequest.create()
.url('https://httpbin.org/get') .url('https://jsonplaceholder.typicode.com/posts/1')
.retry(1); .retry(1);
const response = await client.get(); const response = await client.get();
@@ -92,4 +94,111 @@ tap.test('client: should handle retry configuration', async () => {
expect(response.ok).toBeTrue(); expect(response.ok).toBeTrue();
}); });
tap.test('client: should support keepAlive option for connection reuse', async () => {
// Test basic keepAlive functionality
const responses = [];
// Make multiple requests with keepAlive enabled
for (let i = 0; i < 3; i++) {
const response = await SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.options({ keepAlive: true })
.header('X-Request-Number', String(i))
.get();
expect(response.ok).toBeTrue();
responses.push(response);
}
// Verify all requests succeeded
expect(responses).toHaveLength(3);
// Also test that keepAlive: false works
const responseNoKeepAlive = await SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/2')
.options({ keepAlive: false })
.get();
expect(responseNoKeepAlive.ok).toBeTrue();
// Verify we can parse the responses
const data = await responses[0].json();
expect(data).toHaveProperty('id');
expect(data.id).toEqual(1);
});
tap.test('client: should handle 429 rate limiting with default config', async () => {
// Test that handle429Backoff can be configured without errors
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff();
const response = await client.get();
expect(response.status).toEqual(200);
});
tap.test('client: should handle 429 with custom config', async () => {
let rateLimitCallbackCalled = false;
let attemptCount = 0;
let waitTimeReceived = 0;
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff({
maxRetries: 2,
fallbackDelay: 500,
maxWaitTime: 5000,
onRateLimit: (attempt, waitTime) => {
rateLimitCallbackCalled = true;
attemptCount = attempt;
waitTimeReceived = waitTime;
}
});
const response = await client.get();
expect(response.status).toEqual(200);
// The callback should not have been called for a 200 response
expect(rateLimitCallbackCalled).toBeFalse();
});
tap.test('client: should respect Retry-After header format (seconds)', async () => {
// Test the configuration works - actual 429 testing would require a mock server
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff({
maxRetries: 1,
respectRetryAfter: true
});
const response = await client.get();
expect(response.ok).toBeTrue();
});
tap.test('client: should handle rate limiting with exponential backoff', async () => {
// Test exponential backoff configuration
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/1')
.handle429Backoff({
maxRetries: 3,
fallbackDelay: 100,
backoffFactor: 2,
maxWaitTime: 1000
});
const response = await client.get();
expect(response.status).toEqual(200);
});
tap.test('client: should not retry non-429 errors with rate limit handler', async () => {
// Test that 404 errors are not retried by rate limit handler
const client = SmartRequest.create()
.url('https://jsonplaceholder.typicode.com/posts/999999')
.handle429Backoff();
const response = await client.get();
expect(response.status).toEqual(404);
expect(response.ok).toBeFalse();
});
tap.start(); tap.start();

View File

@@ -5,7 +5,7 @@ export { SmartRequest } from './smartrequest.js';
export { CoreResponse } from '../core/index.js'; export { CoreResponse } from '../core/index.js';
// Export types // 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 { export {
PaginationStrategy, PaginationStrategy,
type TPaginationConfig as PaginationConfig, type TPaginationConfig as PaginationConfig,

View File

@@ -3,7 +3,7 @@ import type { ICoreResponse } from '../core_base/types.js';
import * as plugins from './plugins.js'; import * as plugins from './plugins.js';
import type { ICoreRequestOptions } from '../core_base/types.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 { import {
type TPaginationConfig, type TPaginationConfig,
PaginationStrategy, PaginationStrategy,
@@ -14,6 +14,32 @@ import {
} from './types/pagination.js'; } from './types/pagination.js';
import { createPaginatedResponse } from './features/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 * Modern fluent client for making HTTP requests
*/ */
@@ -23,6 +49,7 @@ export class SmartRequest<T = any> {
private _retries: number = 0; private _retries: number = 0;
private _queryParams: Record<string, string> = {}; private _queryParams: Record<string, string> = {};
private _paginationConfig?: TPaginationConfig; private _paginationConfig?: TPaginationConfig;
private _rateLimitConfig?: RateLimitConfig;
/** /**
* Create a new SmartRequest instance * Create a new SmartRequest instance
@@ -106,6 +133,21 @@ export class SmartRequest<T = any> {
return this; 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 * Set HTTP headers
*/ */
@@ -142,6 +184,17 @@ export class SmartRequest<T = any> {
return this; 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 * 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; this._options.queryParams = this._queryParams;
// Handle retry logic // Track rate limit attempts separately
let rateLimitAttempt = 0;
let lastError: Error; let lastError: Error;
// Main retry loop
for (let attempt = 0; attempt <= this._retries; attempt++) { for (let attempt = 0; attempt <= this._retries; attempt++) {
try { try {
const request = new CoreRequest(this._url, this._options as any); const request = new CoreRequest(this._url, this._options as any);
const response = await request.fire(); const response = await request.fire() as ICoreResponse<R>;
return response 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) { } catch (error) {
lastError = error as Error; lastError = error as Error;

View File

@@ -46,4 +46,16 @@ export interface TimeoutConfig {
connection?: number; // Connection timeout in ms connection?: number; // Connection timeout in ms
socket?: number; // Socket idle timeout in ms socket?: number; // Socket idle timeout in ms
response?: number; // Response 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
} }