@pythnetwork/hermes-client 1.0.2 → 1.0.4
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/lib/HermesClient.d.ts
CHANGED
|
@@ -20,11 +20,16 @@ export type HermesClientConfig = {
|
|
|
20
20
|
* it will timeout regardless of the retries at the configured `timeout` time.
|
|
21
21
|
*/
|
|
22
22
|
httpRetries?: number;
|
|
23
|
+
/**
|
|
24
|
+
* Optional headers to be included in every request.
|
|
25
|
+
*/
|
|
26
|
+
headers?: HeadersInit;
|
|
23
27
|
};
|
|
24
28
|
export declare class HermesClient {
|
|
25
29
|
private baseURL;
|
|
26
30
|
private timeout;
|
|
27
31
|
private httpRetries;
|
|
32
|
+
private headers;
|
|
28
33
|
/**
|
|
29
34
|
* Constructs a new Connection.
|
|
30
35
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HermesClient.d.ts","sourceRoot":"","sources":["../src/HermesClient.ts"],"names":[],"mappings":"AAAA,OAAO,WAAW,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AACvC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAIxB,MAAM,MAAM,SAAS,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,SAAS,CAAC,CAAC;AAC1D,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAC1E,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,YAAY,CAAC,CAAC;AAChE,MAAM,MAAM,iBAAiB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,iBAAiB,CAAC,CAAC;AAC1E,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,OAAO,CAAC,YAAY,CAAC,CAAC;AACh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Y,CAAC;QACxB,MAAM,CAAC,EAAE,OAAO,CAAC;QACjB,cAAc,CAAC,EAAE,OAAO,CAAC;QACzB,cAAc,CAAC,EAAE,OAAO,CAAC;KAC1B,GACA,OAAO,CAAC,WAAW,CAAC;IAcvB,OAAO,CAAC,qBAAqB;CAU9B"}
|
package/lib/HermesClient.js
CHANGED
|
@@ -13,6 +13,7 @@ class HermesClient {
|
|
|
13
13
|
baseURL;
|
|
14
14
|
timeout;
|
|
15
15
|
httpRetries;
|
|
16
|
+
headers;
|
|
16
17
|
/**
|
|
17
18
|
* Constructs a new Connection.
|
|
18
19
|
*
|
|
@@ -23,12 +24,17 @@ class HermesClient {
|
|
|
23
24
|
this.baseURL = endpoint;
|
|
24
25
|
this.timeout = config?.timeout ?? DEFAULT_TIMEOUT;
|
|
25
26
|
this.httpRetries = config?.httpRetries ?? DEFAULT_HTTP_RETRIES;
|
|
27
|
+
this.headers = config?.headers ?? {};
|
|
26
28
|
}
|
|
27
29
|
async httpRequest(url, schema, options, retries = this.httpRetries, backoff = 100 + Math.floor(Math.random() * 100), // Adding randomness to the initial backoff to avoid "thundering herd" scenario where a lot of clients that get kicked off all at the same time (say some script or something) and fail to connect all retry at exactly the same time too
|
|
28
30
|
externalAbortController) {
|
|
29
31
|
const controller = externalAbortController ?? new AbortController();
|
|
30
32
|
const { signal } = controller;
|
|
31
|
-
options = {
|
|
33
|
+
options = {
|
|
34
|
+
...options,
|
|
35
|
+
signal,
|
|
36
|
+
headers: { ...this.headers, ...options?.headers },
|
|
37
|
+
}; // Merge any existing options with the signal and headers
|
|
32
38
|
// Set a timeout to abort the request if it takes too long
|
|
33
39
|
const timeout = setTimeout(() => controller.abort(), this.timeout);
|
|
34
40
|
try {
|
|
@@ -63,7 +69,7 @@ class HermesClient {
|
|
|
63
69
|
* @returns Array of PriceFeedMetadata objects.
|
|
64
70
|
*/
|
|
65
71
|
async getPriceFeeds(options) {
|
|
66
|
-
const url = new URL("
|
|
72
|
+
const url = new URL("v2/price_feeds", this.baseURL);
|
|
67
73
|
if (options) {
|
|
68
74
|
this.appendUrlSearchParams(url, options);
|
|
69
75
|
}
|
|
@@ -82,7 +88,7 @@ class HermesClient {
|
|
|
82
88
|
* @returns PriceUpdate object containing the latest updates.
|
|
83
89
|
*/
|
|
84
90
|
async getLatestPriceUpdates(ids, options) {
|
|
85
|
-
const url = new URL(
|
|
91
|
+
const url = new URL("v2/updates/price/latest", this.baseURL);
|
|
86
92
|
for (const id of ids) {
|
|
87
93
|
url.searchParams.append("ids[]", id);
|
|
88
94
|
}
|
|
@@ -105,7 +111,7 @@ class HermesClient {
|
|
|
105
111
|
* @returns PriceUpdate object containing the updates at the specified timestamp.
|
|
106
112
|
*/
|
|
107
113
|
async getPriceUpdatesAtTimestamp(publishTime, ids, options) {
|
|
108
|
-
const url = new URL(
|
|
114
|
+
const url = new URL(`v2/updates/price/${publishTime}`, this.baseURL);
|
|
109
115
|
for (const id of ids) {
|
|
110
116
|
url.searchParams.append("ids[]", id);
|
|
111
117
|
}
|
|
@@ -130,7 +136,7 @@ class HermesClient {
|
|
|
130
136
|
* @returns An EventSource instance for receiving streaming updates.
|
|
131
137
|
*/
|
|
132
138
|
async getPriceUpdatesStream(ids, options) {
|
|
133
|
-
const url = new URL("
|
|
139
|
+
const url = new URL("v2/updates/price/stream", this.baseURL);
|
|
134
140
|
ids.forEach((id) => {
|
|
135
141
|
url.searchParams.append("ids[]", id);
|
|
136
142
|
});
|
|
@@ -138,7 +144,7 @@ class HermesClient {
|
|
|
138
144
|
const transformedOptions = (0, utils_1.camelToSnakeCaseObject)(options);
|
|
139
145
|
this.appendUrlSearchParams(url, transformedOptions);
|
|
140
146
|
}
|
|
141
|
-
return new eventsource_1.default(url.toString());
|
|
147
|
+
return new eventsource_1.default(url.toString(), { headers: this.headers });
|
|
142
148
|
}
|
|
143
149
|
appendUrlSearchParams(url, params) {
|
|
144
150
|
Object.entries(params).forEach(([key, value]) => {
|
|
@@ -27,8 +27,25 @@ const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
|
|
27
27
|
"parse-numbers": false,
|
|
28
28
|
})
|
|
29
29
|
.parseSync();
|
|
30
|
+
/**
|
|
31
|
+
* Extracts the endpoint and basic authorization headers from a given URL string.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} urlString - The URL string containing the endpoint and optional basic auth credentials.
|
|
34
|
+
* @returns {{ endpoint: string; headers: HeadersInit }} An object containing the endpoint URL and headers.
|
|
35
|
+
*/
|
|
36
|
+
function extractBasicAuthorizationHeadersFromUrl(urlString) {
|
|
37
|
+
const url = new URL(urlString);
|
|
38
|
+
const headers = {};
|
|
39
|
+
if (url.username && url.password) {
|
|
40
|
+
headers["Authorization"] = `Basic ${btoa(`${url.username}:${url.password}`)}`;
|
|
41
|
+
url.username = "";
|
|
42
|
+
url.password = "";
|
|
43
|
+
}
|
|
44
|
+
return { endpoint: url.toString(), headers };
|
|
45
|
+
}
|
|
30
46
|
async function run() {
|
|
31
|
-
const
|
|
47
|
+
const { endpoint, headers } = extractBasicAuthorizationHeadersFromUrl(argv.endpoint);
|
|
48
|
+
const connection = new HermesClient_1.HermesClient(endpoint, { headers });
|
|
32
49
|
const priceIds = argv.priceIds;
|
|
33
50
|
// Get price feeds
|
|
34
51
|
const priceFeeds = await connection.getPriceFeeds({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pythnetwork/hermes-client",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Pyth Hermes Client",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Pyth Data Association"
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"eventsource": "^2.0.2",
|
|
58
58
|
"zod": "^3.23.8"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "07a7767ca5718c839f55702439af08a6889f682b"
|
|
61
61
|
}
|