UNPKG

7.85 kBTypeScriptView Raw
1/// <reference path="events.d.ts" />
2
3/**
4 * An extended version of the `Event` emitted by the `EventSource` object when an error occurs.
5 * While the spec does not include any additional properties, we intentionally go beyond the spec
6 * and provide some (minimal) additional information to aid in debugging.
7 *
8 * @public
9 */
10export declare class ErrorEvent extends Event {
11 /**
12 * HTTP status code, if this was triggered by an HTTP error
13 * Note: this is not part of the spec, but is included for better error handling.
14 *
15 * @public
16 */
17 code?: number | undefined
18 /**
19 * Optional message attached to the error.
20 * Note: this is not part of the spec, but is included for better error handling.
21 *
22 * @public
23 */
24 message?: string | undefined
25}
26
27/**
28 * An `EventSource` instance opens a persistent connection to an HTTP server, which sends events
29 * in `text/event-stream` format. The connection remains open until closed by calling `.close()`.
30 *
31 * @public
32 * @example
33 * ```js
34 * const eventSource = new EventSource('https://example.com/stream')
35 * eventSource.addEventListener('error', (error) => {
36 * console.error(error)
37 * })
38 * eventSource.addEventListener('message', (event) => {
39 * console.log('Received message:', event.data)
40 * })
41 * ```
42 */
43declare class EventSource_2 extends EventTarget {
44 #private
45 /**
46 * ReadyState representing an EventSource currently trying to connect
47 *
48 * @public
49 */
50 static CONNECTING: 0
51 /**
52 * ReadyState representing an EventSource connection that is open (eg connected)
53 *
54 * @public
55 */
56 static OPEN: 1
57 /**
58 * ReadyState representing an EventSource connection that is closed (eg disconnected)
59 *
60 * @public
61 */
62 static CLOSED: 2
63 /**
64 * ReadyState representing an EventSource currently trying to connect
65 *
66 * @public
67 */
68 readonly CONNECTING: 0
69 /**
70 * ReadyState representing an EventSource connection that is open (eg connected)
71 *
72 * @public
73 */
74 readonly OPEN: 1
75 /**
76 * ReadyState representing an EventSource connection that is closed (eg disconnected)
77 *
78 * @public
79 */
80 readonly CLOSED: 2
81 /**
82 * Returns the state of this EventSource object's connection. It can have the values described below.
83 *
84 * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState)
85 *
86 * Note: typed as `number` instead of `0 | 1 | 2` for compatibility with the `EventSource` interface,
87 * defined in the TypeScript `dom` library.
88 *
89 * @public
90 */
91 get readyState(): number
92 /**
93 * Returns the URL providing the event stream.
94 *
95 * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url)
96 *
97 * @public
98 */
99 get url(): string
100 /**
101 * Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise.
102 *
103 * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials)
104 */
105 get withCredentials(): boolean
106 /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */
107 get onerror(): ((ev: ErrorEvent) => unknown) | null
108 set onerror(value: ((ev: ErrorEvent) => unknown) | null)
109 /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */
110 get onmessage(): ((ev: MessageEvent) => unknown) | null
111 set onmessage(value: ((ev: MessageEvent) => unknown) | null)
112 /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */
113 get onopen(): ((ev: Event) => unknown) | null
114 set onopen(value: ((ev: Event) => unknown) | null)
115 addEventListener<K extends keyof EventSourceEventMap>(
116 type: K,
117 listener: (this: EventSource_2, ev: EventSourceEventMap[K]) => unknown,
118 options?: boolean | AddEventListenerOptions,
119 ): void
120 addEventListener(
121 type: string,
122 listener: (this: EventSource_2, event: MessageEvent) => unknown,
123 options?: boolean | AddEventListenerOptions,
124 ): void
125 addEventListener(
126 type: string,
127 listener: EventListenerOrEventListenerObject,
128 options?: boolean | AddEventListenerOptions,
129 ): void
130 removeEventListener<K extends keyof EventSourceEventMap>(
131 type: K,
132 listener: (this: EventSource_2, ev: EventSourceEventMap[K]) => unknown,
133 options?: boolean | EventListenerOptions,
134 ): void
135 removeEventListener(
136 type: string,
137 listener: (this: EventSource_2, event: MessageEvent) => unknown,
138 options?: boolean | EventListenerOptions,
139 ): void
140 removeEventListener(
141 type: string,
142 listener: EventListenerOrEventListenerObject,
143 options?: boolean | EventListenerOptions,
144 ): void
145 constructor(url: string | URL, eventSourceInitDict?: EventSourceInit)
146 /**
147 * Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED.
148 *
149 * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close)
150 *
151 * @public
152 */
153 close(): void
154}
155export {EventSource_2 as EventSource}
156
157/**
158 * Mirrors the official DOM typings, with the exception of the extended ErrorEvent.
159 *
160 * @public
161 */
162export declare interface EventSourceEventMap {
163 error: ErrorEvent
164 message: MessageEvent
165 open: Event
166}
167
168/**
169 * Mirrors the official DOM typings (for the most part)
170 *
171 * @public
172 */
173export declare interface EventSourceInit {
174 /**
175 * A boolean value, defaulting to `false`, indicating if CORS should be set to `include` credentials.
176 */
177 withCredentials?: boolean
178 /**
179 * Optional fetch implementation to use. Defaults to `globalThis.fetch`.
180 * Can also be used for advanced use cases like mocking, proxying, custom certs etc.
181 */
182 fetch?: FetchLike
183}
184
185/**
186 * Stripped down version of `fetch()`, only defining the parts we care about.
187 * This ensures it should work with "most" fetch implementations.
188 *
189 * @public
190 */
191export declare type FetchLike = (
192 url: string | URL,
193 init?: FetchLikeInit,
194) => Promise<FetchLikeResponse>
195
196/**
197 * Stripped down version of `RequestInit`, only defining the parts we care about.
198 *
199 * @public
200 */
201export declare interface FetchLikeInit {
202 /** An AbortSignal to set request's signal. Typed as `any` because of polyfill inconsistencies. */
203 signal?:
204 | {
205 aborted: boolean
206 }
207 | any
208 /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */
209 headers?: Record<string, string>
210 /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */
211 mode?: 'cors' | 'no-cors' | 'same-origin'
212 /** A string indicating whether credentials will be sent with the request always, never, or only when sent to a same-origin URL. Sets request's credentials. */
213 credentials?: 'include' | 'omit' | 'same-origin'
214 /** Controls how the request is cached. */
215 cache?: 'no-store'
216 /** A string indicating whether request follows redirects, results in an error upon encountering a redirect, or returns the redirect (in an opaque fashion). Sets request's redirect. */
217 redirect?: 'error' | 'follow' | 'manual'
218}
219
220/**
221 * Minimal version of the `Response` type returned by `fetch()`.
222 *
223 * @public
224 */
225export declare interface FetchLikeResponse {
226 readonly body:
227 | {
228 getReader(): ReaderLike
229 }
230 | Response['body']
231 | null
232 readonly url: string
233 readonly status: number
234 readonly redirected: boolean
235 readonly headers: {
236 get(name: string): string | null
237 }
238}
239
240/**
241 * Stripped down version of `ReadableStreamDefaultReader`, only defining the parts we care about.
242 *
243 * @public
244 */
245export declare interface ReaderLike {
246 read(): Promise<
247 | {
248 done: false
249 value: unknown
250 }
251 | {
252 done: true
253 value?: undefined
254 }
255 >
256 cancel(): Promise<void>
257}
258
259export {}
260
\No newline at end of file