Const
Function to remove the listener.
const remove = NetworkUtils.addNetworkStatusListener((online) => console.log('Online:', online));
// ...later
remove();
Callback invoked with true
if online, false
if offline.
Makes a fetch request with Bearer token authentication.
The fetch response.
const res = await NetworkUtils.fetchWithAuth('/api', 'token123');
The URL to fetch.
The authentication token.
Additional fetch options.
Makes a fetch request with caching in localStorage.
The fetch response (from cache or network).
const res = await NetworkUtils.fetchWithCache('/api/data');
The URL to fetch.
Fetch options.
Cache time in milliseconds (default 5 minutes).
Makes a fetch request and parses the response as JSON, with error handling.
The parsed response data.
If the response is not OK.
const data = await NetworkUtils.fetchJson<{foo: string}>('/api/data');
Makes a fetch request and tracks download progress.
The fetch response.
await NetworkUtils.fetchWithProgress('/file', {}, (progress) => console.log('Progress:', progress));
The URL to fetch.
Fetch options.
Optional
onProgress: ((progress) => void)Progress callback (0-100).
Makes a fetch request with retry logic on failure.
The fetch response.
If all retries fail.
const res = await NetworkUtils.fetchWithRetry('/api', {}, 5, 500);
The URL to fetch.
Fetch options.
Number of retries (default 3).
Delay between retries in milliseconds (default 1000).
Makes a fetch request with a timeout.
The fetch response.
If the request times out.
const res = await NetworkUtils.fetchWithTimeout('/api', {}, 2000);
The URL to fetch.
Fetch options.
Request timeout in milliseconds (default 5000).
Gets the current network speed (downlink) in Mbps, if available.
The network speed in Mbps, or null if not available.
const speed = NetworkUtils.getNetworkSpeed();
console.log('Speed:', speed);
Gets the current network type (e.g., 'wifi', '4g'), if available.
The network type, or null if not available.
const type = NetworkUtils.getNetworkType();
console.log('Type:', type);
Checks if the network is currently online.
True if the network is online, false otherwise.
if (NetworkUtils.isOnline()) {
// Do something when online
}
Adds a listener for network status changes (online/offline).