chore(pockethost): add configurable request timeout to tryFetch

This commit is contained in:
Ben Allfree 2024-07-30 09:42:24 -04:00
parent 77e91315fd
commit 6311870e21

View File

@ -2,10 +2,12 @@ import fetch, { Response } from 'node-fetch'
import { LoggerService } from '../common'
export const TRYFETCH_RETRY_MS = 50
export const TRYFETCH_TIMEOUT_MS = 500
export type TryFetchConfig = {
preflight: () => Promise<boolean>
retryMs: number
timeoutMs: number
}
/**
@ -25,9 +27,10 @@ export const tryFetch = async (
url: string,
config?: Partial<TryFetchConfig>,
) => {
const { preflight, retryMs }: TryFetchConfig = {
const { preflight, retryMs, timeoutMs }: TryFetchConfig = {
preflight: async () => true,
retryMs: TRYFETCH_RETRY_MS,
timeoutMs: TRYFETCH_TIMEOUT_MS,
...config,
}
const logger = LoggerService().create(`tryFetch`).breadcrumb(url)
@ -53,7 +56,7 @@ export const tryFetch = async (
}
try {
dbg(`Fetch: START`)
const res = await fetch(url, { signal: AbortSignal.timeout(500) })
const res = await fetch(url, { signal: AbortSignal.timeout(timeoutMs) })
dbg(`Fetch: SUCCESS`)
resolve(res)
} catch (e) {