Skip to content

Commit

Permalink
Add private method to get request method
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jul 5, 2024
1 parent 89330d8 commit 6cd7f08
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions packages/api/src/utils/client/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,8 @@ export class HttpClient implements IHttpClient {

if (init.retries > 0) {
return this.requestWithRetries(definition, args, init);
} else if (init.requestWireFormat === WireFormat.ssz) {
return this.requestFallbackToJson(definition, args, init);
} else {
return this._request(definition, args, init);
return this.getRequestMethod(init.requestWireFormat)(definition, args, init);
}
} else {
return this.requestWithFallbacks(definition, args, localInit);
Expand Down Expand Up @@ -203,13 +201,8 @@ export class HttpClient implements IHttpClient {
}
const init = mergeInits(definition, urlInit, localInit);

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

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

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

private getRequestMethod(requestFormat: `${WireFormat}`): typeof this._request {
return requestFormat === WireFormat.ssz ? this.requestFallbackToJson.bind(this) : this._request.bind(this);
}
}

function mergeInits<E extends Endpoint>(
Expand Down

0 comments on commit 6cd7f08

Please sign in to comment.