import { AlovaGenerics, Method } from 'alova'; import { FetchRequestInit } from 'alova/fetch'; import { AlovaEvent, AlovaMethodHandler, ExportedState } from '../general'; type SSEHookReadyState = 0 | 1 | 2; export interface AlovaSSEEvent extends AlovaEvent { method: Method; eventSource: EventSource; // Event source instance } export interface AlovaSSEErrorEvent extends AlovaSSEEvent { error: Error; // error object } export interface AlovaSSEMessageEvent extends AlovaSSEEvent { data: Data; // Data converted by the interceptor for each response } export type SSEOnOpenTrigger = ( event: AlovaSSEEvent ) => void; export type SSEOnMessageTrigger = ( event: AlovaSSEMessageEvent ) => void; export type SSEOnErrorTrigger = ( event: AlovaSSEErrorEvent ) => void; export interface EventSourceFetchInit extends RequestInit {} /** * useSSE() configuration item */ export type SSEHookConfig = FetchRequestInit & { /** * Whether to include credentials in the request */ withCredentials?: boolean; /** * Reconnection time in milliseconds, default is 1000 * set to 0 to disable reconnection */ reconnectionTime?: number; /** * Whether to pass the responded interception of the alova instance * @default true */ interceptByGlobalResponded?: boolean; /** * initial data */ initialData?: any; /** * Whether to initiate a request immediately * @default false */ immediate?: boolean; /** * Whether to interrupt the previous request and trigger this request * @default true * TODO does not currently support specifying */ abortLast?: true; /** * set the message data type. * @default "text" */ responseType?: 'text' | 'json'; }; /** * useSSE() return type */ export interface SSEExposure { readyState: ExportedState; data: ExportedState; eventSource: ExportedState; /** * Make the request manually. This method is automatically triggered when using `immediate: true` * @param args Request parameters will be passed to method */ send(...args: [...Args, ...any[]]): Promise; /** * close connection */ close(): void; /** * Register the callback function of EventSource open * @param callback callback function * @returns Unregister function */ onOpen(callback: SSEOnOpenTrigger): this; /** * Register the callback function for EventSource message * @param callback callback function * @returns Unregister function */ onMessage(callback: SSEOnMessageTrigger): this; /** * Register the callback function for EventSource error * @param callback callback function * @returns Unregister function */ onError(callback: SSEOnErrorTrigger): this; /** * @param eventName Event name, default exists `open` | `error` | `message` * @param handler event handler */ on(eventName: string, handler: (event: AlovaSSEMessageEvent) => void): this; } /** * useSSE * Send requests using Server-sent events * * * @param handler methodget function * @param config Configuration parameters * @return useSSE related data and operation functions */ export declare function useSSE( handler: Method | AlovaMethodHandler, config?: SSEHookConfig ): SSEExposure;