34 lines
855 B
TypeScript
34 lines
855 B
TypeScript
import fetchRetry from "fetch-retry"
|
|
import { DocumentNode } from "graphql"
|
|
import { GraphQLClient } from "graphql-request"
|
|
import { cache } from "react"
|
|
|
|
import { env } from "@/env/server"
|
|
|
|
import { request as _request } from "./_request"
|
|
|
|
import { Data } from "@/types/request"
|
|
|
|
const client = new GraphQLClient(env.CMS_URL, {
|
|
fetch: cache(async function (
|
|
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)
|
|
}),
|
|
})
|
|
|
|
export async function request<T>(
|
|
query: string | DocumentNode,
|
|
variables?: {},
|
|
next?: NextFetchRequestConfig
|
|
): Promise<Data<T>> {
|
|
return _request(client, query, variables, next)
|
|
}
|