UNPKG

@andranik-arakelyan/js-utilities

Version:
48 lines (47 loc) 1.73 kB
import { AxiosInstance, AxiosResponse } from 'axios'; /** * Configuration options for the BaseApiClient. */ export interface BaseApiClientConfig { baseUrl: string; apiToken: string; urlPrefix?: string; } /** * Base API client class providing common request handling functionality. * All specific API clients should extend this class. */ export declare abstract class BaseApiClient { protected config: BaseApiClientConfig; protected axiosInstance: AxiosInstance; /** * Creates a new base API client instance. * @param config The configuration options for the API client */ constructor(config: BaseApiClientConfig); /** * Makes an HTTP request to the API. * @template T The expected response type * @param options Request configuration options * @param options.path The API endpoint path * @param options.query Optional query parameters * @param options.body Optional request body * @param options.method HTTP method (defaults to GET) * @returns The parsed response data * @throws {Error} If the request fails or returns an error response */ protected request<T>(options: { path: string; query?: Record<string, string | number | undefined>; body?: any; method?: "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; }): Promise<T>; /** * Handles API response parsing and error checking. * @template T The expected response type * @param response The axios Response object * @returns The parsed and validated response data * @throws {Error} If the response indicates an error or has success: false */ protected handleResponse<T>(response: AxiosResponse): T; }