Skip to content

feat(hermes-client): allow passing fetch options #2334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 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.1",
"version": "1.4.0",
"description": "Pyth Hermes Client",
"author": {
"name": "Pyth Data Association"
Expand Down Expand Up @@ -47,7 +47,7 @@
"openapi-zod-client": "^1.18.1",
"prettier": "^2.6.2",
"ts-jest": "^29.0.5",
"typescript": "^4.6.3",
"typescript": "catalog:",
"yargs": "^17.4.1"
},
"dependencies": {
Expand Down
63 changes: 36 additions & 27 deletions apps/hermes/client/js/src/HermesClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,17 @@
schema: z.ZodSchema<ResponseData>,
options?: RequestInit,
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
externalAbortController?: AbortController
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
): Promise<ResponseData> {
const controller = externalAbortController ?? new AbortController();
const { signal } = controller;
options = {
...options,
signal,
headers: { ...this.headers, ...options?.headers },
}; // Merge any existing options with the signal and headers

// Set a timeout to abort the request if it takes too long
const timeout = setTimeout(() => controller.abort(), this.timeout);

try {
const response = await fetch(url, options);
clearTimeout(timeout); // Clear the timeout if the request completes in time
const response = await fetch(url, {
...options,
signal: AbortSignal.any([
...(options?.signal ? [options.signal] : []),
AbortSignal.timeout(this.timeout),
]),
headers: { ...this.headers, ...options?.headers },
});
if (!response.ok) {
const errorBody = await response.text();
throw new Error(
Expand All @@ -91,7 +85,6 @@
const data = await response.json();
return schema.parse(data);
} catch (error) {
clearTimeout(timeout);
if (
retries > 0 &&
!(error instanceof Error && error.name === "AbortError")
Expand All @@ -115,17 +108,22 @@
*
* @returns Array of PriceFeedMetadata objects.
*/
async getPriceFeeds(options?: {
async getPriceFeeds({
fetchOptions,
...options
}: {
query?: string;
filter?: string;
}): Promise<PriceFeedMetadata[]> {
fetchOptions?: RequestInit;
} = {}): Promise<PriceFeedMetadata[]> {
const url = this.buildURL("price_feeds");
if (options) {
this.appendUrlSearchParams(url, options);
}
return await this.httpRequest(
url.toString(),
schemas.PriceFeedMetadata.array()
schemas.PriceFeedMetadata.array(),
fetchOptions
);
}

Expand All @@ -140,10 +138,14 @@
*
* @returns PublisherCaps object containing the latest publisher stake caps.
*/
async getLatestPublisherCaps(options?: {
async getLatestPublisherCaps({
fetchOptions,

Check warning on line 142 in apps/hermes/client/js/src/HermesClient.ts

View workflow job for this annotation

GitHub Actions / test

'fetchOptions' is assigned a value but never used
...options
}: {
encoding?: EncodingType;
parsed?: boolean;
}): Promise<PublisherCaps> {
fetchOptions?: RequestInit;
} = {}): Promise<PublisherCaps> {
const url = this.buildURL("updates/publisher_stake_caps/latest");
if (options) {
this.appendUrlSearchParams(url, options);
Expand Down Expand Up @@ -173,7 +175,8 @@
encoding?: EncodingType;
parsed?: boolean;
ignoreInvalidPriceIds?: boolean;
}
},
fetchOptions?: RequestInit
): Promise<PriceUpdate> {
const url = this.buildURL("updates/price/latest");
for (const id of ids) {
Expand All @@ -185,7 +188,7 @@
this.appendUrlSearchParams(url, transformedOptions);
}

return this.httpRequest(url.toString(), schemas.PriceUpdate);
return this.httpRequest(url.toString(), schemas.PriceUpdate, fetchOptions);
}

/**
Expand All @@ -209,7 +212,8 @@
encoding?: EncodingType;
parsed?: boolean;
ignoreInvalidPriceIds?: boolean;
}
},
fetchOptions?: RequestInit
): Promise<PriceUpdate> {
const url = this.buildURL(`updates/price/${publishTime}`);
for (const id of ids) {
Expand All @@ -221,7 +225,7 @@
this.appendUrlSearchParams(url, transformedOptions);
}

return this.httpRequest(url.toString(), schemas.PriceUpdate);
return this.httpRequest(url.toString(), schemas.PriceUpdate, fetchOptions);
}

/**
Expand Down Expand Up @@ -286,7 +290,8 @@
encoding?: EncodingType;
parsed?: boolean;
ignoreInvalidPriceIds?: boolean;
}
},
fetchOptions?: RequestInit
): Promise<TwapsResponse> {
const url = this.buildURL(`updates/twap/${window_seconds}/latest`);
for (const id of ids) {
Expand All @@ -298,7 +303,11 @@
this.appendUrlSearchParams(url, transformedOptions);
}

return this.httpRequest(url.toString(), schemas.TwapsResponse);
return this.httpRequest(
url.toString(),
schemas.TwapsResponse,
fetchOptions
);
}

private appendUrlSearchParams(
Expand Down
Loading
Loading