/** * @youranalytics/web-sdk Network Communication Layer * Handles sending events to the analytics server with fallbacks and offline support */ import type { EventBatch, RawEvent } from './types'; /** * Enable or disable debug logging. * @param enabled - Whether to enable debug mode */ export declare function setDebugMode(enabled: boolean): void; /** * Custom error class for network-related errors. * Includes status code and retry information. */ export declare class NetworkError extends Error { readonly statusCode?: number | undefined; readonly retry?: boolean | undefined; constructor(message: string, statusCode?: number | undefined, retry?: boolean | undefined); } /** Send data using navigator.sendBeacon (non-blocking, survives page unload). */ export declare function sendBeacon(url: string, data: EventBatch): boolean; /** Send data using fetch API with optional Authorization header. */ export declare function sendFetch(url: string, data: EventBatch, apiKey?: string): Promise; /** * Wait for specified milliseconds. */ export declare function delay(ms: number): Promise; /** Send with exponential backoff retry (1s, 2s, 4s). */ export declare function sendWithRetry(url: string, data: EventBatch, maxRetries?: number, apiKey?: string): Promise; /** Compress JSON payload using GZIP via CompressionStream. */ export declare function compressData(data: string): Promise; /** Send compressed data using fetch. Falls back to uncompressed. */ export declare function sendCompressed(url: string, data: EventBatch, apiKey?: string): Promise; /** Store events in localStorage when offline. */ export declare function queueOfflineEvents(events: RawEvent[]): void; /** Retrieve queued offline events from localStorage. */ export declare function getOfflineEvents(): RawEvent[]; /** Clear offline queue. */ export declare function clearOfflineEvents(): void; /** Check if browser is currently offline. */ export declare function isOffline(): boolean; /** Register callback for when browser comes back online. Returns cleanup function. */ export declare function onOnline(callback: () => void): () => void; /** * Generate unique request ID. * Format: timestamp_random * Used to prevent duplicate sends. * * @returns Unique request ID */ export declare function generateRequestId(): string; /** * Check if request was already sent. * Checks sent IDs in sessionStorage. * * @param requestId - Request ID to check * @returns true if request was already sent */ export declare function wasRequestSent(requestId: string): boolean; /** * Mark request as sent. * Stores in sessionStorage. Keeps last MAX_TRACKED_REQUESTS IDs. * * @param requestId - Request ID to mark as sent */ export declare function markRequestSent(requestId: string): void; /** * Clear all tracked request IDs. * Useful for testing. */ export declare function clearSentRequests(): void; /** * Send events to the server using the best available method. * Handles offline state, deduplication, and fallbacks. * * @param url - Endpoint URL * @param data - Event batch to send * @param options - Send options * @returns Promise that resolves when sent (or queued if offline) */ export declare function send(url: string, data: EventBatch, options?: { useBeacon?: boolean; compress?: boolean; retry?: boolean; requestId?: string; apiKey?: string; }): Promise; export declare const _testing: { OFFLINE_QUEUE_KEY: string; SENT_REQUESTS_KEY: string; MAX_OFFLINE_EVENTS: number; MAX_TRACKED_REQUESTS: number; MAX_BEACON_PAYLOAD: number; BASE_RETRY_DELAY: number; }; //# sourceMappingURL=network.d.ts.map