import { default as React } from 'react'; import { TimeoutError } from './resilience'; /** * Result type for operations that can fail */ export type SafeResult = { success: true; data: T; } | { success: false; error: E; }; /** * JSON parse error */ export declare class JSONParseError extends Error { readonly isJSONParseError = true; readonly originalValue: string; constructor(message: string, originalValue: string); } /** * Operation timeout error */ export declare class OperationTimeoutError extends TimeoutError { readonly operationName: string; readonly timeoutMs: number; constructor(operationName: string, timeoutMs: number); } /** * Safely parse JSON with a fallback value * * @param value - The JSON string to parse * @param fallback - The fallback value if parsing fails * @returns The parsed value or fallback * * @example * ```typescript * const config = safeJSONParse(configString, { theme: 'light' }); * ``` */ export declare function safeJSONParse(value: string, fallback: T): T; /** * Parse JSON with detailed error information * * @param value - The JSON string to parse * @returns A SafeResult with parsed data or error * * @example * ```typescript * const result = parseJSONSafe(configString); * if (result.success) { * useConfig(result.data); * } else { * logError(result.error); * } * ``` */ export declare function parseJSONSafe(value: string): SafeResult; /** * Parse JSON with validation * * @param value - The JSON string to parse * @param validator - A function to validate the parsed result * @param fallback - Optional fallback value if parsing or validation fails * @returns The validated parsed value, fallback, or throws * * @example * ```typescript * const config = parseJSONWithValidation( * configString, * (data): data is Config => 'theme' in data, * defaultConfig * ); * ``` */ export declare function parseJSONWithValidation(value: string, validator: (data: unknown) => data is T, fallback?: T): T; /** * Stringify JSON safely with fallback * * @param value - The value to stringify * @param fallback - The fallback string if stringification fails * @returns The JSON string or fallback */ export declare function safeJSONStringify(value: unknown, fallback?: string): string; /** * Execute an async operation with timeout * * @param operation - The async operation to execute * @param timeoutMs - Timeout in milliseconds * @param operationName - Name of the operation for error messages * @returns The operation result * @throws OperationTimeoutError if the operation times out * * @example * ```typescript * const data = await withOperationTimeout( * () => fetchData(), * 5000, * 'fetchData' * ); * ``` */ export declare function withOperationTimeout(operation: () => Promise, timeoutMs: number, operationName?: string): Promise; /** * Execute an async operation with timeout and fallback * * @param operation - The async operation to execute * @param timeoutMs - Timeout in milliseconds * @param fallback - Fallback value if operation times out * @param operationName - Name of the operation for logging * @returns The operation result or fallback * * @example * ```typescript * const config = await withTimeoutFallback( * () => fetchRemoteConfig(), * 5000, * defaultConfig, * 'fetchRemoteConfig' * ); * ``` */ export declare function withTimeoutFallback(operation: () => Promise, timeoutMs: number, fallback: T, operationName?: string): Promise; /** * Execute a sync operation with timeout using a promise wrapper * Note: This doesn't actually interrupt the sync operation, * but provides timeout behavior for tracking purposes * * @param operation - The sync operation to execute * @param timeoutMs - Timeout in milliseconds * @param operationName - Name of the operation */ export declare function withSyncTimeout(operation: () => T, timeoutMs: number, operationName?: string): Promise; /** * Safely get item from localStorage with fallback * * @param key - The storage key * @param fallback - The fallback value if retrieval fails * @returns The stored value or fallback */ export declare function safeLocalStorageGet(key: string, fallback: T): T; /** * Safely set item in localStorage * * @param key - The storage key * @param value - The value to store * @returns true if successful, false otherwise */ export declare function safeLocalStorageSet(key: string, value: unknown): boolean; /** * Safely remove item from localStorage * * @param key - The storage key * @returns true if successful, false otherwise */ export declare function safeLocalStorageRemove(key: string): boolean; /** * Safely get item from sessionStorage with fallback */ export declare function safeSessionStorageGet(key: string, fallback: T): T; /** * Safely set item in sessionStorage */ export declare function safeSessionStorageSet(key: string, value: unknown): boolean; /** * Safely access a nested property with fallback * * @param obj - The object to access * @param path - The property path (e.g., 'user.profile.name') * @param fallback - The fallback value if access fails * @returns The property value or fallback * * @example * ```typescript * const name = safeGet(user, 'profile.name', 'Anonymous'); * ``` */ export declare function safeGet(obj: unknown, path: string, fallback: T): T; /** * Execute a function safely, catching any errors * * @param fn - The function to execute * @param fallback - The fallback value if execution fails * @param onError - Optional error handler * @returns The function result or fallback */ export declare function safeExecute(fn: () => T, fallback: T, onError?: (error: unknown) => void): T; /** * Execute an async function safely, catching any errors * * @param fn - The async function to execute * @param fallback - The fallback value if execution fails * @param onError - Optional error handler * @returns The function result or fallback */ export declare function safeExecuteAsync(fn: () => Promise, fallback: T, onError?: (error: unknown) => void): Promise; /** * Execute multiple promises and collect results with errors * This is a wrapper around Promise.allSettled with better typing * * @param promises - Array of promises to execute * @returns Object with successful results and errors * * @example * ```typescript * const { results, errors } = await safePromiseAll([ * fetchUser(), * fetchPosts(), * fetchComments(), * ]); * ``` */ export declare function safePromiseAll(promises: Promise[]): Promise<{ results: T[]; errors: { index: number; error: unknown; }[]; allSucceeded: boolean; }>; /** * Execute multiple named promises and collect results with errors * * @param namedPromises - Object with named promises * @returns Object with results, errors, and success status * * @example * ```typescript * const { results, errors } = await safePromiseAllNamed({ * user: fetchUser(), * posts: fetchPosts(), * comments: fetchComments(), * }); * * if (results.user) { * // User was fetched successfully * } * ``` */ export declare function safePromiseAllNamed>>(namedPromises: T): Promise<{ results: { [K in keyof T]?: Awaited; }; errors: { [K in keyof T]?: unknown; }; allSucceeded: boolean; }>; /** * Create a safe event handler that catches errors * * @param handler - The event handler function * @param onError - Optional error handler * @returns A wrapped handler that catches errors * * @example * ```typescript * * ``` */ export declare function createSafeHandler(handler: (event: E) => void | Promise, onError?: (error: unknown, event: E) => void): (event: E) => void; /** * Create a safe React event handler */ export declare function createSafeReactHandler(handler: (event: E) => void | Promise, onError?: (error: unknown, event: E) => void): (event: E) => void;