Merged in feat/sw-3006-timeout-fetch (pull request #2335)

feat(SW-3006): added default timeout to all requests

* feat(sw-3006): added default timeout to all requests

* Fixed spreading


Approved-by: Joakim Jäderberg
This commit is contained in:
Linus Flood
2025-06-11 12:39:58 +00:00
parent d560ac0fca
commit 3b9d01af9d
14 changed files with 46 additions and 29 deletions

View File

@@ -21,7 +21,7 @@ const defaultOptions: RequestInit = {
}
const wrappedFetch = fetchRetry(fetch, {
retries: 3,
retries: 2,
retryDelay: function (attempt) {
return Math.pow(2, attempt) * 150 // 150, 300, 600
},
@@ -35,10 +35,11 @@ export async function get(
const url = new URL(env.API_BASEURL)
url.pathname = endpoint
url.search = new URLSearchParams(params).toString()
return wrappedFetch(
url,
merge.all([defaultOptions, { method: "GET" }, options])
)
return wrappedFetch(url, {
...merge.all([defaultOptions, { method: "GET" }, options]),
signal: AbortSignal.timeout(15_000),
})
}
export async function patch(
@@ -50,14 +51,14 @@ export async function patch(
const url = new URL(env.API_BASEURL)
url.pathname = endpoint
url.search = new URLSearchParams(params).toString()
return wrappedFetch(
url,
merge.all([
return wrappedFetch(url, {
...merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "PATCH" },
requestOptions,
])
)
]),
signal: AbortSignal.timeout(15_000),
})
}
export async function post(
@@ -69,14 +70,14 @@ export async function post(
const url = new URL(env.API_BASEURL)
url.pathname = endpoint
url.search = new URLSearchParams(params).toString()
return wrappedFetch(
url,
merge.all([
return wrappedFetch(url, {
...merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "POST" },
requestOptions,
])
)
]),
signal: AbortSignal.timeout(15_000),
})
}
export async function put(
@@ -88,14 +89,14 @@ export async function put(
const url = new URL(env.API_BASEURL)
url.pathname = endpoint
url.search = new URLSearchParams(params).toString()
return wrappedFetch(
url,
merge.all([
return wrappedFetch(url, {
...merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "PUT" },
requestOptions,
])
)
]),
signal: AbortSignal.timeout(15_000),
})
}
export async function remove(
@@ -107,12 +108,12 @@ export async function remove(
const url = new URL(env.API_BASEURL)
url.pathname = endpoint
url.search = new URLSearchParams(params).toString()
return wrappedFetch(
url,
merge.all([
return wrappedFetch(url, {
...merge.all([
defaultOptions,
{ body: JSON.stringify(body), method: "DELETE" },
requestOptions,
])
)
]),
signal: AbortSignal.timeout(15_000),
})
}

View File

@@ -73,12 +73,15 @@ function internalRequest<T>(
params?: RequestInit
) {
const wrappedFetch = fetchRetry(fetch, {
retries: 3,
retries: 2,
retryDelay: function (attempt) {
return Math.pow(2, attempt) * 150 // 150, 300, 600
},
})
return wrappedFetch(url, params)
return wrappedFetch(url, {
...params,
signal: AbortSignal.timeout(15_000),
})
}),
})