/** * Request utilities for vTilt tracking * * Handles HTTP requests with: * - GZip compression (via fflate) * - Multiple transport methods (fetch, XHR, sendBeacon) * - Automatic fallback between transports * * Compression transport split (important — see `utils/base64.ts`): * * - fetch / XHR → binary `Blob` of gzip bytes, body uses `compression=gzip-js` * - sendBeacon → base64 *text* of the same gzip bytes, body uses * `compression=base64` * * `sendBeacon` with a binary `Blob` is unreliable on `pagehide` / tab * discard: the browser queues the beacon synchronously but serializes the * body *after* JS is torn down, and the underlying `ArrayBuffer` can be * detached/GC'd by then. The wire body lands empty or truncated while the * URL we already committed still carries `compression=gzip-js`, and the * server's `gunzipSync` throws `Z_BUF_ERROR`. Switching the beacon to a * base64 string body means `Blob([string])` owns its own UTF-8 copy, which * survives unload reliably. * * Based on PostHog's request.ts pattern. */ import { Compression, jsonStringify } from "@v-tilt/core"; /** * Shared with the Node SDK via `@v-tilt/core` so both SDKs stay wire-compatible * with the ingestion endpoint. Re-exported here because the browser code (and * tests) historically import `Compression` / `jsonStringify` from `../request`. */ export { Compression, jsonStringify }; export interface RequestResponse { statusCode: number; text?: string; json?: any; } export interface RequestOptions { url: string; data?: any; method?: "POST" | "GET"; headers?: Record; transport?: "XHR" | "fetch" | "sendBeacon"; compression?: Compression; timeout?: number; callback?: (response: RequestResponse) => void; /** * Project API token. When set, the helper authenticates the request: * * - `fetch` / `XHR` transports → sent as an `x-api-key` header so the * URL stays clean and is less likely to match ad/privacy blocker * filter rules that look for `?token=` on known analytics origins. * - `sendBeacon` transport → appended as `?token=` because * beacon requests cannot carry custom headers. * * The server accepts both (`x-api-key` is checked before the query * parameter), so older SDK versions that still embed the token in the * URL continue to work unchanged. */ projectToken?: string; } export declare const request: (options: RequestOptions) => void; export declare const requestAsync: (options: Omit) => Promise; export declare const shouldCompress: (data: any) => boolean;