UNPKG

eventsource

Version:

WhatWG/W3C compliant EventSource client for Node.js and browsers

128 lines 5.57 kB
import { ErrorEvent } from './errors.ts'; import type { AddEventListenerOptions, EventListenerOptions, EventListenerOrEventListenerObject, EventSourceEventMap, EventSourceInit } from './types.ts'; /** * An `EventSource` instance opens a persistent connection to an HTTP server, which sends events * in `text/event-stream` format. The connection remains open until closed by calling `.close()`. * * Deliberately an `interface` and not a `class`: it lets consumers pass the native `EventSource`, * a mock, or any other implementation wherever this type is expected. * * @public * @example * ```js * const eventSource = new EventSource('https://example.com/stream') * eventSource.addEventListener('error', (error) => { * console.error(error) * }) * eventSource.addEventListener('message', (event) => { * console.log('Received message:', event.data) * }) * ``` */ export interface EventSource extends EventTarget { /** * ReadyState representing an EventSource currently trying to connect * * @public */ readonly CONNECTING: 0; /** * ReadyState representing an EventSource connection that is open (eg connected) * * @public */ readonly OPEN: 1; /** * ReadyState representing an EventSource connection that is closed (eg disconnected) * * @public */ readonly CLOSED: 2; /** * Returns the state of this EventSource object's connection. It can have the values described below. * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState) * * Note: typed as `number` instead of `0 | 1 | 2` for compatibility with the `EventSource` interface, * defined in the TypeScript `dom` library. * * @public */ readonly readyState: number; /** * Returns the URL providing the event stream. * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url) * * @public */ readonly url: string; /** * Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise. * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials) * * @public */ readonly withCredentials: boolean; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */ onerror: ((this: EventSource, ev: ErrorEvent) => unknown) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */ onmessage: ((this: EventSource, ev: MessageEvent) => unknown) | null; /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */ onopen: ((this: EventSource, ev: Event) => unknown) | null; addEventListener<K extends keyof EventSourceEventMap>(type: K, listener: (this: EventSource, ev: EventSourceEventMap[K]) => unknown, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: (this: EventSource, event: MessageEvent) => unknown, options?: boolean | AddEventListenerOptions): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void; removeEventListener<K extends keyof EventSourceEventMap>(type: K, listener: (this: EventSource, ev: EventSourceEventMap[K]) => unknown, options?: boolean | EventListenerOptions): void; removeEventListener(type: string, listener: (this: EventSource, event: MessageEvent) => unknown, options?: boolean | EventListenerOptions): void; removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void; /** * Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED. * * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close) * * @public */ close(): void; } /** * The `EventSource` constructor. Mirrors the shape of the native one, so the two can be used * interchangeably - eg for functions that take an implementation to construct. * * @public */ export interface EventSourceConstructor { readonly prototype: EventSource; /** * Constructs a new `EventSource` instance, which immediately starts connecting. * * @param url - The URL to connect to * @param eventSourceInitDict - Options for the connection */ new (url: string | URL, eventSourceInitDict?: EventSourceInit): EventSource; /** ReadyState representing an EventSource currently trying to connect */ readonly CONNECTING: 0; /** ReadyState representing an EventSource connection that is open (eg connected) */ readonly OPEN: 1; /** ReadyState representing an EventSource connection that is closed (eg disconnected) */ readonly CLOSED: 2; } /** * An `EventSource` instance opens a persistent connection to an HTTP server, which sends events * in `text/event-stream` format. The connection remains open until closed by calling `.close()`. * * @public * @example * ```js * const eventSource = new EventSource('https://example.com/stream') * eventSource.addEventListener('error', (error) => { * console.error(error) * }) * eventSource.addEventListener('message', (event) => { * console.log('Received message:', event.data) * }) * ``` */ export declare const EventSource: EventSourceConstructor; //# sourceMappingURL=EventSource.d.ts.map