UNPKG

@supunlakmal/hooks

Version:

A collection of reusable React hooks

30 lines (29 loc) 1.29 kB
/** * Represents the state of the EventSource connection. */ export type EventSourceStatus = 'connecting' | 'open' | 'closed' | 'error'; interface UseEventSourceOptions { /** Whether to include credentials (cookies, authorization headers) with the request. */ withCredentials?: boolean; /** Optional event listeners to register for specific event types. */ eventListeners?: Record<string, (event: MessageEvent) => void>; } interface UseEventSourceReturn<T = any> { /** The current status of the EventSource connection. */ status: EventSourceStatus; /** The last received message data (parsed if possible, otherwise raw). */ lastMessage: MessageEvent<T> | null; /** Any error that occurred with the connection. */ error: Event | null; /** Function to explicitly close the EventSource connection. */ close: () => void; } /** * Custom hook to connect to a Server-Sent Events (SSE) endpoint. * * @param url The URL of the SSE endpoint. * @param options Configuration options for the connection. * @returns An object with connection status, last message, error, and close function. */ export declare function useEventSource<T = any>(url: string | null | undefined, options?: UseEventSourceOptions): UseEventSourceReturn<T>; export {};