preact-missing-hooks
Version:
A lightweight, extendable collection of missing React-like hooks for Preact — plus fresh, powerful new ones designed specifically for modern Preact apps.
38 lines (37 loc) • 1.58 kB
TypeScript
/**
* useWebRTCIP – detect local/public IPs via WebRTC ICE candidates and STUN.
* Not highly reliable; use as first-priority hint and fall back to a public IP API (e.g. ipapi.co) if needed.
* @module useWebRTCIP
*/
export interface UseWebRTCIPOptions {
/** STUN server URLs (default: Google STUN). */
stunServers?: string[];
/** Stop gathering after this many ms (default: 3000). */
timeout?: number;
/** Called once per newly detected IP (no duplicates). */
onDetect?: (ip: string) => void;
}
export interface UseWebRTCIPReturn {
/** Unique IPv4 addresses found from ICE candidates. */
ips: string[];
/** True while ICE gathering is in progress. */
loading: boolean;
/** Error message if WebRTC is unavailable or detection fails. */
error: string | null;
}
/**
* Attempts to detect client IP addresses using WebRTC ICE candidates and a STUN server.
* Works frontend-only (no backend). Not guaranteed to return a public IP; use as a hint and
* fall back to a public IP API (e.g. ipapi.co, ip-api.com) if you need reliability.
*
* @param options - Optional: stunServers, timeout (ms), onDetect(ip) callback.
* @returns { ips, loading, error } – unique IPv4s, loading flag, and error message.
*
* @example
* const { ips, loading, error } = useWebRTCIP({
* timeout: 5000,
* onDetect: (ip) => console.log('Detected:', ip),
* })
* // If ips is empty and error is set, fall back to: fetch('https://api.ipify.org?format=json')
*/
export declare function useWebRTCIP(options?: UseWebRTCIPOptions): UseWebRTCIPReturn;