asyncsse
Version:
Fetch Server-Sent Events (SSE) as an async iterable
34 lines (33 loc) • 1.34 kB
TypeScript
/**
* asyncSSE yields events when streaming from an SSE endpoint.
*
* @param {string | Request} request - The URL or Request object for the SSE endpoint.
* @param {RequestInit} options - Optional RequestInit object to configure the fetch request.
* @param {SSEConfig} config - Optional configuration object for the SSE stream.
* @returns {AsyncGenerator<SSEEvent, void, unknown>} An AsyncGenerator that yields SSEEvent objects.
*
* @example
* for await (const event of asyncSSE("https://example.com/stream", { method: "POST" })) {
* console.log(event);
* }
*/
export declare function asyncSSE(request: string | Request, options?: RequestInit, config?: SSEConfig): AsyncGenerator<SSEEvent, void, unknown>;
/**
* Represents an event object returned by the SSE stream.
* This interface allows for any string key-value pairs, with some common SSE fields explicitly defined.
*/
export interface SSEEvent {
data?: string;
event?: string;
error?: string;
[key: string]: string | undefined;
}
/**
* Configuration options for the SSE stream
*/
export interface SSEConfig {
/** Custom fetch implementation. Defaults to global fetch if not provided. */
fetch?: typeof fetch;
/** Callback function called with the Response object after successful fetch */
onResponse?: (response: Response) => any;
}