-
Notifications
You must be signed in to change notification settings - Fork 33
/
index.d.ts
77 lines (65 loc) · 1.93 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
export type BuiltInEventType = 'open' | 'message' | 'error' | 'close';
export type EventType<E extends string = never> = E | BuiltInEventType;
export interface MessageEvent {
type: 'message';
data: string | null;
lastEventId: string | null;
url: string;
}
export interface OpenEvent {
type: 'open';
}
export interface CloseEvent {
type: 'close';
}
export interface TimeoutEvent {
type: 'timeout';
}
export interface ErrorEvent {
type: 'error';
message: string;
xhrState: number;
xhrStatus: number;
}
export interface CustomEvent<E extends string> {
type: E;
data: string | null;
lastEventId: string | null;
url: string;
}
export interface ExceptionEvent {
type: 'exception';
message: string;
error: Error;
}
export interface EventSourceOptions {
method?: string;
timeout?: number;
timeoutBeforeConnection?: number;
withCredentials?: boolean;
headers?: Record<string, any>;
body?: any;
debug?: boolean;
pollingInterval?: number;
lineEndingCharacter?: string;
}
type BuiltInEventMap = {
'message': MessageEvent,
'open': OpenEvent,
'close': CloseEvent,
'error': ErrorEvent | TimeoutEvent | ExceptionEvent,
};
export type EventSourceEvent<E extends T, T extends string = any> = E extends BuiltInEventType ? BuiltInEventMap[E] : CustomEvent<E>;
export type EventSourceListener<E extends string = never, T extends EventType<E> = EventType<E>> = (
event: EventSourceEvent<T>
) => void;
declare class EventSource<E extends string = never> {
constructor(url: URL | string, options?: EventSourceOptions);
open(): void;
close(): void;
addEventListener<T extends EventType<E>>(type: T, listener: EventSourceListener<E, T>): void;
removeEventListener<T extends EventType<E>>(type: T, listener: EventSourceListener<E, T>): void;
removeAllEventListeners<T extends EventType<E>>(type?: T): void;
dispatch<T extends EventType<E>>(type: T, data: EventSourceEvent<T>): void;
}
export default EventSource;