36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { ContentstackLivePreview } from "@contentstack/live-preview-utils"
|
|
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"
|
|
|
|
export async function request<T>(
|
|
query: string | DocumentNode,
|
|
variables?: {},
|
|
params?: RequestInit
|
|
): Promise<Data<T>> {
|
|
const previewHash = ContentstackLivePreview.hash
|
|
|
|
const cmsUrl = previewHash ? env.CMS_PREVIEW_URL : env.CMS_URL
|
|
|
|
const client = new GraphQLClient(cmsUrl, {
|
|
fetch: cache(async function (url: URL | RequestInfo, params?: RequestInit) {
|
|
const wrappedFetch = fetchRetry(fetch, {
|
|
retries: 3,
|
|
retryDelay: function (attempt, error, response) {
|
|
return Math.pow(2, attempt) * 150 // 150, 300, 600
|
|
},
|
|
})
|
|
return wrappedFetch(url, params)
|
|
}),
|
|
})
|
|
|
|
return _request(client, query, variables, params)
|
|
}
|