feat(429 handling): now handles 429 correctly

This commit is contained in:
2025-07-29 13:19:43 +00:00
parent cf24bf94b9
commit 4cbca08f43
7 changed files with 266 additions and 19 deletions

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
The API provides a fetch-like interface for handling different response types:
@@ -326,17 +345,19 @@ import { SmartRequest } from '@push.rocks/smartrequest';
// Enable keep-alive for better performance with multiple requests
async function performMultipleRequests() {
const client = SmartRequest.create()
.header('Connection', 'keep-alive');
// Note: keepAlive is NOT enabled by default
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 results = await Promise.all([
client.url('https://api.example.com/endpoint1').get(),
client.url('https://api.example.com/endpoint2').get(),
client.url('https://api.example.com/endpoint3').get()
]);
const response2 = await SmartRequest.create()
.url('https://api.example.com/endpoint2')
.options({ keepAlive: true })
.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()];
}
```
@@ -349,7 +370,7 @@ When running in a browser, you can use browser-specific fetch options:
```typescript
const response = await SmartRequest.create()
.url('https://api.example.com/data')
.option({
.options({
credentials: 'include', // Include cookies
mode: 'cors', // CORS mode
cache: 'no-cache', // Cache mode
@@ -367,7 +388,7 @@ import { Agent } from 'https';
const response = await SmartRequest.create()
.url('https://api.example.com/data')
.option({
.options({
agent: new Agent({ keepAlive: true }), // Custom agent
socketPath: '/var/run/api.sock', // Unix socket
})