Przeglądaj źródła

Merge pull request #2286 from pyth-network/cprussin/fix-hermes-client

fix(hermes-client): ensure relative urls are built properly
Connor Prussin 10 miesięcy temu
rodzic
commit
cdaf99e862

+ 4 - 1
apps/hermes/client/js/README.md

@@ -32,7 +32,10 @@ const priceIds = [
 ];
 
 // Get price feeds
-const priceFeeds = await connection.getPriceFeeds("btc", "crypto");
+const priceFeeds = await connection.getPriceFeeds({
+  query: "btc",
+  filter: "crypto",
+});
 console.log(priceFeeds);
 
 // Latest price updates

+ 1 - 1
apps/hermes/client/js/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@pythnetwork/hermes-client",
-  "version": "1.3.0",
+  "version": "1.3.1",
   "description": "Pyth Hermes Client",
   "author": {
     "name": "Pyth Data Association"

+ 15 - 9
apps/hermes/client/js/src/HermesClient.ts

@@ -119,7 +119,7 @@ export class HermesClient {
     query?: string;
     filter?: string;
   }): Promise<PriceFeedMetadata[]> {
-    const url = new URL("v2/price_feeds", this.baseURL);
+    const url = this.buildURL("price_feeds");
     if (options) {
       this.appendUrlSearchParams(url, options);
     }
@@ -144,7 +144,7 @@ export class HermesClient {
     encoding?: EncodingType;
     parsed?: boolean;
   }): Promise<PublisherCaps> {
-    const url = new URL("v2/updates/publisher_stake_caps/latest", this.baseURL);
+    const url = this.buildURL("updates/publisher_stake_caps/latest");
     if (options) {
       this.appendUrlSearchParams(url, options);
     }
@@ -175,7 +175,7 @@ export class HermesClient {
       ignoreInvalidPriceIds?: boolean;
     }
   ): Promise<PriceUpdate> {
-    const url = new URL("v2/updates/price/latest", this.baseURL);
+    const url = this.buildURL("updates/price/latest");
     for (const id of ids) {
       url.searchParams.append("ids[]", id);
     }
@@ -211,7 +211,7 @@ export class HermesClient {
       ignoreInvalidPriceIds?: boolean;
     }
   ): Promise<PriceUpdate> {
-    const url = new URL(`v2/updates/price/${publishTime}`, this.baseURL);
+    const url = this.buildURL(`updates/price/${publishTime}`);
     for (const id of ids) {
       url.searchParams.append("ids[]", id);
     }
@@ -251,7 +251,7 @@ export class HermesClient {
       ignoreInvalidPriceIds?: boolean;
     }
   ): Promise<EventSource> {
-    const url = new URL("v2/updates/price/stream", this.baseURL);
+    const url = this.buildURL("updates/price/stream");
     ids.forEach((id) => {
       url.searchParams.append("ids[]", id);
     });
@@ -288,10 +288,7 @@ export class HermesClient {
       ignoreInvalidPriceIds?: boolean;
     }
   ): Promise<TwapsResponse> {
-    const url = new URL(
-      `v2/updates/twap/${window_seconds}/latest`,
-      this.baseURL
-    );
+    const url = this.buildURL(`updates/twap/${window_seconds}/latest`);
     for (const id of ids) {
       url.searchParams.append("ids[]", id);
     }
@@ -314,4 +311,13 @@ export class HermesClient {
       }
     });
   }
+
+  private buildURL(endpoint: string) {
+    return new URL(
+      `./v2/${endpoint}`,
+      // We ensure the `baseURL` ends with a `/` so that URL doesn't resolve the
+      // path relative to the parent.
+      `${this.baseURL}${this.baseURL.endsWith("/") ? "" : "/"}`
+    );
+  }
 }