index.d.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /**
  2. * Mirrors the official DOM typings (sorta).
  3. *
  4. * @public
  5. */
  6. export declare interface AddEventListenerOptions extends EventListenerOptions {
  7. /** When `true`, the listener is automatically removed when it is first invoked. Default: `false`. */
  8. once?: boolean
  9. /** When `true`, serves as a hint that the listener will not call the `Event` object's `preventDefault()` method. Default: false. */
  10. passive?: boolean
  11. /** The listener will be removed when the given AbortSignal object's `abort()` method is called. */
  12. signal?: AbortSignal
  13. }
  14. /**
  15. * An extended version of the `Event` emitted by the `EventSource` object when an error occurs.
  16. * While the spec does not include any additional properties, we intentionally go beyond the spec
  17. * and provide some (minimal) additional information to aid in debugging.
  18. *
  19. * @public
  20. */
  21. export declare class ErrorEvent extends Event {
  22. /**
  23. * HTTP status code, if this was triggered by an HTTP error
  24. * Note: this is not part of the spec, but is included for better error handling.
  25. *
  26. * @public
  27. */
  28. code?: number | undefined
  29. /**
  30. * Optional message attached to the error.
  31. * Note: this is not part of the spec, but is included for better error handling.
  32. *
  33. * @public
  34. */
  35. message?: string | undefined
  36. /**
  37. * Constructs a new `ErrorEvent` instance. This is typically not called directly,
  38. * but rather emitted by the `EventSource` object when an error occurs.
  39. *
  40. * @param type - The type of the event (should be "error")
  41. * @param errorEventInitDict - Optional properties to include in the error event
  42. */
  43. constructor(
  44. type: string,
  45. errorEventInitDict?: {
  46. message?: string | undefined
  47. code?: number | undefined
  48. },
  49. )
  50. }
  51. /**
  52. * Mirrors the official DOM typings.
  53. *
  54. * @public
  55. */
  56. export declare interface EventListener {
  57. (evt: Event | MessageEvent): void
  58. }
  59. /**
  60. * Mirrors the official DOM typings.
  61. *
  62. * @public
  63. */
  64. export declare interface EventListenerObject {
  65. handleEvent(object: Event): void
  66. }
  67. /**
  68. * Mirrors the official DOM typings (sorta).
  69. *
  70. * @public
  71. */
  72. export declare interface EventListenerOptions {
  73. /** Not directly used by Node.js. Added for API completeness. Default: `false`. */
  74. capture?: boolean
  75. }
  76. /**
  77. * Mirrors the official DOM typings.
  78. *
  79. * @public
  80. */
  81. export declare type EventListenerOrEventListenerObject = EventListener | EventListenerObject
  82. /**
  83. * An `EventSource` instance opens a persistent connection to an HTTP server, which sends events
  84. * in `text/event-stream` format. The connection remains open until closed by calling `.close()`.
  85. *
  86. * @public
  87. * @example
  88. * ```js
  89. * const eventSource = new EventSource('https://example.com/stream')
  90. * eventSource.addEventListener('error', (error) => {
  91. * console.error(error)
  92. * })
  93. * eventSource.addEventListener('message', (event) => {
  94. * console.log('Received message:', event.data)
  95. * })
  96. * ```
  97. */
  98. declare class EventSource_2 extends EventTarget {
  99. #private
  100. /**
  101. * ReadyState representing an EventSource currently trying to connect
  102. *
  103. * @public
  104. */
  105. static CONNECTING: 0
  106. /**
  107. * ReadyState representing an EventSource connection that is open (eg connected)
  108. *
  109. * @public
  110. */
  111. static OPEN: 1
  112. /**
  113. * ReadyState representing an EventSource connection that is closed (eg disconnected)
  114. *
  115. * @public
  116. */
  117. static CLOSED: 2
  118. /**
  119. * ReadyState representing an EventSource currently trying to connect
  120. *
  121. * @public
  122. */
  123. readonly CONNECTING: 0
  124. /**
  125. * ReadyState representing an EventSource connection that is open (eg connected)
  126. *
  127. * @public
  128. */
  129. readonly OPEN: 1
  130. /**
  131. * ReadyState representing an EventSource connection that is closed (eg disconnected)
  132. *
  133. * @public
  134. */
  135. readonly CLOSED: 2
  136. /**
  137. * Returns the state of this EventSource object's connection. It can have the values described below.
  138. *
  139. * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/readyState)
  140. *
  141. * Note: typed as `number` instead of `0 | 1 | 2` for compatibility with the `EventSource` interface,
  142. * defined in the TypeScript `dom` library.
  143. *
  144. * @public
  145. */
  146. get readyState(): number
  147. /**
  148. * Returns the URL providing the event stream.
  149. *
  150. * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/url)
  151. *
  152. * @public
  153. */
  154. get url(): string
  155. /**
  156. * Returns true if the credentials mode for connection requests to the URL providing the event stream is set to "include", and false otherwise.
  157. *
  158. * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/withCredentials)
  159. */
  160. get withCredentials(): boolean
  161. /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/error_event) */
  162. get onerror(): ((ev: ErrorEvent) => unknown) | null
  163. set onerror(value: ((ev: ErrorEvent) => unknown) | null)
  164. /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/message_event) */
  165. get onmessage(): ((ev: MessageEvent) => unknown) | null
  166. set onmessage(value: ((ev: MessageEvent) => unknown) | null)
  167. /** [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/open_event) */
  168. get onopen(): ((ev: Event) => unknown) | null
  169. set onopen(value: ((ev: Event) => unknown) | null)
  170. addEventListener<K extends keyof EventSourceEventMap>(
  171. type: K,
  172. listener: (this: EventSource_2, ev: EventSourceEventMap[K]) => unknown,
  173. options?: boolean | AddEventListenerOptions,
  174. ): void
  175. addEventListener(
  176. type: string,
  177. listener: (this: EventSource_2, event: MessageEvent) => unknown,
  178. options?: boolean | AddEventListenerOptions,
  179. ): void
  180. addEventListener(
  181. type: string,
  182. listener: EventListenerOrEventListenerObject,
  183. options?: boolean | AddEventListenerOptions,
  184. ): void
  185. removeEventListener<K extends keyof EventSourceEventMap>(
  186. type: K,
  187. listener: (this: EventSource_2, ev: EventSourceEventMap[K]) => unknown,
  188. options?: boolean | EventListenerOptions,
  189. ): void
  190. removeEventListener(
  191. type: string,
  192. listener: (this: EventSource_2, event: MessageEvent) => unknown,
  193. options?: boolean | EventListenerOptions,
  194. ): void
  195. removeEventListener(
  196. type: string,
  197. listener: EventListenerOrEventListenerObject,
  198. options?: boolean | EventListenerOptions,
  199. ): void
  200. constructor(url: string | URL, eventSourceInitDict?: EventSourceInit)
  201. /**
  202. * Aborts any instances of the fetch algorithm started for this EventSource object, and sets the readyState attribute to CLOSED.
  203. *
  204. * [MDN Reference](https://developer.mozilla.org/docs/Web/API/EventSource/close)
  205. *
  206. * @public
  207. */
  208. close(): void
  209. }
  210. export {EventSource_2 as EventSource}
  211. /**
  212. * Mirrors the official DOM typings, with the exception of the extended ErrorEvent.
  213. *
  214. * @public
  215. */
  216. export declare interface EventSourceEventMap {
  217. error: ErrorEvent
  218. message: MessageEvent
  219. open: Event
  220. }
  221. /**
  222. * Subset of `RequestInit` used for `fetch()` calls made by the `EventSource` class.
  223. * As we know that we will be passing certain values, we can be more specific and have
  224. * users not have to do optional chaining and similar for things that will always be there.
  225. *
  226. * @public
  227. */
  228. export declare interface EventSourceFetchInit {
  229. /** An AbortSignal to set request's signal. Typed as `any` because of polyfill inconsistencies. */
  230. signal:
  231. | {
  232. aborted: boolean
  233. }
  234. | any
  235. /** A Headers object, an object literal, or an array of two-item arrays to set request's headers. */
  236. headers: {
  237. [key: string]: string
  238. Accept: 'text/event-stream'
  239. }
  240. /** A string to indicate whether the request will use CORS, or will be restricted to same-origin URLs. Sets request's mode. */
  241. mode: 'cors' | 'no-cors' | 'same-origin'
  242. /** 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. */
  243. credentials?: 'include' | 'omit' | 'same-origin'
  244. /** Controls how the request is cached. */
  245. cache: 'no-store'
  246. /** 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. */
  247. redirect: 'error' | 'follow' | 'manual'
  248. }
  249. /**
  250. * Mirrors the official DOM typings (for the most part)
  251. *
  252. * @public
  253. */
  254. export declare interface EventSourceInit {
  255. /**
  256. * A boolean value, defaulting to `false`, indicating if CORS should be set to `include` credentials.
  257. */
  258. withCredentials?: boolean
  259. /**
  260. * Optional fetch implementation to use. Defaults to `globalThis.fetch`.
  261. * Can also be used for advanced use cases like mocking, proxying, custom certs etc.
  262. */
  263. fetch?: FetchLike
  264. }
  265. /**
  266. * Stripped down version of `fetch()`, only defining the parts we care about.
  267. * This ensures it should work with "most" fetch implementations.
  268. *
  269. * @public
  270. */
  271. export declare type FetchLike = (
  272. url: string | URL,
  273. init: EventSourceFetchInit,
  274. ) => Promise<FetchLikeResponse>
  275. /**
  276. * @public
  277. * @deprecated Use `EventSourceFetchInit` instead.
  278. * This type is only here for backwards compatibility and will be removed in a future version.
  279. */
  280. export declare type FetchLikeInit = EventSourceFetchInit
  281. /**
  282. * Minimal version of the `Response` type returned by `fetch()`.
  283. *
  284. * @public
  285. */
  286. export declare interface FetchLikeResponse {
  287. readonly body:
  288. | {
  289. getReader(): ReaderLike
  290. }
  291. | Response['body']
  292. | null
  293. readonly url: string
  294. readonly status: number
  295. readonly redirected: boolean
  296. readonly headers: {
  297. get(name: string): string | null
  298. }
  299. }
  300. /**
  301. * Stripped down version of `ReadableStreamDefaultReader`, only defining the parts we care about.
  302. *
  303. * @public
  304. */
  305. export declare interface ReaderLike {
  306. read(): Promise<
  307. | {
  308. done: false
  309. value: unknown
  310. }
  311. | {
  312. done: true
  313. value?: undefined
  314. }
  315. >
  316. cancel(): Promise<void>
  317. }
  318. export {}