Skip to content

Commit

Permalink
chore: reduce call stack in http client (#6934)
Browse files Browse the repository at this point in the history
* chore: reduce call stack in http client

* Fix binding

* Add private method to get request method

* Pass init to request method getter
  • Loading branch information
nflaig authored Jul 5, 2024
1 parent 0cb3998 commit dc5b68d
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/api/src/utils/client/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export class HttpClient implements IHttpClient {
if (init.retries > 0) {
return this.requestWithRetries(definition, args, init);
} else {
return this.requestFallbackToJson(definition, args, init);
return this.getRequestMethod(init)(definition, args, init);
}
} else {
return this.requestWithFallbacks(definition, args, localInit);
Expand Down Expand Up @@ -202,7 +202,7 @@ export class HttpClient implements IHttpClient {
}
const init = mergeInits(definition, urlInit, localInit);

const requestMethod = (init.retries > 0 ? this.requestWithRetries : this.requestFallbackToJson).bind(this);
const requestMethod = init.retries > 0 ? this.requestWithRetries.bind(this) : this.getRequestMethod(init);

requestMethod(definition, args, init).then(
async (res) => {
Expand Down Expand Up @@ -273,10 +273,11 @@ export class HttpClient implements IHttpClient {
): Promise<ApiResponse<E>> {
const {retries, retryDelay, signal} = init;
const routeId = definition.operationId;
const requestMethod = this.getRequestMethod(init);

return retry(
async (attempt) => {
const res = await this.requestFallbackToJson(definition, args, init);
const res = await requestMethod(definition, args, init);
if (!res.ok && attempt <= retries) {
throw res.error();
}
Expand Down Expand Up @@ -395,6 +396,10 @@ export class HttpClient implements IHttpClient {
abortSignals.forEach((s) => s?.removeEventListener("abort", onSignalAbort));
}
}

private getRequestMethod(init: ApiRequestInitRequired): typeof this._request {
return init.requestWireFormat === WireFormat.ssz ? this.requestFallbackToJson.bind(this) : this._request.bind(this);
}
}

function mergeInits<E extends Endpoint>(
Expand Down

0 comments on commit dc5b68d

Please sign in to comment.