Skip to content

Commit

Permalink
Merge pull request #2286 from pyth-network/cprussin/fix-hermes-client
Browse files Browse the repository at this point in the history
fix(hermes-client): ensure relative urls are built properly
  • Loading branch information
cprussin authored Jan 22, 2025
2 parents 4e034a6 + 741a8bd commit cdaf99e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
5 changes: 4 additions & 1 deletion apps/hermes/client/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion apps/hermes/client/js/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
24 changes: 15 additions & 9 deletions apps/hermes/client/js/src/HermesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
}
Expand All @@ -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("/") ? "" : "/"}`
);
}
}

0 comments on commit cdaf99e

Please sign in to comment.