feat(WEB-300): add retry for fetch calls

This commit is contained in:
Michael Zetterberg
2024-07-24 10:47:55 +02:00
parent 80c80fe1bd
commit ffa76cd8a2
4 changed files with 22 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
import merge from "deepmerge"
import fetchRetry from "fetch-retry"
import { env } from "@/env/server"
@@ -18,6 +19,13 @@ const defaultOptions: RequestInit = {
mode: "cors",
}
const fetch = fetchRetry(global.fetch, {
retries: 3,
retryDelay: function (attempt, error, response) {
return Math.pow(2, attempt) * 150 // 150, 300, 600
},
})
export async function get(
endpoint: Endpoint,
options: RequestOptionsWithOutBody,

View File

@@ -1,3 +1,4 @@
import fetchRetry from "fetch-retry"
import { DocumentNode } from "graphql"
import { GraphQLClient } from "graphql-request"
import { cache } from "react"
@@ -13,6 +14,12 @@ const client = new GraphQLClient(env.CMS_URL, {
url: URL | RequestInfo,
params: RequestInit | undefined
) {
const fetch = fetchRetry(global.fetch, {
retries: 3,
retryDelay: function (attempt, error, response) {
return Math.pow(2, attempt) * 150 // 150, 300, 600
},
})
return fetch(url, params)
}),
})

6
package-lock.json generated
View File

@@ -26,6 +26,7 @@
"clean-deep": "^3.4.0",
"dayjs": "^1.11.10",
"deepmerge": "^4.3.1",
"fetch-retry": "^6.0.0",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0",
"graphql-tag": "^2.12.6",
@@ -9721,6 +9722,11 @@
"pend": "~1.2.0"
}
},
"node_modules/fetch-retry": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/fetch-retry/-/fetch-retry-6.0.0.tgz",
"integrity": "sha512-BUFj1aMubgib37I3v4q78fYo63Po7t4HUPTpQ6/QE6yK6cIQrP+W43FYToeTEyg5m2Y7eFUtijUuAv/PDlWuag=="
},
"node_modules/figures": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz",

View File

@@ -42,6 +42,7 @@
"clean-deep": "^3.4.0",
"dayjs": "^1.11.10",
"deepmerge": "^4.3.1",
"fetch-retry": "^6.0.0",
"graphql": "^16.8.1",
"graphql-request": "^6.1.0",
"graphql-tag": "^2.12.6",