LunexClientOptions
The LunexClientOptions
class defines optional configurations for customising the behaviour of the LunexClient
. These options enhance request handling with fine-grained control through timeout control, retry logic, and lifecycle hooks for logging or interception.
Features
- Request timeout control (in milliseconds)
- Retry configuration for transient HTTP errors
- Customizable retry decision logic
- Hook functions for logging or response tracking
- Pluggable delay function for retry intervals
Constructor
new LunexClientOptions(options?: LunexClientOptionsConfig)
The constructor accepts an optional configuration object (LunexClientOptionsConfig) that allows you to customize the behavior of the client.
Configuration Parameters
Option | Type | Default | Description |
---|---|---|---|
timeout | number | 10000 (10 seconds) | Maximum request duration in milliseconds before it’s automatically aborted. |
maxRetries | number | 0 | Number of retry attempts for retryable HTTP status codes. |
shouldRetry | (response: Response) => boolean | Retry on 502 , 503 , 504 | Function to determine if a request should be retried based on the response. |
delayFn | (ms: number) => Promise<void> | setTimeout -based delay | Asynchronous delay function used between retries. |
onRequestStart | (method: string, url: string, options: RequestInit) => void | null | Hook called just before sending a request. |
onRequestEnd | (response: Response) => void | null | Hook called after receiving a successful response. |
onRequestError | (error: any) => void | null | Hook called when a request throws an error. |
Example
import LunexClient, { LunexClientOptions } from 'lunex-http';
const options = new LunexClientOptions({
timeout: 15000,
maxRetries: 2,
shouldRetry: (res) => [502, 503, 504].includes(res.status),
delayFn: (ms) => new Promise(resolve => setTimeout(resolve, ms)),
onRequestStart: (method, url) => {
console.log(`Request started: [${method}] ${url}`);
},
onRequestEnd: (response) => {
console.log(`Response status: ${response.status}`);
},
onRequestError: (error) => {
console.error('Request failed:', error);
}
});
const client = new LunexClient("https://api.example.com", {
Authorization: "Bearer YOUR_TOKEN"
}, options);
Default Retry Logic
When maxRetries
is greater than 0, the client retries failed requests that match the shouldRetry
condition. By default, this includes HTTP status codes 502, 503, and 504.
shouldRetry: (response) => [502, 503, 504].includes(response.status)
The time between retries is handled by delayFn
, which defaults to:
(ms) => new Promise(resolve => setTimeout(resolve, ms))
You can customize this for exponential backoff or jittered retries.
Lifecycle Hooks
These optional callbacks allow you to tap into different phases of a request's lifecycle:
Hook | Signature | When It Runs |
---|---|---|
onRequestStart | (method: string, url: string, options: RequestInit) => void | Just before the request is sent |
onRequestEnd | (response: Response) => void | Immediately after a successful response |
onRequestError | (error: any) => void | If the request fails or throws an exception |
Notes
- You can omit any or all configuration fields—defaults will apply automatically.
- If no
LunexClientOptions
is passed toLunexClient
, built-in defaults are used. - This options class is useful for logging, diagnostics, retry customization, and request control.