All files / hooks/use-network-state/src types.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116                                                                                                                                                                                                                                       
/**
 * The effective connection type, as reported by the Network Information API's
 * `NetworkInformation.effectiveType`. Describes the measured round-trip time and
 * downlink to bucket the connection into a cellular-equivalent quality.
 *
 * `undefined` when the Network Information API is unsupported (Firefox, Safari).
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/effectiveType
 */
export type EffectiveConnectionType = "slow-2g" | "2g" | "3g" | "4g";
 
/**
 * The physical connection type, as reported by the Network Information API's
 * `NetworkInformation.type`.
 *
 * `undefined` when the Network Information API is unsupported, or when the
 * browser exposes `effectiveType`/`downlink` but not `type` (common on desktop
 * Chromium, which omits `type` for privacy reasons).
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation/type
 */
export type ConnectionType =
  | "bluetooth"
  | "cellular"
  | "ethernet"
  | "none"
  | "wifi"
  | "wimax"
  | "other"
  | "unknown";
 
/**
 * A snapshot of the device's network status, combining `navigator.onLine` with
 * the Network Information API (`navigator.connection`).
 *
 * Only `online` is guaranteed on every platform. Every Network Information
 * field is optional and is `undefined` wherever the API (or that specific
 * field) is unsupported — the hook never throws and always reports `online`.
 */
export interface NetworkState {
  /**
   * Whether the browser currently has network connectivity, from
   * `navigator.onLine`. Updated on the window `online`/`offline` events.
   *
   * Defaults to `true` on the server (SSR) and in environments without a
   * `navigator`, matching the optimistic assumption most apps want.
   */
  online: boolean;
 
  /**
   * Timestamp of the last `online`/`offline` transition observed by this hook.
   *
   * `undefined` on the initial render and until the first transition occurs —
   * it reflects *when connectivity changed*, not when the hook mounted.
   */
  since?: Date;
 
  /**
   * Effective downlink bandwidth estimate in megabits per second, rounded to
   * the nearest 25 kbps. `undefined` when unsupported.
   */
  downlink?: number;
 
  /**
   * Maximum downlink speed of the underlying connection technology, in
   * megabits per second. Rarely populated in practice; `undefined` when
   * unsupported.
   */
  downlinkMax?: number;
 
  /**
   * The effective connection type (`"slow-2g" | "2g" | "3g" | "4g"`).
   * `undefined` when unsupported.
   */
  effectiveType?: EffectiveConnectionType;
 
  /**
   * Estimated effective round-trip time in milliseconds, rounded to the nearest
   * 25 ms. `undefined` when unsupported.
   */
  rtt?: number;
 
  /**
   * Whether the user has requested a reduced-data mode ("Data Saver").
   * `undefined` when unsupported.
   */
  saveData?: boolean;
 
  /**
   * The physical connection type (`"wifi" | "cellular" | ...`).
   * `undefined` when unsupported.
   */
  type?: ConnectionType;
}
 
/**
 * The value returned by {@link useNetworkState}: the current {@link NetworkState}.
 */
export type UseNetworkStateReturn = NetworkState;
 
/**
 * The subset of the `NetworkInformation` interface this hook reads, plus the
 * `EventTarget` methods used to subscribe to its `change` event. Vendor-prefixed
 * (`mozConnection`, `webkitConnection`) implementations expose the same shape.
 *
 * @see https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
 */
export interface NetworkInformationLike extends Partial<EventTarget> {
  readonly downlink?: number;
  readonly downlinkMax?: number;
  readonly effectiveType?: EffectiveConnectionType;
  readonly rtt?: number;
  readonly saveData?: boolean;
  readonly type?: ConnectionType;
}