UNPKG

guardz-axios

Version:

Type-safe HTTP client built on top of Axios with runtime validation using guardz. Part of the guardz ecosystem for comprehensive TypeScript type safety.

82 lines 2.31 kB
import { TypeGuardFn } from "guardz"; import { AxiosResponse } from "axios"; /** * Safely extract data from Axios response with type validation */ export declare function safeExtractData<T>(response: unknown, dataGuard: TypeGuardFn<T>): { success: true; data: T; response: AxiosResponse; } | { success: false; error: string; }; /** * Validate and extract paginated response data */ export declare function extractPaginatedData<T>(response: unknown, itemGuard: TypeGuardFn<T>): { success: true; data: T[]; pagination: { page: number; total: number; hasNext: boolean; }; response: AxiosResponse; } | { success: false; error: string; }; /** * Handle Axios errors with detailed categorization */ export declare function handleAxiosError(error: unknown): { category: "network" | "timeout" | "cancel" | "client" | "server" | "unknown"; message: string; statusCode?: number; errorCode?: string; isRetryable: boolean; details?: any; }; /** * Create a response validator with custom error messages */ export declare function createResponseValidator<T>(dataGuard: TypeGuardFn<T>, options?: { allowedStatuses?: number[]; customErrorMessages?: Record<number, string>; validateContentType?: string; }): (response: unknown) => { success: true; data: T; response: AxiosResponse; } | { success: false; error: string; statusCode?: number; }; /** * Retry configuration for Axios requests */ export interface RetryConfig { maxRetries: number; baseDelay: number; maxDelay: number; retryCondition?: (error: unknown) => boolean; } /** * Default retry condition - retry on network errors, timeouts, and 5xx server errors */ export declare function defaultRetryCondition(error: unknown): boolean; /** * Calculate exponential backoff delay */ export declare function calculateBackoffDelay(attempt: number, baseDelay: number, maxDelay: number): number; /** * Utility to check if error should trigger authentication flow */ export declare function shouldTriggerAuth(error: unknown): boolean; /** * Extract error message from Axios error */ export declare function extractErrorMessage(error: unknown, fallback?: string): string; //# sourceMappingURL=axios-utils.d.ts.map