Skip to main content

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

OptionTypeDefaultDescription
timeoutnumber10000 (10 seconds)Maximum request duration in milliseconds before it’s automatically aborted.
maxRetriesnumber0Number of retry attempts for retryable HTTP status codes.
shouldRetry(response: Response) => booleanRetry on 502, 503, 504Function to determine if a request should be retried based on the response.
delayFn(ms: number) => Promise<void>setTimeout-based delayAsynchronous delay function used between retries.
onRequestStart(method: string, url: string, options: RequestInit) => voidnullHook called just before sending a request.
onRequestEnd(response: Response) => voidnullHook called after receiving a successful response.
onRequestError(error: any) => voidnullHook 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:

HookSignatureWhen It Runs
onRequestStart(method: string, url: string, options: RequestInit) => voidJust before the request is sent
onRequestEnd(response: Response) => voidImmediately after a successful response
onRequestError(error: any) => voidIf 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 to LunexClient, built-in defaults are used.
  • This options class is useful for logging, diagnostics, retry customization, and request control.