Skip to content

Commit 4da51b8

Browse files
authored
Merge pull request #5 from hackbg/refactor/srp
v2.0.0 (v2.1.0?)
2 parents 04235ea + da81d5a commit 4da51b8

File tree

17 files changed

+1294
-753
lines changed

17 files changed

+1294
-753
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
indent_style = space
2+
indent_size = 2
3+
max_line_length = 100

CHANGELOG.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
# 2.0.0
22

3-
* Automatic and manual reconnection of WebSocket
4-
* Configuration changes:
5-
* Full URLs now required instead of bare hostnames:
6-
* `hostname` -> `apiUrl`, now requires protocol (i.e. `https://`)
7-
* `wsHostname` -> `wsUrl`, now requires protocol (i.e. `wss://`)
8-
* `http://` and `ws://` supported (for testing only!)
9-
* `clientID` -> `clientId` to match capitalization of other parameters
10-
* Configuration variables in `.env` updated accordingly;, see `.env.example`.
11-
* `lazy` flag prevents initial autoconnect, allowing for `connect()`
12-
to be called manually at a later time
3+
* **NEW:** Unencrypted `http://` and `ws://` are now supported (use for testing only!)
4+
* **NEW:** Custom `Error` classes, discernible using `instanceof`.
5+
* **NEW:** Constructor option `reconnect` (default on) allows reconnection of WebSocket.
6+
* **NEW:** Constructor option `lazy` (default off) prevents autoconnect to socket.
7+
* **NEW:** Methods `connect()` and `disconnect()` manually set the socket connection state.
8+
* **NEW:** Method `unsubscribeAll()` clears feeds and disconnects.
9+
* **NEW:** Field `socketState` reports connection state of currently used `WebSocket`.
10+
* **BREAKING:** `feeds` field is now read-only.
11+
* **BREAKING:** Constructor option `hostname` is now `apiUrl` and requires protocol
12+
(preferably `https://`, alternatively `http://`, possibly `//`)
13+
* **BREAKING:** Constructor option `wsHostname` is now `wsUrl` and requires protocol
14+
(preferably `wss://`, alternatively `ws://`, possibly `//`)
15+
* **BREAKING:** Constructor option `clientID` is now `clientId`, to match others.
16+
* **BREAKING:** Configuration variables in `.env` updated, see `.env.example`.
1317

1418
# 1.1.0
1519

16-
* Allow only `GET|HEAD|OPTIONS` in `generateHeaders`
20+
* **NEW**: Allow only `GET|HEAD|OPTIONS` in `generateHeaders`
1721

1822
# 1.0.1
1923

20-
* Harden `generateHeaders`
24+
* **FIX**: Harden `generateHeaders`
2125

2226
# 1.0.0
2327

24-
* Initial release
28+
* **NEW**: Initial release

index.d.ts

Lines changed: 67 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import type { WebSocket } from 'ws';
22

3-
export type ReconnectOptions = {
4-
enabled?: boolean;
5-
maxReconnectAttempts?: number;
6-
reconnectInterval?: number;
7-
};
8-
export default class ChainlinkDataStreamsConsumer {
3+
export {
4+
Report,
5+
EventEmitter,
6+
ChainlinkDataStreamsConsumer as default,
7+
ChainlinkDataStreamsConsumer as Consumer,
8+
ChainlinkDataStreamsError as Error,
9+
generateHeaders,
10+
}
11+
12+
declare class EventEmitter {
13+
on(event: string, cb: Function): this;
14+
off(event: string, cb: Function): this;
15+
once(event: string, cb: Function): this;
16+
emit(event: string, data: unknown): this;
17+
}
18+
19+
declare class ChainlinkDataStreamsConsumer extends EventEmitter {
920
constructor(args: {
1021
/** API client ID. */
1122
clientId?: string;
@@ -17,40 +28,42 @@ export default class ChainlinkDataStreamsConsumer {
1728
wsUrl?: string;
1829
/** List of feed IDs to subscribe to. */
1930
feeds?: string[];
20-
/** Disable or configure socket auto-reconnect. */
21-
reconnect?: boolean|ReconnectOptions;
22-
/** Don't automatically connect to socket on construction or setting feeds. */
31+
/** Automatically connect to socket on construction or setting feeds? */
2332
lazy?: boolean;
33+
/** Disable or configure socket auto-reconnect. */
34+
reconnect?: boolean|{
35+
enabled?: boolean;
36+
maxAttempts?: number;
37+
interval?: number;
38+
};
2439
});
25-
26-
fetchFeed(args: {
27-
timestamp: string | number;
28-
feed: string;
29-
}): Promise<Report>;
30-
31-
fetchFeeds(args: {
32-
timestamp: string | number;
33-
feeds: string[];
34-
}): Promise<Record<string, Report>>;
35-
36-
subscribeTo(feeds: string | string[]): Promise<WebSocket | null>;
37-
38-
unsubscribeFrom(feeds: string | string[]): Promise<WebSocket | null>;
39-
40-
connect(): Promise<void>;
41-
42-
disconnect(): void;
43-
44-
feeds: Set<string>;
45-
46-
on(event: string, cb: Function): this;
47-
48-
off(event: string, cb: Function): this;
49-
50-
once(event: string, cb: Function): this;
40+
get socketState ():
41+
WebSocket["readyState"]|null;
42+
fetchFeed(args: { timestamp: string | number; feed: string; }):
43+
Promise<Report>;
44+
fetchFeeds(args: { timestamp: string | number; feeds: string[]; }):
45+
Promise<Record<string, Report>>;
46+
subscribeTo(feeds: string | string[]):
47+
Promise<WebSocket | null>;
48+
unsubscribeFrom(feeds: string | string[]):
49+
Promise<WebSocket | null>;
50+
unsubscribeAll():
51+
Promise<WebSocket | null>;
52+
connect():
53+
Promise<void>;
54+
disconnect():
55+
void;
56+
get feeds:
57+
Set<string> & { add: never, delete: never, clear: never };
58+
generateHeaders(
59+
method: string,
60+
path: string,
61+
search: string|URLSearchParams,
62+
timestamp?: number
63+
): AuthHeaders;
5164
}
5265

53-
export type Report = {
66+
declare type Report = {
5467
feedId: string;
5568
observationsTimestamp: bigint;
5669
} & (
@@ -90,3 +103,21 @@ export type Report = {
90103
marketStatus: number;
91104
}
92105
);
106+
107+
declare class ChainlinkDataStreamsError extends Error {
108+
}
109+
110+
declare function generateHeaders (args: {
111+
clientId: string,
112+
clientSecret: string,
113+
method: string,
114+
path: string,
115+
search: string|URLSearchParams,
116+
timestamp?: number
117+
}): AuthHeaders
118+
119+
declare type AuthHeaders = {
120+
'Authorization': string
121+
'X-Authorization-Timestamp': string
122+
'X-Authorization-Signature-SHA256': string
123+
}

0 commit comments

Comments
 (0)