//#region src/subscribe/index.d.ts /** * GraphQL Subscription Client * Framework-agnostic subscription client supporting WebSocket and SSE transports * * Uses native browser WebSocket API and EventSource for maximum compatibility. * * @example WebSocket (default) * ```typescript * import { createSubscriptionClient } from 'nitro-graphql/subscribe' * * const client = createSubscriptionClient({ wsEndpoint: '/api/graphql/ws' }) * * // Simple subscription * client.subscribe( * 'subscription { countdown(from: 10) }', * {}, * (data) => console.log(data), * (error) => console.error(error) * ) * * // Multiplexed session (multiple subscriptions, single connection) * const session = client.createSession() * session.subscribe(query1, vars1, onData1) * session.subscribe(query2, vars2, onData2) * ``` * * @example SSE Transport (same API, different transport) * ```typescript * // Use SSE when WebSocket is blocked (corporate firewalls, etc.) * client.subscribe( * 'subscription { countdown(from: 10) }', * {}, * (data) => console.log(data), * (error) => console.error(error), * { transport: 'sse' } * ) * ``` * * @example Auto Transport (WebSocket first, SSE fallback) * ```typescript * // Automatically fallback to SSE if WebSocket fails * client.subscribe( * 'subscription { countdown(from: 10) }', * {}, * (data) => console.log(data), * (error) => console.error(error), * { transport: 'auto' } * ) * ``` * * @module nitro-graphql/subscribe */ type ConnectionState = 'idle' | 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'error'; /** Transport type for subscriptions */ type SubscriptionTransport = 'websocket' | 'sse' | 'auto'; /** Transport options for subscribe calls */ interface TransportOptions { /** Transport type: 'websocket' (default), 'sse', or 'auto' (WS first, SSE fallback) */ transport?: SubscriptionTransport; } interface SubscriptionOptions> { query: string; variables?: TVariables; onData?: (data: unknown) => void; onError?: (error: Error) => void; onConnected?: () => void; onReconnected?: () => void; onDisconnected?: () => void; onRetrying?: (attempt: number, maxAttempts: number) => void; onMaxRetriesReached?: () => void; onStateChange?: (state: ConnectionState) => void; maxRetries?: number; connectionTimeoutMs?: number; connectionParams?: Record; } interface SubscriptionHandle { unsubscribe: () => void; readonly isConnected: boolean; readonly state: ConnectionState; readonly id: string; /** The active transport type */ readonly transport: 'websocket' | 'sse'; } type StateChangeCallback = (state: ConnectionState, subscriptionCount: number) => void; interface SubscriptionSession { subscribe: >(query: string, variables?: TVariables, onData?: (data: TData) => void, onError?: (error: Error) => void) => SubscriptionHandle; readonly state: ConnectionState; readonly isConnected: boolean; readonly subscriptionCount: number; close: () => void; onStateChange: (callback: StateChangeCallback) => () => void; } interface SubscriptionClientConfig { /** WebSocket endpoint (default: '/api/graphql/ws') */ wsEndpoint?: string; /** SSE endpoint for SSE transport (default: '/api/graphql') */ sseEndpoint?: string; /** Connection parameters for WebSocket handshake */ connectionParams?: Record | (() => Record | Promise>); /** Connection timeout in ms (default: 10000) */ connectionTimeoutMs?: number; /** Maximum retry attempts (default: 5) */ maxRetries?: number; } interface SubscriptionClient { subscribe: >(query: string, variables?: TVariables, onData?: (data: TData) => void, onError?: (error: Error) => void, transportOptions?: TransportOptions) => SubscriptionHandle; subscribeAsync: <_TData = unknown, TVariables = Record>(options: SubscriptionOptions, transportOptions?: TransportOptions) => Promise; createSession: () => SubscriptionSession; } declare function createSubscriptionClient(config?: SubscriptionClientConfig): SubscriptionClient; interface SseSubscriptionOptions> { query: string; variables?: TVariables; onData?: (data: unknown) => void; onError?: (error: Error) => void; onConnected?: () => void; onReconnected?: () => void; onDisconnected?: () => void; onStateChange?: (state: ConnectionState) => void; } interface SseSubscriptionHandle { close: () => void; } /** * Create an SSE subscription using native EventSource (legacy API) * Use this when WebSocket is blocked (corporate firewalls, etc.) * * @deprecated Use the unified client with { transport: 'sse' } instead * * @example * ```typescript * const handle = createSseSubscription('/api/graphql', { * query: 'subscription { countdown(from: 10) }', * onData: (data) => console.log(data), * }) * * // Later... * handle.close() * ``` */ declare function createSseSubscription>(endpoint: string, options: SseSubscriptionOptions): SseSubscriptionHandle; //#endregion export { ConnectionState, SseSubscriptionHandle, SseSubscriptionOptions, StateChangeCallback, SubscriptionClient, SubscriptionClientConfig, SubscriptionHandle, SubscriptionOptions, SubscriptionSession, SubscriptionTransport, TransportOptions, createSseSubscription, createSubscriptionClient };