react-native-apikit
Version:
Modern API toolkit for React Native and Expo with automatic token management, smart response parsing, and built-in error handling
70 lines (69 loc) • 2.33 kB
TypeScript
type ApiMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
export interface ApiRequestConfig {
url: string;
method?: ApiMethod;
data?: any;
params?: Record<string, any>;
headers?: Record<string, string>;
timeout?: number;
signal?: AbortSignal;
}
export interface ApiResponse<T = any> {
data: T;
status: number;
headers: Record<string, string>;
raw: Response | any;
}
export interface ApiError {
message: string;
status?: number;
details?: any;
isNetworkError?: boolean;
isTimeout?: boolean;
isUnauthorized?: boolean;
}
export interface ApiEngine {
request<T = any>(config: ApiRequestConfig): Promise<ApiResponse<T>>;
cancel?(requestId: string): void;
}
export interface TokenStorage {
getToken(): Promise<string | null>;
setToken(token: string): Promise<void>;
removeToken(): Promise<void>;
}
export interface UseApiState<T = any> {
loading: boolean;
error: ApiError | null;
data: T | null;
}
export interface CancelableRequest {
promise: Promise<void>;
cancel: () => void | undefined;
}
export interface UseApiReturn<T = any> extends UseApiState<T> {
get: (url: string, config?: ApiRequestConfig) => CancelableRequest;
post: (url: string, data?: any, config?: ApiRequestConfig) => CancelableRequest;
put: (url: string, data?: any, config?: ApiRequestConfig) => CancelableRequest;
patch: (url: string, data?: any, config?: ApiRequestConfig) => CancelableRequest;
del: (url: string, config?: ApiRequestConfig) => CancelableRequest;
reset: () => void;
}
export interface ApiCache {
get<T = any>(key: string): Promise<T | undefined> | T | undefined;
set<T = any>(key: string, value: T, ttlMs?: number): Promise<void> | void;
clear?(key?: string): Promise<void> | void;
}
export interface ApiKitConfig {
baseUrl?: string;
engine?: 'fetch' | 'axios' | ApiEngine;
tokenStorage?: TokenStorage;
onUnauthorized?: () => void;
retry?: number;
timeout?: number;
headers?: Record<string, string>;
onRequest?: (config: ApiRequestConfig) => Promise<ApiRequestConfig> | ApiRequestConfig;
onResponse?: (response: ApiResponse) => Promise<ApiResponse> | ApiResponse;
onError?: (error: ApiError) => Promise<ApiError> | ApiError;
cache?: ApiCache;
}
export {};