/** * @file Unified Network Utilities * @description Standardized network information access, connection quality * detection, and online/offline status utilities. * * This module unifies patterns from: * - hooks/shared/networkUtils.ts * - utils/networkStatus.ts * * @module shared/network-utils */ /** * Connection type from Network Information API. */ export type ConnectionType = 'wifi' | '4g' | '3g' | '2g' | 'slow-2g' | 'ethernet' | 'bluetooth' | 'cellular' | 'none' | 'unknown'; /** * Effective connection type (speed-based). */ export type EffectiveType = 'slow-2g' | '2g' | '3g' | '4g'; /** * Network quality tier. */ export type NetworkQuality = 'excellent' | 'good' | 'fair' | 'poor' | 'offline'; /** * Network information from the Network Information API. */ export interface NetworkInfo { /** Whether the browser reports online status */ online: boolean; /** Connection type (wifi, cellular, etc.) */ type: ConnectionType; /** Effective connection type based on measured speed */ effectiveType: EffectiveType; /** Estimated downlink speed in Mbps */ downlinkMbps: number; /** Estimated round-trip time in milliseconds */ rttMs: number; /** Whether data saver mode is enabled */ saveData: boolean; /** Calculated quality tier */ quality: NetworkQuality; /** Timestamp of measurement */ timestamp: number; } /** * Get current network information. * * @returns Current network information with quality assessment * * @example * ```ts * const network = getNetworkInfo(); * if (network.quality === 'poor') { * // Show low-quality content * } * ``` */ export declare function getNetworkInfo(): NetworkInfo; /** * Check if browser is currently online. */ export declare function isOnline(): boolean; /** * Check if current connection meets minimum quality requirement. * * @param minimum - Minimum required effective type * @returns True if current connection meets requirement */ export declare function meetsMinimumQuality(minimum: EffectiveType): boolean; /** * Check if connection is considered slow. * * @param thresholds - Custom thresholds for slow detection */ export declare function isSlowConnection(thresholds?: { maxDownlinkMbps?: number; maxRttMs?: number; }): boolean; /** * Get human-readable quality label. */ export declare function getQualityLabel(quality?: NetworkQuality): string; /** * Callback for network status changes. */ export type NetworkChangeCallback = (info: NetworkInfo) => void; /** * Monitor network quality changes. * * @param callback - Function called when network quality changes * @returns Cleanup function to stop monitoring * * @example * ```ts * const unsubscribe = onNetworkChange((network) => { * console.log('Network changed:', network.quality); * }); * * // Later, cleanup * unsubscribe(); * ``` */ export declare function onNetworkChange(callback: NetworkChangeCallback): () => void; /** * Listen for online status changes only. */ export declare function onOnlineChange(callback: (online: boolean) => void): () => void; /** * Check if prefetching should be allowed based on network conditions. * * @param options - Prefetch configuration * @returns True if prefetching should proceed * * @example * ```ts * if (shouldAllowPrefetch({ minQuality: '3g', respectDataSaver: true })) { * prefetchNextPage(); * } * ``` */ export declare function shouldAllowPrefetch(options?: { /** Minimum required effective connection type */ minQuality?: EffectiveType; /** Skip prefetch if data saver is enabled */ respectDataSaver?: boolean; /** Skip prefetch if offline */ requireOnline?: boolean; }): boolean; /** * Get recommended resource loading strategy based on network. */ export declare function getLoadingStrategy(): 'eager' | 'lazy' | 'none'; /** * Get recommended image quality based on network. */ export declare function getRecommendedImageQuality(): 'high' | 'medium' | 'low'; /** * Ping an endpoint to check actual connectivity. * * Note: This function intentionally uses raw fetch() rather than apiClient because: * 1. It uses 'no-cors' mode which is only available with raw fetch * 2. Ping is a low-level network diagnostic, not an API call * 3. It must work with any URL, not just API endpoints * * @see {@link @/lib/api/api-client} for the main API client * * @param url - URL to ping * @param timeoutMs - Timeout in milliseconds * @returns True if ping succeeded */ export declare function pingEndpoint(url: string, timeoutMs?: number): Promise; /** * Measure actual round-trip time to an endpoint. * * @param url - URL to measure * @param samples - Number of samples to average * @returns Average RTT in milliseconds, or null if failed */ export declare function measureRtt(url: string, samples?: number): Promise;