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:
@@ -55,6 +55,7 @@ export async function GET(
|
||||
).toString(),
|
||||
client_id: env.SAS_AUTH_CLIENTID,
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -36,6 +36,7 @@ async function refreshTokens(token: JWT) {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
method: "POST",
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
|
||||
const new_tokens = await response.json()
|
||||
|
||||
@@ -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),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
})
|
||||
}),
|
||||
})
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ async function fetchAndCacheRedirect(lang: Lang, pathname: string) {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -39,7 +40,7 @@ async function fetchAndCacheRedirect(lang: Lang, pathname: string) {
|
||||
return null
|
||||
},
|
||||
// longer once tested
|
||||
"1m"
|
||||
"1d"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ export default async (request: Request, _context: Context) => {
|
||||
)
|
||||
const response = await fetch(url, {
|
||||
headers,
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
|
||||
@@ -18,6 +18,7 @@ export default async (request: Request, _context: Context) => {
|
||||
)
|
||||
const response = await fetch(url, {
|
||||
headers,
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
|
||||
@@ -122,6 +122,7 @@ async function callWarmup(key: WarmupFunctionsKey, context: Context) {
|
||||
cache: "no-store",
|
||||
Authorization: `Bearer ${warmupToken}`,
|
||||
},
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -11,6 +11,7 @@ export async function warmupHotelDataOnLang(lang: Lang) {
|
||||
`${PUBLIC_URL}/api/hoteldata?lang=${lang}`,
|
||||
{
|
||||
headers: { cache: "no-store" },
|
||||
signal: AbortSignal.timeout(30_000),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@@ -880,7 +880,9 @@ export const hotelQueryRouter = router({
|
||||
`coordinates:${address}`,
|
||||
async function () {
|
||||
const url = `https://maps.googleapis.com/maps/api/geocode/json?address=${encodeURIComponent(address)}&key=${apiKey}`
|
||||
const response = await fetch(url)
|
||||
const response = await fetch(url, {
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
const data = await response.json()
|
||||
|
||||
if (data.status !== "OK") {
|
||||
|
||||
@@ -32,6 +32,7 @@ export const jobylonQueryRouter = router({
|
||||
async () => {
|
||||
const response = await fetch(url, {
|
||||
cache: "no-cache",
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
@@ -110,6 +110,7 @@ async function fetchRequestOtp({ sasAuthToken }: { sasAuthToken: string }) {
|
||||
body: JSON.stringify({
|
||||
referenceId: uuidv4(),
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -87,6 +87,7 @@ async function fetchVerifyOtp(input: z.infer<typeof inputSchema>) {
|
||||
otpCode: input.otp,
|
||||
databaseUUID: databaseUUID,
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
@@ -91,6 +91,7 @@ async function fetchServiceToken(scopes: string[]) {
|
||||
client_secret: env.CURITY_CLIENT_SECRET_SERVICE,
|
||||
scope: scopes.join(" "),
|
||||
}),
|
||||
signal: AbortSignal.timeout(15_000),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
|
||||
Reference in New Issue
Block a user