Fetch Client API Documentation - v1.1.1
    Preparing search index...

    Class Client

    High-performance API client with automatic retries, interceptors, and type safety

    Modern HTTP client built on the native fetch API with enterprise-grade features including request/response interceptors, automatic retry logic with exponential backoff, comprehensive error handling, and full TypeScript support.

    // Basic usage
    const api = new Client({
    baseURL: 'https://api.example.com',
    timeout: 10000,
    headers: {
    'Authorization': 'Bearer your-token',
    'Content-Type': 'application/json'
    }
    });

    // Make requests
    const users = await api.get('/users');
    const newUser = await api.post('/users', { name: 'John', email: 'john@example.com' });

    // With interceptors
    api.interceptors.request.use((request) => {
    console.log('Sending request to:', request.url);
    return request;
    });

    api.interceptors.response.use((response) => {
    console.log('Received response:', response.status);
    return response;
    });
    Index

    Constructors

    • Create a new Client instance

      Parameters

      • config: ClientConfig = {}

        Client configuration options

        Configuration for the API client instance

        • OptionalbaseURL?: string

          Base URL for the request - will be combined with url

          'https://api.example.com'
          
        • Optionaltimeout?: number

          Request timeout in milliseconds before aborting

          10000 // 10 second timeout
          
        • Optionalretry?: number | RetryConfig

          Retry configuration for failed requests

          3 // Simple: retry 3 times with defaults
          
          { maxRetries: 5, delay: 2000 } // Advanced configuration
          
        • Optionalheaders?: Record<string, string>

          Default headers for all requests made by this client

          These headers will be merged with request-specific headers

          { 'Authorization': 'Bearer token', 'Content-Type': 'application/json' }
          
        • OptionalresponseType?: ResponseType

          Default response type for all requests

          Can be overridden per request

          'json' // Default to JSON parsing for all responses
          

      Returns Client

      Initializes the HTTP client with the provided configuration. Sets up interceptors and validates the configuration for common issues.

      // Minimal configuration
      const api = new Client();

      // Full configuration
      const api = new Client({
      baseURL: 'https://api.example.com',
      timeout: 15000,
      headers: {
      'User-Agent': 'MyApp/1.0',
      'Accept': 'application/json',
      'Authorization': 'Bearer your-token'
      },
      retryConfig: {
      retries: 3,
      retryDelay: 1000,
      retryCondition: (error) => error.status >= 500
      },
      responseType: 'json'
      });

      When configuration is invalid (negative timeout, invalid baseURL format)

    Properties

    interceptors: {
        request: Interceptor<ApiRequest>;
        response: Interceptor<ApiResponse>;
    }

    Request and response interceptors for middleware processing

    Provides hooks to transform requests before they're sent and responses before they're returned. Useful for authentication, logging, error handling, and data transformation.

    // Add authentication to all requests
    api.interceptors.request.use((request) => {
    request.headers.set('Authorization', `Bearer ${getToken()}`);
    return request;
    });

    // Handle errors globally
    api.interceptors.response.use((response) => {
    if (response.status === 401) {
    redirectToLogin();
    }
    return response;
    });

    Methods

    • Sends an HTTP GET request

      Type Parameters

      • T = unknown

        The expected response data type

      Parameters

      • url: string

        The endpoint path (relative to baseURL or absolute URL)

      • options: RequestOptions = {}

        Optional request configuration

      Returns Promise<T>

      Promise resolving to parsed response data of type T

      When the request fails or returns an error status

      // Simple GET request
      const users = await api.get<User[]>('/users');

      // GET with custom headers
      const user = await api.get<User>('/users/123', {
      headers: { 'Cache-Control': 'no-cache' }
      });

      // GET with timeout
      const data = await api.get('/slow-endpoint', {
      timeout: 5000
      });

    post

    • post<T = unknown>(
          url: string,
          data?: Body,
          options?: RequestOptions,
      ): Promise<T>

      Sends an HTTP POST request

      Type Parameters

      • T = unknown

        The expected response data type

      Parameters

      • url: string

        The endpoint path (relative to baseURL or absolute URL)

      • Optionaldata: Body

        Request body payload (JSON object, FormData, string, etc.)

      • options: RequestOptions = {}

        Optional request configuration

      Returns Promise<T>

      Promise resolving to parsed response data of type T

      When the request fails or returns an error status

      // POST JSON data
      const newUser = await api.post<User>('/users', {
      name: 'John Doe',
      email: 'john@example.com'
      });

      // POST FormData
      const formData = new FormData();
      formData.append('file', file);
      const result = await api.post('/upload', formData);

      // POST with custom headers
      const response = await api.post('/webhook', payload, {
      headers: { 'X-Webhook-Secret': secret }
      });
    • Sends an HTTP PUT request

      Type Parameters

      • T = unknown

        The expected response data type

      Parameters

      • url: string

        The endpoint path (relative to baseURL or absolute URL)

      • Optionaldata: Body

        Request body payload for updating the entire resource

      • options: RequestOptions = {}

        Optional request configuration

      Returns Promise<T>

      Promise resolving to parsed response data of type T

      When the request fails or returns an error status

      // Update entire user resource
      const updatedUser = await api.put<User>('/users/123', {
      id: 123,
      name: 'Jane Doe',
      email: 'jane@example.com',
      status: 'active'
      });
    • Sends an HTTP PATCH request

      Type Parameters

      • T = unknown

        The expected response data type

      Parameters

      • url: string

        The endpoint path (relative to baseURL or absolute URL)

      • Optionaldata: Body

        Partial payload to update specific fields of the resource

      • options: RequestOptions = {}

        Optional request configuration

      Returns Promise<T>

      Promise resolving to parsed response data of type T

      When the request fails or returns an error status

      // Partial update of user
      const user = await api.patch<User>('/users/123', {
      name: 'Updated Name' // Only update the name field
      });
    • Sends an HTTP DELETE request

      Type Parameters

      • T = unknown

        The expected response data type (often void or deletion confirmation)

      Parameters

      • url: string

        The endpoint path (relative to baseURL or absolute URL)

      • options: RequestOptions = {}

        Optional request configuration

      Returns Promise<T>

      Promise resolving to parsed response data of type T

      When the request fails or returns an error status

      // Delete a user
      await api.delete('/users/123');

      // Delete with confirmation response
      const result = await api.delete<{success: boolean}>('/users/123');
    • Sends an HTTP HEAD request.

      Type Parameters

      • T = unknown

      Parameters

      • url: string

        The endpoint path.

      • options: RequestOptions = {}

        Additional request options.

      Returns Promise<T>

      Parsed response headers as T.

    • Sets a default header applied to all outgoing requests.

      Parameters

      • name: string

        The header name.

      • value: string

        The header value.

      Returns void

    • Removes a default header.

      Parameters

      • name: string

        The header name to remove.

      Returns void

    • Executes the raw request flow:

      1. Merges configurations.
      2. Applies request interceptors.
      3. Sends the HTTP request.
      4. Applies response interceptors.

      Parameters

      • requestConfig: RequestConfig

        The user-supplied request configuration.

      Returns Promise<ApiResponse>

      The wrapped ApiResponse instance.

      If the request or response fails.