fetch-api-client
Version:
A TypeScript API client using fetch with axios-like interface
56 lines (55 loc) • 2.13 kB
TypeScript
import { ClientConfig, RequestConfig, ApiResponse } from './types';
import { RequestInterceptorManager, ResponseInterceptorManager } from './interceptors';
/**
* Main API Client class that provides axios-like interface using fetch
*/
export declare class ApiClient {
private config;
interceptors: {
request: RequestInterceptorManager;
response: ResponseInterceptorManager;
};
constructor(config?: ClientConfig);
/**
* Build complete URL from base URL and endpoint
*/
private buildUrl;
/**
* Prepare headers for the request
*/
private prepareHeaders;
/**
* Prepare request body
*/
private prepareBody;
/**
* Create AbortController with timeout
*/
private createAbortController;
/**
* Parse response based on content type
*/
private parseResponse;
/**
* Create standardized error object
*/
private createError;
/**
* Core request method - Enhanced with CORS-friendly credentials handling
*/
request<T = any>(config: RequestConfig): Promise<ApiResponse<T>>;
/**
* HTTP methods remain the same but ensure credentials are properly passed
*/
get<T = any>(url: string, config?: Omit<RequestConfig, 'method' | 'url'>): Promise<ApiResponse<T>>;
post<T = any>(url: string, data?: any, config?: Omit<RequestConfig, 'method' | 'url' | 'data'>): Promise<ApiResponse<T>>;
put<T = any>(url: string, data?: any, config?: Omit<RequestConfig, 'method' | 'url' | 'data'>): Promise<ApiResponse<T>>;
delete<T = any>(url: string, config?: Omit<RequestConfig, 'method' | 'url'>): Promise<ApiResponse<T>>;
patch<T = any>(url: string, data?: any, config?: Omit<RequestConfig, 'method' | 'url' | 'data'>): Promise<ApiResponse<T>>;
head<T = any>(url: string, config?: Omit<RequestConfig, 'method' | 'url'>): Promise<ApiResponse<T>>;
options<T = any>(url: string, config?: Omit<RequestConfig, 'method' | 'url'>): Promise<ApiResponse<T>>;
}
/**
* Create a new API client instance
*/
export declare function createClient(config?: ClientConfig): ApiClient;