@laravel/stream-react
Version:
Laravel streaming hooks for React
68 lines (61 loc) • 2.22 kB
TypeScript
declare type EventStreamOptions = {
eventName?: string | string[];
endSignal?: string;
glue?: string;
replace?: boolean;
onMessage?: (event: MessageEvent) => void;
onComplete?: () => void;
onError?: (error: Event) => void;
};
declare type EventStreamResult = {
message: string;
messageParts: string[];
close: (resetMessage?: boolean) => void;
clearMessage: () => void;
};
declare type StreamOptions<TSendBody extends Record<string, any> = {}> = {
id?: string;
initialInput?: TSendBody;
headers?: Record<string, string>;
csrfToken?: string;
json?: boolean;
credentials?: RequestCredentials;
onResponse?: (response: Response) => void;
onData?: (data: string) => void;
onCancel?: () => void;
onFinish?: () => void;
onError?: (error: Error) => void;
onBeforeSend?: (request: RequestInit) => RequestInit | boolean | void;
};
/**
* Hook for handling server-sent event (SSE) streams
*
* @param url - The URL to connect to for the EventSource
* @param options - Options for the stream
*
* @link https://laravel.com/docs/responses#event-streams
*
* @returns StreamResult object containing the accumulated response, close, and reset functions
*/
export declare const useEventStream: (url: string, { eventName, endSignal, glue, replace, onMessage, onComplete, onError, }?: EventStreamOptions) => EventStreamResult;
export declare const useJsonStream: <TJsonData = null, TSendBody extends Record<string, any> = {}>(url: string, options?: Omit<StreamOptions<TSendBody>, "json">) => {
isFetching: boolean;
isStreaming: boolean;
id: string;
send: (body: TSendBody) => void;
cancel: () => void;
clearData: () => void;
data: TJsonData | null;
strData: string;
};
export declare const useStream: <TSendBody extends Record<string, any> = {}, TJsonData = null>(url: string, options?: StreamOptions<TSendBody>) => {
data: string;
jsonData: TJsonData | null;
isFetching: boolean;
isStreaming: boolean;
id: string;
send: (body: TSendBody) => void;
cancel: () => void;
clearData: () => void;
};
export { }