# @bernierllc/http-client-base

Shared HTTP client utilities for API and download connectors, wrapping axios with retry and rate limiting support.

## Overview

This core package provides a unified HTTP client interface built on top of axios, with integrated retry policies and rate limiting. It's designed to be used by both API connectors and download connectors to ensure consistent HTTP behavior across the data ingestion system.

## Installation

```bash
npm install @bernierllc/http-client-base
```

## Usage

### Basic Usage

```typescript
import { HttpClient } from '@bernierllc/http-client-base';

const client = new HttpClient({
  baseURL: 'https://api.example.com',
  timeout: 30000
});

// GET request
const response = await client.get('/users');
console.log(response.data);

// POST request
const result = await client.post('/users', { name: 'John' });
```

### With Authentication

```typescript
const client = new HttpClient({
  baseURL: 'https://api.example.com',
  auth: {
    type: 'bearer',
    token: 'your-token-here'
  }
});
```

### With Retry and Rate Limiting

```typescript
const client = new HttpClient({
  baseURL: 'https://api.example.com',
  retryConfig: {
    enabled: true,
    maxAttempts: 3,
    retryableStatusCodes: [429, 500, 502, 503, 504]
  },
  rateLimitConfig: {
    enabled: true,
    requestsPerSecond: 10,
    requestsPerMinute: 100
  }
});
```

### With Interceptors

```typescript
const client = new HttpClient({
  baseURL: 'https://api.example.com',
  interceptors: {
    request: [
      {
        onFulfilled: (config) => {
          config.headers['X-Custom-Header'] = 'value';
          return config;
        }
      }
    ],
    response: [
      {
        onFulfilled: (response) => {
          // Transform response
          return response;
        }
      }
    ]
  }
});
```

### File Download

```typescript
const buffer = await client.download(
  'https://example.com/file.pdf',
  (progress) => {
    console.log(`Downloaded: ${progress.percent}%`);
  }
);
```

## API Reference

### HttpClient

Main HTTP client class wrapping axios.

#### Constructor

```typescript
new HttpClient(config?: HttpClientConfig)
```

#### Methods

- `request<T>(options: HttpRequestOptions): Promise<HttpResponse<T>>` - Make HTTP request
- `get<T>(url: string, options?): Promise<HttpResponse<T>>` - GET request
- `post<T>(url: string, data?, options?): Promise<HttpResponse<T>>` - POST request
- `put<T>(url: string, data?, options?): Promise<HttpResponse<T>>` - PUT request
- `patch<T>(url: string, data?, options?): Promise<HttpResponse<T>>` - PATCH request
- `delete<T>(url: string, options?): Promise<HttpResponse<T>>` - DELETE request
- `download(url: string, onProgress?, options?): Promise<Buffer>` - Download file

## Dependencies

- `axios@^1.7.0` - HTTP client library
- `@bernierllc/retry-policy` - Retry logic
- `@bernierllc/rate-limiter` - Rate limiting

## License

Bernier LLC - Limited Use License

