NetworkUtils: {
    addNetworkStatusListener: ((callback) => (() => void));
    fetchWithAuth: ((url, token, options?) => Promise<Response>);
    fetchWithCache: ((url, options?, cacheTime?) => Promise<Response>);
    fetchJson: (<T>(url, options?) => Promise<T>);
    fetchWithProgress: ((url, options?, onProgress?) => Promise<Response>);
    fetchWithRetry: ((url, options?, retries?, delay?) => Promise<Response>);
    fetchWithTimeout: ((url, options?, timeout?) => Promise<Response>);
    getNetworkSpeed: (() => null | string);
    getNetworkType: (() => null | string);
    isOnline: (() => boolean);
} = ...

Type declaration

  • addNetworkStatusListener: ((callback) => (() => void))

    Adds a listener for network status changes (online/offline).

    Returns

    Function to remove the listener.

    Example

    const remove = NetworkUtils.addNetworkStatusListener((online) => console.log('Online:', online));
    // ...later
    remove();
      • (callback): (() => void)
      • Parameters

        • callback: ((online) => void)

          Callback invoked with true if online, false if offline.

            • (online): void
            • Parameters

              • online: boolean

              Returns void

        Returns (() => void)

          • (): void
          • Returns void

  • fetchWithAuth: ((url, token, options?) => Promise<Response>)

    Makes a fetch request with Bearer token authentication.

    Returns

    The fetch response.

    Example

    const res = await NetworkUtils.fetchWithAuth('/api', 'token123');
    
      • (url, token, options?): Promise<Response>
      • Parameters

        • url: string

          The URL to fetch.

        • token: string

          The authentication token.

        • options: RequestInit = {}

          Additional fetch options.

        Returns Promise<Response>

  • fetchWithCache: ((url, options?, cacheTime?) => Promise<Response>)

    Makes a fetch request with caching in localStorage.

    Returns

    The fetch response (from cache or network).

    Example

    const res = await NetworkUtils.fetchWithCache('/api/data');
    
      • (url, options?, cacheTime?): Promise<Response>
      • Parameters

        • url: string

          The URL to fetch.

        • options: RequestInit = {}

          Fetch options.

        • cacheTime: number = ...

          Cache time in milliseconds (default 5 minutes).

        Returns Promise<Response>

  • fetchJson: (<T>(url, options?) => Promise<T>)

    Makes a fetch request and parses the response as JSON, with error handling.

    Returns

    The parsed response data.

    Throws

    If the response is not OK.

    Example

    const data = await NetworkUtils.fetchJson<{foo: string}>('/api/data');
    
      • <T>(url, options?): Promise<T>
      • Type Parameters

        • T

        Parameters

        • url: string

          The URL to fetch.

        • options: RequestInit = {}

          Fetch options.

        Returns Promise<T>

  • fetchWithProgress: ((url, options?, onProgress?) => Promise<Response>)

    Makes a fetch request and tracks download progress.

    Returns

    The fetch response.

    Example

    await NetworkUtils.fetchWithProgress('/file', {}, (progress) => console.log('Progress:', progress));
    
      • (url, options?, onProgress?): Promise<Response>
      • Parameters

        • url: string

          The URL to fetch.

        • options: RequestInit = {}

          Fetch options.

        • Optional onProgress: ((progress) => void)

          Progress callback (0-100).

            • (progress): void
            • Parameters

              • progress: number

              Returns void

        Returns Promise<Response>

  • fetchWithRetry: ((url, options?, retries?, delay?) => Promise<Response>)

    Makes a fetch request with retry logic on failure.

    Returns

    The fetch response.

    Throws

    If all retries fail.

    Example

    const res = await NetworkUtils.fetchWithRetry('/api', {}, 5, 500);
    
      • (url, options?, retries?, delay?): Promise<Response>
      • Parameters

        • url: string

          The URL to fetch.

        • options: RequestInit = {}

          Fetch options.

        • retries: number = 3

          Number of retries (default 3).

        • delay: number = 1000

          Delay between retries in milliseconds (default 1000).

        Returns Promise<Response>

  • fetchWithTimeout: ((url, options?, timeout?) => Promise<Response>)

    Makes a fetch request with a timeout.

    Returns

    The fetch response.

    Throws

    If the request times out.

    Example

    const res = await NetworkUtils.fetchWithTimeout('/api', {}, 2000);
    
      • (url, options?, timeout?): Promise<Response>
      • Parameters

        • url: string

          The URL to fetch.

        • options: RequestInit = {}

          Fetch options.

        • timeout: number = 5000

          Request timeout in milliseconds (default 5000).

        Returns Promise<Response>

  • getNetworkSpeed: (() => null | string)

    Gets the current network speed (downlink) in Mbps, if available.

    Returns

    The network speed in Mbps, or null if not available.

    Example

    const speed = NetworkUtils.getNetworkSpeed();
    console.log('Speed:', speed);
      • (): null | string
      • Returns null | string

  • getNetworkType: (() => null | string)

    Gets the current network type (e.g., 'wifi', '4g'), if available.

    Returns

    The network type, or null if not available.

    Example

    const type = NetworkUtils.getNetworkType();
    console.log('Type:', type);
      • (): null | string
      • Returns null | string

  • isOnline: (() => boolean)

    Checks if the network is currently online.

    Returns

    True if the network is online, false otherwise.

    Example

    if (NetworkUtils.isOnline()) {
    // Do something when online
    }
      • (): boolean
      • Returns boolean