@pagamio/frontend-commons-lib
Version:
Pagamio library for Frontend reusable components like the form engine and table container
114 lines (113 loc) • 3.53 kB
TypeScript
/**
* @fileoverview API Context and utilities for managing a shared API client instance
* Provides context, provider component, and hooks for consistent API access across the application
*/
import { type ReactNode } from 'react';
import type { CustomAuthConfig, TokenManager } from '../auth';
import { ApiClient } from './client';
import type { MockConfigArray } from './types';
/**
* Props for the ApiProvider component
*
* @template T - Type extending CustomAuthConfig that defines the shape of authentication data
*
* @example
* ```tsx
* const apiClient = createApiClient<MyAuthConfig>({...});
*
* return (
* <ApiProvider apiClient={apiClient}>
* <App />
* </ApiProvider>
* );
* ```
*/
interface ApiProviderProps<T extends CustomAuthConfig> {
/** React children nodes */
children: ReactNode;
/** Configured API client instance */
apiClient: ApiClient<T>;
/** Whether to use mock API */
mocked?: boolean;
/** Mock configurations for API requests */
mockConfig?: MockConfigArray;
}
/**
* Provider component for the API context
* Makes the API client instance available to all child components
*
* @template T - Type extending CustomAuthConfig
*
* @example
* ```tsx
* function App() {
* return (
* <ApiProvider apiClient={apiClient}>
* <MyComponents />
* </ApiProvider>
* );
* }
* ```
*/
export declare function ApiProvider<T extends CustomAuthConfig>({ children, apiClient, mocked, mockConfig, }: Readonly<ApiProviderProps<T>>): import("react/jsx-runtime").JSX.Element;
/**
* Hook for accessing the API client instance
* Must be used within an ApiProvider component
*
* @template T - Type extending CustomAuthConfig for type-safe API operations
* @returns The shared API client instance
* @throws Error if used outside of ApiProvider
*
* @example
* ```tsx
* function MyComponent() {
* const api = useApi<MyAuthConfig>();
*
* const fetchData = async () => {
* try {
* const data = await api.get('/endpoint');
* // Handle response
* } catch (error) {
* // Handle error
* }
* };
*
* return <div>...</div>;
* }
* ```
*/
export declare function useApi<T extends CustomAuthConfig>(): ApiClient<T>;
/**
* Factory function to create a configured ApiClient instance
*
* @template T - Type extending CustomAuthConfig that defines the shape of authentication data
* @param config - Configuration options for the API client
* @param config.baseURL - Base URL for all API requests
* @param config.tokenManager - Token manager instance for handling authentication
* @param config.defaultHeaders - Default headers to include in all requests
* @param config.timeout - Request timeout in milliseconds (default: 30000)
* @param config.retries - Number of retry attempts for failed requests (default: 1)
* @returns Configured ApiClient instance
*
* @example
* ```typescript
* const apiClient = createApiClient<MyAuthConfig>({
* baseURL: 'https://api.example.com',
* tokenManager: tokenManager,
* defaultHeaders: {
* 'Content-Type': 'application/json',
* },
* timeout: 5000,
* retries: 2,
* });
* ```
*/
export declare function createApiClient<T extends CustomAuthConfig>({ baseURL, tokenManager, defaultHeaders, timeout, retries, credentials, }: {
baseURL: string;
tokenManager: TokenManager<T>;
defaultHeaders?: HeadersInit;
timeout?: number;
retries?: number;
credentials?: RequestCredentials;
}): ApiClient<T>;
export {};