| 6 | const LastEventId = 'last-event-id'; |
| 7 | |
| 8 | export interface FetchEventSourceInit extends RequestInit { |
| 9 | /** |
| 10 | * The request headers. FetchEventSource only supports the Record<string,string> format. |
| 11 | */ |
| 12 | headers?: Record<string, string>, |
| 13 | |
| 14 | /** |
| 15 | * Called when a response is received. Use this to validate that the response |
| 16 | * actually matches what you expect (and throw if it doesn't.) If not provided, |
| 17 | * will default to a basic validation to ensure the content-type is text/event-stream. |
| 18 | */ |
| 19 | onopen?: (response: Response) => Promise<void>, |
| 20 | |
| 21 | /** |
| 22 | * Called when a message is received. NOTE: Unlike the default browser |
| 23 | * EventSource.onmessage, this callback is called for _all_ events, |
| 24 | * even ones with a custom `event` field. |
| 25 | */ |
| 26 | onmessage?: (ev: EventSourceMessage) => void; |
| 27 | |
| 28 | /** |
| 29 | * Called when a response finishes. If you don't expect the server to kill |
| 30 | * the connection, you can throw an exception here and retry using onerror. |
| 31 | */ |
| 32 | onclose?: () => void; |
| 33 | |
| 34 | /** |
| 35 | * Called when there is any error making the request / processing messages / |
| 36 | * handling callbacks etc. Use this to control the retry strategy: if the |
| 37 | * error is fatal, rethrow the error inside the callback to stop the entire |
| 38 | * operation. Otherwise, you can return an interval (in milliseconds) after |
| 39 | * which the request will automatically retry (with the last-event-id). |
| 40 | * If this callback is not specified, or it returns undefined, fetchEventSource |
| 41 | * will treat every error as retriable and will try again after 1 second. |
| 42 | */ |
| 43 | onerror?: (err: any) => number | null | undefined | void, |
| 44 | |
| 45 | /** |
| 46 | * If true, will keep the request open even if the document is hidden. |
| 47 | * By default, fetchEventSource will close the request and reopen it |
| 48 | * automatically when the document becomes visible again. |
| 49 | */ |
| 50 | openWhenHidden?: boolean; |
| 51 | |
| 52 | /** The Fetch function to use. Defaults to window.fetch */ |
| 53 | fetch?: typeof fetch; |
| 54 | } |
| 55 | |
| 56 | export function fetchEventSource(input: RequestInfo, { |
| 57 | signal: inputSignal, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…