Class HttpClient

    HTTP client with built-in retry logic and timeout handling

    Features:

    • Automatic retries with exponential backoff
    • Configurable timeout per request
    • Default JSON content type
    • Proper error handling for HTTP status codes
    • Does not retry client errors (4xx)
    const client = new HttpClient({
    baseUrl: 'https://api.example.com',
    timeout: 10000,
    retries: 3
    })

    // GET request
    const data = await client.get<UserData>('/users/123')

    // POST request
    const result = await client.post<CreateResult>('/users', {
    name: 'John',
    email: 'john@example.com'
    })

    Constructors

    Methods

    Constructors

    Methods

    • Makes a GET request

      Type Parameters

      • T

      Parameters

      • path: string

        API path to request

      • Optionaloptions: RequestOptions

        Optional request options

      Returns Promise<T>

      Promise resolving to the parsed JSON response

      const user = await client.get<User>('/users/123')
      const users = await client.get<User[]>('/users', {
      headers: { 'X-Custom-Header': 'value' }
      })
    • Makes a POST request

      Type Parameters

      • T

      Parameters

      • path: string

        API path to request

      • Optionalbody: unknown

        Request body (will be JSON stringified)

      • Optionaloptions: RequestOptions

        Optional request options

      Returns Promise<T>

      Promise resolving to the parsed JSON response

      const result = await client.post<CreateResult>('/users', {
      name: 'John',
      email: 'john@example.com'
      })

      const response = await client.post<Response>('/api/action', payload, {
      timeout: 5000
      })
    • Makes an HTTP request with automatic retries and timeout handling

      Implements exponential backoff for retries:

      • 1st retry: wait 1 second
      • 2nd retry: wait 2 seconds
      • 3rd retry: wait 4 seconds

      Client errors (4xx) are not retried.

      Type Parameters

      • T

      Parameters

      • path: string

        API path to request (will be appended to baseUrl)

      • options: RequestOptions = {}

        Request options (method, headers, body, timeout)

      Returns Promise<T>

      Promise resolving to the parsed JSON response

      If all retry attempts fail or response is not ok

      const response = await client.request<UserData>('/users/123', {
      method: 'GET',
      headers: { 'Authorization': 'Bearer token' }
      })