Skip to content

Commit

Permalink
Switch to factory
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Sep 20, 2024
1 parent b582c0a commit 6379d37
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions oxide-openapi-gen-ts/src/client/static/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export interface FullParams extends FetchParams {
method?: string;
}

export type RetryHandler = (url: RequestInfo | URL, init: RequestInit, err: any) => boolean;
export type RetryHandler = (err: any) => boolean;
export type RetryHandlerFactory = (url: RequestInfo | URL, init: RequestInit) => RetryHandler;

export interface ApiConfig {
/**
Expand All @@ -127,14 +128,14 @@ export interface ApiConfig {
host?: string;
token?: string;
baseParams?: FetchParams;
retryHandler?: RetryHandler;
retryHandler?: RetryHandlerFactory;
}

export class HttpClient {
host: string;
token?: string;
baseParams: FetchParams;
retryHandler: RetryHandler;
retryHandler: RetryHandlerFactory;

constructor({ host = "", baseParams = {}, token, retryHandler }: ApiConfig = {}) {
this.host = host;
Expand All @@ -145,7 +146,7 @@ export class HttpClient {
headers.append("Authorization", `Bearer ${token}`);
}
this.baseParams = mergeParams({ headers }, baseParams);
this.retryHandler = retryHandler ? retryHandler : () => false;
this.retryHandler = retryHandler ? retryHandler : () => () => false;
}

public async request<Data>({
Expand All @@ -160,15 +161,15 @@ export class HttpClient {
...mergeParams(this.baseParams, fetchParams),
body: JSON.stringify(snakeify(body), replacer),
};
return fetchWithRetry(fetch, url, init, this.retryHandler)
return fetchWithRetry(fetch, url, init, this.retryHandler(url, init))
}
}

export async function fetchWithRetry<Data>(fetch: any, url: string, init: RequestInit, retry: RetryHandler): Promise<ApiResult<Data>> {
try {
return handleResponse(await fetch(url, init));
} catch (err) {
if (retry(url, init, err)) {
if (retry(err)) {
return await fetchWithRetry(fetch, url, init, retry)
}

Expand Down

0 comments on commit 6379d37

Please sign in to comment.