Skip to content

Commit

Permalink
default with 3 tries and 10 sec timeout, no external options
Browse files Browse the repository at this point in the history
  • Loading branch information
dwdozier committed Jun 4, 2024
1 parent 66359f4 commit 843b536
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,52 @@ import {HttpResponse} from './gen/http-client'

import fetch from 'isomorphic-fetch'
import {env} from 'process'
import { clear } from 'console'

const USER_AGENT = `configure-action/${LIB_VERSION}`

export default async function fetchWithRetry(
url: RequestInfo, {headers, ...options}: RequestInit = {},
init?: RequestInit,
{timeoutInSeconds, tries } = {timeoutInSeconds: 10, tries: 3},
) : Promise<Response> {
let response: Response;
let controller: AbortController;

for (let tryCount = 0; tryCount < tries; tryCount++) {
let timeoutId;

try {
controller = new AbortController();
timeoutId = setTimeout(() => controller.abort(), timeoutInSeconds * 1000);
response = await fetch(url, {
signal: controller.signal,
headers: {
'User-Agent': USER_AGENT,
...headers
}, ...init});

clearTimeout(timeoutId);

if (response.ok) {
return response;
}
} catch (e) {
if (timeoutId) {
clearTimeout(timeoutId);
}

if (!(e instanceof DOMException) || e.name !== 'AbortError') {
throw e;
}
}
}

throw new Error(
`Failed to fetch ${url} after ${tries} tries with ${timeoutInSeconds} seconds timeout.`
);
}

export const configurefetch = (
url: RequestInfo,
/* istanbul ignore next */
Expand All @@ -34,7 +77,7 @@ type SecurityDataType = {apikey: string}
export function api(): Api<SecurityDataType> {
const api = new Api<SecurityDataType>({
baseUrl: core.getInput('server') || 'https://api.cloudtruth.io',
customFetch: configurefetch,
customFetch: fetchWithRetry,
securityWorker: (securityData: SecurityDataType | null) => {
return {
headers: {
Expand Down

0 comments on commit 843b536

Please sign in to comment.