Skip to content

Commit

Permalink
feat(WebHttpClient): add optional fetch parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-titarenko committed Jan 27, 2023
1 parent 7fe8ca4 commit 8761db4
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/clients/http/WebHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export type WebHttpClientOptions = {
*/
transformRequestUrl?: TransformRequestUrl

/**
* Optional Fetch API implementation, could be useful for testing.
*/
fetch?: (url: string, init?: RequestInit) => Promise<Response>

/**
* The number of retries that the client attempt to do on failure.
*/
Expand All @@ -35,6 +40,7 @@ export type WebHttpClientOptions = {
*/
export function makeWebHttpClient(options: WebHttpClientOptions = {}): HttpClient {
const transformRequestUrl = options.transformRequestUrl ?? DefaultTransformRequestUrl
const fetchImpl = options.fetch ?? globalThis.fetch
const retriesCount = options.retriesCount ?? 3

/**
Expand All @@ -50,7 +56,7 @@ export function makeWebHttpClient(options: WebHttpClientOptions = {}): HttpClien
async function fetchWithRetry(url: string, options: RequestInit, n = 1): Promise<Response> {
try {
const targetUrl = transformRequestUrl(url, Boolean(options.credentials));
return await fetch(targetUrl, options)
return await fetchImpl(targetUrl, options)
} catch (err) {
if (n <= 1) {
throw err
Expand Down

0 comments on commit 8761db4

Please sign in to comment.