Skip to content

Commit

Permalink
feat: improve function docs and types
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielforster committed Aug 29, 2024
1 parent 69fcc4f commit 22022ff
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/http/call.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
import axios, { type AxiosRequestConfig } from 'axios'

type Options = AxiosRequestConfig & { retryCount?: number, timeoutBetweenRetries?: number }
type Options = AxiosRequestConfig & {
retryCount?: number
timeoutBetweenRetries?: number
}

export async function callHttpWithRetry (url: string, options: Options) {
/*
* @description This function makes an HTTP request to the given URL with the given options.
* If the request fails, the function will retry the request up to the given number
* of times after the given timout.
*/
export async function callHttpWithRetry<T> (
url: string,
options: Options
): Promise<T | undefined> {
const { retryCount, timeoutBetweenRetries, ...axiosOptions } = options
const retries = retryCount ?? 2

for (let i = 0; i < retries; i++) {
try {
const { data } = await axios(url, axiosOptions)
return data
return data as T
} catch (error) {
if (i === retries - 1) {
throw error
Expand Down

0 comments on commit 22022ff

Please sign in to comment.