mirror of
https://github.com/pockethost/pockethost.git
synced 2025-03-30 15:08:30 +00:00
chore(pockethost): add configurable request timeout to tryFetch
This commit is contained in:
parent
77e91315fd
commit
6311870e21
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user