pagamio-frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
89 lines (88 loc) • 2.86 kB
TypeScript
/**
* @fileoverview Generic API client for making HTTP requests with authentication support
*/
import { type CustomAuthConfig } from '../auth';
import { type ApiClientConfig, type MockConfigArray, type RequestConfig } from './types';
export declare class ApiClient<T extends CustomAuthConfig> {
private readonly config;
private readonly activeControllers;
private mockConfig?;
constructor(config: ApiClientConfig<T>);
/**
* Sets the mock configurations for the API client.
* @param mockConfig - Array of mock configurations.
*/
setMockConfig(mockConfig: MockConfigArray): void;
/**
* Makes a GET request.
*/
get<ResponseType>(endpoint: string, config?: RequestConfig): Promise<ResponseType>;
/**
* Makes a POST request.
*/
post<ResponseType>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<ResponseType>;
/**
* Makes a PUT request.
*/
put<ResponseType>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<ResponseType>;
/**
* Makes a PATCH request.
*/
patch<ResponseType>(endpoint: string, data?: unknown, config?: RequestConfig): Promise<ResponseType>;
/**
* Makes a DELETE request.
*/
delete<ResponseType>(endpoint: string, config?: RequestConfig): Promise<ResponseType>;
/**
* Aborts all ongoing requests.
*/
/**
* Aborts all ongoing requests started without a custom signal.
*/
abort(): void;
/**
* Sets a default header for all requests.
*/
setDefaultHeader(key: string, value: string): void;
/**
* Finds a mock response that matches the request path, method, and params.
* @param path - The API endpoint path.
* @param method - The HTTP method.
* @param params - The query parameters or payload.
* @returns The mock response if a match is found, otherwise undefined.
*/
private findMockResponse;
/**
* Normalizes request parameters or body into a plain object.
* @param params - The query parameters or body.
* @returns A plain object representation of the parameters or body.
*/
private normalizeParams;
/**
* Creates a URL with query parameters.
*/
private createUrl;
/**
* Handles a request by checking for a matching mock response.
* If no mock is found, proceeds with the real API request.
*/
private handleRequest;
/**
* Makes the actual HTTP request.
*/
private makeRequest;
private buildRequestConfig;
private injectAuthHeader;
private applyOnRequestHook;
private fetchWithTimeout;
private shouldHandle401;
private handle401AndRetry;
/**
* Handles the API response.
*/
private handleResponse;
/**
* Handles API errors.
*/
private handleError;
}