@hyper-fetch/sockets 2.0.0 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -12
- package/dist/{client/client.d.ts → adapter/adapter.d.ts} +6 -6
- package/dist/{client/client.types.d.ts → adapter/adapter.types.d.ts} +4 -3
- package/dist/adapter/adapter.utils.d.ts +2 -0
- package/dist/adapter/index.d.ts +3 -0
- package/dist/emitter/emitter.d.ts +14 -14
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +4 -4
- package/dist/index.d.ts +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/index.esm.js.map +4 -4
- package/dist/listener/listener.d.ts +9 -9
- package/dist/socket/socket.d.ts +33 -25
- package/dist/socket/socket.types.d.ts +11 -11
- package/dist/socket/socket.utils.d.ts +1 -1
- package/dist/types/extract.types.d.ts +8 -4
- package/package.json +1 -1
- package/src/{client/client.ts → adapter/adapter.ts} +30 -28
- package/src/{client/client.types.ts → adapter/adapter.types.ts} +4 -3
- package/src/{client/client.utils.ts → adapter/adapter.utils.ts} +3 -3
- package/src/adapter/index.ts +3 -0
- package/src/emitter/emitter.ts +22 -27
- package/src/index.ts +1 -1
- package/src/listener/listener.ts +11 -13
- package/src/socket/socket.ts +49 -31
- package/src/socket/socket.types.ts +11 -11
- package/src/socket/socket.utils.ts +3 -3
- package/src/types/extract.types.ts +11 -7
- package/dist/client/client.utils.d.ts +0 -2
- package/dist/client/index.d.ts +0 -3
- package/src/client/index.ts +0 -3
|
@@ -5,22 +5,23 @@ export type RemoveListenerCallbackType = () => void;
|
|
|
5
5
|
|
|
6
6
|
export type ListenerCallbackType<D = any> = (data: D, event: MessageEvent<D>) => void;
|
|
7
7
|
|
|
8
|
-
export type
|
|
8
|
+
export type AdapterType = {
|
|
9
9
|
connecting: boolean;
|
|
10
10
|
listeners: Map<string, Set<ListenerCallbackType>>;
|
|
11
11
|
emit: (eventMessageId: string, emitter: EmitterInstance, ack?: (error: Error | null, response: any) => void) => void;
|
|
12
12
|
listen: (listener: ListenerInstance, callback: ListenerCallbackType) => RemoveListenerCallbackType;
|
|
13
13
|
removeListener: (name: string, callback: (...args: any) => void) => void;
|
|
14
14
|
connect: () => void;
|
|
15
|
+
reconnect: () => void;
|
|
15
16
|
disconnect: () => void;
|
|
16
17
|
};
|
|
17
18
|
|
|
18
|
-
export type
|
|
19
|
+
export type SSEAdapterOptionsType = {
|
|
19
20
|
eventSourceInit?: EventSourceInit;
|
|
20
21
|
reconnectTimeout?: number;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
|
-
export type
|
|
24
|
+
export type WSAdapterOptionsType = {
|
|
24
25
|
protocols?: string[];
|
|
25
26
|
pingTimeout?: number;
|
|
26
27
|
pongTimeout?: number;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SocketInstance } from "socket";
|
|
2
2
|
|
|
3
|
-
export const
|
|
3
|
+
export const getAdapter = (socket: SocketInstance) => {
|
|
4
4
|
const queryParams = socket.queryParamsStringify(socket.queryParams).substring(1);
|
|
5
5
|
const authParams = socket.queryParamsStringify(socket.auth).substring(1);
|
|
6
6
|
const connector = queryParams && authParams ? "&" : "";
|
|
@@ -8,8 +8,8 @@ export const getClient = (socket: SocketInstance) => {
|
|
|
8
8
|
|
|
9
9
|
if ("isSSE" in socket.options) {
|
|
10
10
|
if (!window?.EventSource) return null;
|
|
11
|
-
return new EventSource(fullUrl, socket.options.
|
|
11
|
+
return new EventSource(fullUrl, socket.options.adapterOptions?.eventSourceInit);
|
|
12
12
|
}
|
|
13
13
|
if (!window?.WebSocket) return null;
|
|
14
|
-
return new WebSocket(fullUrl, socket.options.
|
|
14
|
+
return new WebSocket(fullUrl, socket.options.adapterOptions?.protocols);
|
|
15
15
|
};
|
package/src/emitter/emitter.ts
CHANGED
|
@@ -2,20 +2,20 @@ import { DateInterval, getUniqueRequestId } from "@hyper-fetch/core";
|
|
|
2
2
|
|
|
3
3
|
import { Socket } from "socket";
|
|
4
4
|
import { EmitterOptionsType, EmitterCloneOptionsType } from "emitter";
|
|
5
|
-
import {
|
|
5
|
+
import { AdapterType } from "adapter";
|
|
6
6
|
import { ExtractEmitterOptionsType } from "types/extract.types";
|
|
7
7
|
|
|
8
|
-
export class Emitter<
|
|
8
|
+
export class Emitter<Payload, Response, Adapter extends AdapterType, MappedData = void> {
|
|
9
9
|
readonly name: string;
|
|
10
10
|
timeout: number;
|
|
11
|
-
data:
|
|
12
|
-
options: ExtractEmitterOptionsType<
|
|
11
|
+
data: Payload | null = null;
|
|
12
|
+
options: ExtractEmitterOptionsType<Adapter>;
|
|
13
13
|
|
|
14
14
|
constructor(
|
|
15
|
-
readonly socket: Socket<
|
|
16
|
-
readonly emitterOptions: EmitterOptionsType<ExtractEmitterOptionsType<
|
|
17
|
-
dump?: Partial<Emitter<
|
|
18
|
-
readonly dataMapper?: (data:
|
|
15
|
+
readonly socket: Socket<Adapter>,
|
|
16
|
+
readonly emitterOptions: EmitterOptionsType<ExtractEmitterOptionsType<Adapter>>,
|
|
17
|
+
dump?: Partial<Emitter<Payload, Response, Adapter, MappedData>>,
|
|
18
|
+
readonly dataMapper?: (data: Payload) => MappedData,
|
|
19
19
|
) {
|
|
20
20
|
const { name, timeout = DateInterval.second * 3, options } = emitterOptions;
|
|
21
21
|
this.name = dump?.name ?? name;
|
|
@@ -24,7 +24,7 @@ export class Emitter<RequestDataType, ResponseDataType, ClientType extends Webso
|
|
|
24
24
|
this.options = dump?.options ?? options;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
setOptions(options: ExtractEmitterOptionsType<
|
|
27
|
+
setOptions(options: ExtractEmitterOptionsType<Adapter>) {
|
|
28
28
|
return this.clone({ options });
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -32,46 +32,41 @@ export class Emitter<RequestDataType, ResponseDataType, ClientType extends Webso
|
|
|
32
32
|
return this.clone({ timeout });
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
setData(data:
|
|
35
|
+
setData(data: Payload) {
|
|
36
36
|
if (this.dataMapper) {
|
|
37
37
|
return this.clone<MappedData>({ data: this.dataMapper(data) });
|
|
38
38
|
}
|
|
39
39
|
return this.clone({ data });
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
setDataMapper = <MapperData>(mapper: (data:
|
|
43
|
-
return this.clone<
|
|
42
|
+
setDataMapper = <MapperData>(mapper: (data: Payload) => MapperData) => {
|
|
43
|
+
return this.clone<Payload, MapperData>(undefined, mapper);
|
|
44
44
|
};
|
|
45
45
|
|
|
46
|
-
clone<
|
|
47
|
-
config?: Partial<EmitterCloneOptionsType<
|
|
48
|
-
mapper?: (data:
|
|
49
|
-
): Emitter<
|
|
50
|
-
const dump: Partial<Emitter<
|
|
46
|
+
clone<NewPayload = Payload, MapperData = MappedData>(
|
|
47
|
+
config?: Partial<EmitterCloneOptionsType<NewPayload, ExtractEmitterOptionsType<Adapter>>>,
|
|
48
|
+
mapper?: (data: NewPayload) => MapperData,
|
|
49
|
+
): Emitter<NewPayload, Response, Adapter, MapperData> {
|
|
50
|
+
const dump: Partial<Emitter<NewPayload, Response, Adapter, MapperData>> = {
|
|
51
51
|
timeout: this.timeout,
|
|
52
52
|
options: this.options,
|
|
53
|
-
data: this.data as unknown as
|
|
53
|
+
data: this.data as unknown as NewPayload,
|
|
54
54
|
...config,
|
|
55
55
|
name: this.name,
|
|
56
56
|
};
|
|
57
57
|
const mapperFn = (mapper || this.dataMapper) as typeof mapper;
|
|
58
58
|
|
|
59
|
-
return new Emitter<
|
|
60
|
-
this.socket,
|
|
61
|
-
this.emitterOptions,
|
|
62
|
-
dump,
|
|
63
|
-
mapperFn,
|
|
64
|
-
);
|
|
59
|
+
return new Emitter<NewPayload, Response, Adapter, MapperData>(this.socket, this.emitterOptions, dump, mapperFn);
|
|
65
60
|
}
|
|
66
61
|
|
|
67
62
|
emit(
|
|
68
|
-
options?: Partial<EmitterCloneOptionsType<
|
|
69
|
-
ack?: (error: Error | null, response:
|
|
63
|
+
options?: Partial<EmitterCloneOptionsType<Payload, ExtractEmitterOptionsType<Adapter>>>,
|
|
64
|
+
ack?: (error: Error | null, response: Response) => void,
|
|
70
65
|
) {
|
|
71
66
|
const instance = this.clone(options);
|
|
72
67
|
const eventMessageId = getUniqueRequestId(this.name);
|
|
73
68
|
|
|
74
|
-
this.socket.
|
|
69
|
+
this.socket.adapter.emit(eventMessageId, instance, ack);
|
|
75
70
|
return eventMessageId;
|
|
76
71
|
}
|
|
77
72
|
}
|
package/src/index.ts
CHANGED
package/src/listener/listener.ts
CHANGED
|
@@ -1,42 +1,40 @@
|
|
|
1
1
|
import { Socket } from "socket";
|
|
2
2
|
import { ListenerOptionsType } from "listener";
|
|
3
|
-
import { ListenerCallbackType,
|
|
3
|
+
import { ListenerCallbackType, AdapterType } from "adapter";
|
|
4
4
|
import { ExtractListenerOptionsType } from "types/extract.types";
|
|
5
5
|
|
|
6
|
-
export class Listener<
|
|
6
|
+
export class Listener<Response, Adapter extends AdapterType> {
|
|
7
7
|
readonly name: string;
|
|
8
|
-
options?: ExtractListenerOptionsType<
|
|
8
|
+
options?: ExtractListenerOptionsType<Adapter>;
|
|
9
9
|
|
|
10
10
|
constructor(
|
|
11
|
-
readonly socket: Socket<
|
|
12
|
-
readonly listenerOptions?: ListenerOptionsType<ExtractListenerOptionsType<
|
|
11
|
+
readonly socket: Socket<Adapter>,
|
|
12
|
+
readonly listenerOptions?: ListenerOptionsType<ExtractListenerOptionsType<Adapter>>,
|
|
13
13
|
) {
|
|
14
14
|
const { name, options } = listenerOptions;
|
|
15
15
|
this.name = name;
|
|
16
16
|
this.options = options;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
setOptions(options: ExtractListenerOptionsType<
|
|
19
|
+
setOptions(options: ExtractListenerOptionsType<Adapter>) {
|
|
20
20
|
return this.clone({ options });
|
|
21
21
|
}
|
|
22
22
|
|
|
23
|
-
clone(
|
|
24
|
-
|
|
25
|
-
): Listener<ResponseType, ClientType> {
|
|
26
|
-
return new Listener<ResponseType, ClientType>(this.socket, {
|
|
23
|
+
clone(config?: Partial<ListenerOptionsType<ExtractListenerOptionsType<Adapter>>>): Listener<Response, Adapter> {
|
|
24
|
+
return new Listener<Response, Adapter>(this.socket, {
|
|
27
25
|
...this.listenerOptions,
|
|
28
26
|
...config,
|
|
29
27
|
name: this.name,
|
|
30
28
|
});
|
|
31
29
|
}
|
|
32
30
|
|
|
33
|
-
listen(callback: (data: ListenerCallbackType<
|
|
31
|
+
listen(callback: (data: ListenerCallbackType<Response>) => void) {
|
|
34
32
|
const instance = this.clone();
|
|
35
33
|
|
|
36
|
-
this.socket.
|
|
34
|
+
this.socket.adapter.listen(instance, callback);
|
|
37
35
|
|
|
38
36
|
const removeListener = () => {
|
|
39
|
-
this.socket.
|
|
37
|
+
this.socket.adapter.removeListener(instance.name, callback);
|
|
40
38
|
};
|
|
41
39
|
|
|
42
40
|
return [removeListener, callback] as const;
|
package/src/socket/socket.ts
CHANGED
|
@@ -2,16 +2,16 @@ import EventEmitter from "events";
|
|
|
2
2
|
import {
|
|
3
3
|
stringifyQueryParams,
|
|
4
4
|
StringifyCallbackType,
|
|
5
|
-
|
|
5
|
+
QueryStringifyOptionsType,
|
|
6
6
|
LoggerManager,
|
|
7
7
|
SeverityType,
|
|
8
8
|
AppManager,
|
|
9
9
|
DateInterval,
|
|
10
|
-
|
|
10
|
+
QueryParamsType,
|
|
11
11
|
} from "@hyper-fetch/core";
|
|
12
12
|
|
|
13
13
|
import {
|
|
14
|
-
|
|
14
|
+
SocketAdapterOptionsType,
|
|
15
15
|
ReconnectCallbackType,
|
|
16
16
|
ReconnectStopCallbackType,
|
|
17
17
|
OpenCallbackType,
|
|
@@ -23,36 +23,36 @@ import {
|
|
|
23
23
|
interceptListener,
|
|
24
24
|
interceptEmitter,
|
|
25
25
|
} from "socket";
|
|
26
|
-
import {
|
|
26
|
+
import { AdapterType, SocketAdapter } from "adapter";
|
|
27
27
|
import { Listener, ListenerOptionsType } from "listener";
|
|
28
28
|
import { Emitter, EmitterInstance, EmitterOptionsType } from "emitter";
|
|
29
29
|
|
|
30
|
-
export class Socket<
|
|
30
|
+
export class Socket<Adapter extends Record<keyof AdapterType, any>> {
|
|
31
31
|
public emitter = new EventEmitter();
|
|
32
32
|
public events = getSocketEvents(this.emitter);
|
|
33
33
|
|
|
34
34
|
url: string;
|
|
35
35
|
reconnect: number;
|
|
36
36
|
reconnectTime: number;
|
|
37
|
-
auth?:
|
|
38
|
-
queryParams?:
|
|
37
|
+
auth?: QueryParamsType | string;
|
|
38
|
+
queryParams?: QueryParamsType | string;
|
|
39
39
|
debug: boolean;
|
|
40
40
|
autoConnect: boolean;
|
|
41
41
|
|
|
42
42
|
// Callbacks
|
|
43
|
-
__onOpenCallbacks: OpenCallbackType<Socket<
|
|
44
|
-
__onCloseCallbacks: CloseCallbackType<Socket<
|
|
45
|
-
__onReconnectCallbacks: ReconnectCallbackType<Socket<
|
|
46
|
-
__onReconnectStopCallbacks: ReconnectStopCallbackType<Socket<
|
|
47
|
-
__onMessageCallbacks: MessageCallbackType<Socket<
|
|
43
|
+
__onOpenCallbacks: OpenCallbackType<Socket<Adapter>, any>[] = [];
|
|
44
|
+
__onCloseCallbacks: CloseCallbackType<Socket<Adapter>, any>[] = [];
|
|
45
|
+
__onReconnectCallbacks: ReconnectCallbackType<Socket<Adapter>>[] = [];
|
|
46
|
+
__onReconnectStopCallbacks: ReconnectStopCallbackType<Socket<Adapter>>[] = [];
|
|
47
|
+
__onMessageCallbacks: MessageCallbackType<Socket<Adapter>, any>[] = [];
|
|
48
48
|
__onSendCallbacks: SendCallbackType<EmitterInstance>[] = [];
|
|
49
|
-
__onErrorCallbacks: ErrorCallbackType<Socket<
|
|
49
|
+
__onErrorCallbacks: ErrorCallbackType<Socket<Adapter>, any>[] = [];
|
|
50
50
|
|
|
51
51
|
// Config
|
|
52
|
-
|
|
52
|
+
adapter: Adapter;
|
|
53
53
|
loggerManager = new LoggerManager(this);
|
|
54
54
|
appManager = new AppManager();
|
|
55
|
-
queryParamsConfig?:
|
|
55
|
+
queryParamsConfig?: QueryStringifyOptionsType;
|
|
56
56
|
|
|
57
57
|
// Logger
|
|
58
58
|
logger = this.loggerManager.init("Socket");
|
|
@@ -64,11 +64,11 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
64
64
|
return stringifyQueryParams(queryParams, this.queryParamsConfig);
|
|
65
65
|
};
|
|
66
66
|
|
|
67
|
-
constructor(public options:
|
|
67
|
+
constructor(public options: SocketAdapterOptionsType<Adapter>) {
|
|
68
68
|
const {
|
|
69
69
|
url,
|
|
70
70
|
auth,
|
|
71
|
-
|
|
71
|
+
adapter,
|
|
72
72
|
queryParams,
|
|
73
73
|
autoConnect,
|
|
74
74
|
reconnect,
|
|
@@ -91,8 +91,8 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
91
91
|
this.queryParamsStringify = queryParamsStringify;
|
|
92
92
|
}
|
|
93
93
|
|
|
94
|
-
//
|
|
95
|
-
this.
|
|
94
|
+
// Adapter must be initialized at the end
|
|
95
|
+
this.adapter = adapter || (new SocketAdapter(this) as unknown as Adapter);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
/**
|
|
@@ -114,11 +114,29 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
114
114
|
/**
|
|
115
115
|
* Set the new logger instance to the socket
|
|
116
116
|
*/
|
|
117
|
-
setLogger = (callback: (socket: Socket<
|
|
117
|
+
setLogger = (callback: (socket: Socket<Adapter>) => LoggerManager) => {
|
|
118
118
|
this.loggerManager = callback(this);
|
|
119
119
|
return this;
|
|
120
120
|
};
|
|
121
121
|
|
|
122
|
+
/**
|
|
123
|
+
* Set the new auth data to the socket
|
|
124
|
+
*/
|
|
125
|
+
setAuth = (auth: QueryParamsType | string) => {
|
|
126
|
+
this.auth = auth;
|
|
127
|
+
this.adapter.reconnect();
|
|
128
|
+
return this;
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Set the new query data to the socket
|
|
133
|
+
*/
|
|
134
|
+
setQuery = (queryParams: QueryParamsType | string) => {
|
|
135
|
+
this.queryParams = queryParams;
|
|
136
|
+
this.adapter.reconnect();
|
|
137
|
+
return this;
|
|
138
|
+
};
|
|
139
|
+
|
|
122
140
|
/**
|
|
123
141
|
* Callbacks
|
|
124
142
|
*/
|
|
@@ -128,7 +146,7 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
128
146
|
* @param callback
|
|
129
147
|
* @returns
|
|
130
148
|
*/
|
|
131
|
-
onOpen<Event = MessageEvent>(callback: OpenCallbackType<Socket<
|
|
149
|
+
onOpen<Event = MessageEvent>(callback: OpenCallbackType<Socket<Adapter>, Event>) {
|
|
132
150
|
this.__onOpenCallbacks.push(callback);
|
|
133
151
|
return this;
|
|
134
152
|
}
|
|
@@ -137,7 +155,7 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
137
155
|
* @param callback
|
|
138
156
|
* @returns
|
|
139
157
|
*/
|
|
140
|
-
onClose<Event = MessageEvent>(callback: CloseCallbackType<Socket<
|
|
158
|
+
onClose<Event = MessageEvent>(callback: CloseCallbackType<Socket<Adapter>, Event>) {
|
|
141
159
|
this.__onCloseCallbacks.push(callback);
|
|
142
160
|
return this;
|
|
143
161
|
}
|
|
@@ -147,7 +165,7 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
147
165
|
* @param callback
|
|
148
166
|
* @returns
|
|
149
167
|
*/
|
|
150
|
-
onReconnect(callback: ReconnectCallbackType<Socket<
|
|
168
|
+
onReconnect(callback: ReconnectCallbackType<Socket<Adapter>>) {
|
|
151
169
|
this.__onReconnectCallbacks.push(callback);
|
|
152
170
|
return this;
|
|
153
171
|
}
|
|
@@ -157,7 +175,7 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
157
175
|
* @param callback
|
|
158
176
|
* @returns
|
|
159
177
|
*/
|
|
160
|
-
onReconnectStop(callback: ReconnectStopCallbackType<Socket<
|
|
178
|
+
onReconnectStop(callback: ReconnectStopCallbackType<Socket<Adapter>>) {
|
|
161
179
|
this.__onReconnectStopCallbacks.push(callback);
|
|
162
180
|
return this;
|
|
163
181
|
}
|
|
@@ -167,7 +185,7 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
167
185
|
* @param callback
|
|
168
186
|
* @returns
|
|
169
187
|
*/
|
|
170
|
-
onMessage<Event = MessageEvent>(callback: MessageCallbackType<Socket<
|
|
188
|
+
onMessage<Event = MessageEvent>(callback: MessageCallbackType<Socket<Adapter>, Event>) {
|
|
171
189
|
this.__onMessageCallbacks.push(callback);
|
|
172
190
|
return this;
|
|
173
191
|
}
|
|
@@ -187,7 +205,7 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
187
205
|
* @param callback
|
|
188
206
|
* @returns
|
|
189
207
|
*/
|
|
190
|
-
onError<Event = MessageEvent>(callback: ErrorCallbackType<Socket<
|
|
208
|
+
onError<Event = MessageEvent>(callback: ErrorCallbackType<Socket<Adapter>, Event>) {
|
|
191
209
|
this.__onErrorCallbacks.push(callback);
|
|
192
210
|
return this;
|
|
193
211
|
}
|
|
@@ -217,8 +235,8 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
217
235
|
* @param options
|
|
218
236
|
* @returns
|
|
219
237
|
*/
|
|
220
|
-
createListener = <
|
|
221
|
-
return new Listener<
|
|
238
|
+
createListener = <Response>(options: ListenerOptionsType<Adapter>) => {
|
|
239
|
+
return new Listener<Response, Adapter>(this, options as any);
|
|
222
240
|
};
|
|
223
241
|
|
|
224
242
|
/**
|
|
@@ -226,10 +244,10 @@ export class Socket<ClientType extends Record<keyof WebsocketClientType, any>> {
|
|
|
226
244
|
* @param options
|
|
227
245
|
* @returns
|
|
228
246
|
*/
|
|
229
|
-
createEmitter = <
|
|
247
|
+
createEmitter = <Payload, Response = never>(options: EmitterOptionsType<Adapter>) => {
|
|
230
248
|
if ("isSSE" in this.options) {
|
|
231
|
-
throw new Error("Cannot create emitters for SSE
|
|
249
|
+
throw new Error("Cannot create emitters for SSE adapter");
|
|
232
250
|
}
|
|
233
|
-
return new Emitter<
|
|
251
|
+
return new Emitter<Payload, Response, Adapter>(this, options as any);
|
|
234
252
|
};
|
|
235
253
|
}
|
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { QueryParamsType, QueryStringifyOptionsType, StringifyCallbackType } from "@hyper-fetch/core";
|
|
2
2
|
|
|
3
3
|
import { Socket } from "socket";
|
|
4
|
-
import {
|
|
4
|
+
import { SSEAdapterOptionsType, WSAdapterOptionsType } from "adapter";
|
|
5
5
|
import { EmitterInstance } from "emitter";
|
|
6
6
|
|
|
7
7
|
export type SocketInstance = Socket<any>;
|
|
8
8
|
|
|
9
|
-
export type
|
|
9
|
+
export type SocketOptionsType<SocketAdapterType> = {
|
|
10
10
|
url: string;
|
|
11
|
-
|
|
12
|
-
auth?:
|
|
13
|
-
queryParams?:
|
|
11
|
+
adapter?: SocketAdapterType;
|
|
12
|
+
auth?: QueryParamsType;
|
|
13
|
+
queryParams?: QueryParamsType | string;
|
|
14
14
|
reconnect?: number;
|
|
15
15
|
reconnectTime?: number;
|
|
16
16
|
autoConnect?: boolean;
|
|
17
|
-
queryParamsConfig?:
|
|
17
|
+
queryParamsConfig?: QueryStringifyOptionsType;
|
|
18
18
|
queryParamsStringify?: StringifyCallbackType;
|
|
19
19
|
};
|
|
20
20
|
|
|
21
|
-
export type
|
|
22
|
-
| (
|
|
23
|
-
| (
|
|
24
|
-
|
|
21
|
+
export type SocketAdapterOptionsType<SocketAdapterType> =
|
|
22
|
+
| (SocketOptionsType<SocketAdapterType> & { isSSE: true; adapterOptions?: SSEAdapterOptionsType })
|
|
23
|
+
| (SocketOptionsType<SocketAdapterType> & {
|
|
24
|
+
adapterOptions?: WSAdapterOptionsType;
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
export type ReconnectCallbackType<SocketType extends SocketInstance> = (socket: SocketType) => void;
|
|
@@ -14,10 +14,10 @@ export const getListenerRemoveByNameKey = (event: string) => `${event}_listener_
|
|
|
14
14
|
export const getListenerEventByNameKey = (event: string) => `${event}_listener_event`;
|
|
15
15
|
export const getEmitterEventByNameKey = (event: string) => `${event}_emitter_event`;
|
|
16
16
|
|
|
17
|
-
export const interceptListener = <
|
|
18
|
-
interceptors: MessageCallbackType<
|
|
17
|
+
export const interceptListener = <Socket extends SocketInstance>(
|
|
18
|
+
interceptors: MessageCallbackType<Socket, MessageEvent>[],
|
|
19
19
|
response: MessageEvent,
|
|
20
|
-
socket:
|
|
20
|
+
socket: Socket,
|
|
21
21
|
) => {
|
|
22
22
|
let newResponse = response;
|
|
23
23
|
// eslint-disable-next-line no-restricted-syntax
|
|
@@ -1,16 +1,20 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AdapterType } from "adapter";
|
|
2
2
|
import { Emitter } from "emitter";
|
|
3
3
|
import { Listener } from "listener";
|
|
4
4
|
|
|
5
|
-
//
|
|
5
|
+
// Adapter
|
|
6
|
+
|
|
7
|
+
export type ExtractEmitterOptionsType<E extends Record<keyof AdapterType | string, any>> = E["emitterOptions"];
|
|
8
|
+
export type ExtractListenerOptionsType<E extends Record<keyof AdapterType | string, any>> = E["listenerOptions"];
|
|
6
9
|
|
|
7
|
-
|
|
10
|
+
// Emitter
|
|
8
11
|
|
|
9
|
-
export type
|
|
10
|
-
|
|
12
|
+
export type ExtractEmitterPayloadType<E> = E extends Emitter<infer P, any, any, any> ? P : never;
|
|
13
|
+
export type ExtractEmitterResponseType<E> = E extends Emitter<any, infer R, any, any> ? R : never;
|
|
14
|
+
export type ExtractEmitterAdapterType<E> = E extends Emitter<any, any, infer A, any> ? A : never;
|
|
15
|
+
export type ExtractEmitterMappedDataType<E> = E extends Emitter<any, any, any, infer M> ? M : never;
|
|
11
16
|
|
|
12
17
|
// Listener
|
|
13
18
|
|
|
14
19
|
export type ExtractListenerResponseType<T> = T extends Listener<infer R, any> ? R : never;
|
|
15
|
-
|
|
16
|
-
export type ExtractListenerDataType<T> = T extends Emitter<infer D, any, any> ? D : never;
|
|
20
|
+
export type ExtractListenerAdapterType<E> = E extends Listener<any, infer A> ? A : never;
|
package/dist/client/index.d.ts
DELETED
package/src/client/index.ts
DELETED