feat: add edge request function

This commit is contained in:
Christel Westerberg
2024-06-03 15:18:00 +02:00
parent f9acabce1f
commit 74b05ed6a0
11 changed files with 122 additions and 95 deletions

View File

@@ -1,31 +1,23 @@
import { DocumentNode, print } from "graphql"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
import { edgeRequest } from "@/lib/graphql/edgeRequest"
import { ResolveEntryByUrl } from "@/lib/graphql/Query/ResolveEntry.graphql"
import { internalServerError } from "@/server/errors/next"
import { validateEntryResolveSchema } from "@/types/requests/entry"
export async function resolve(url: string, lang = Lang.en) {
const result = await fetch(env.CMS_URL, {
method: "POST",
headers: {
access_token: env.CMS_ACCESS_TOKEN,
"Content-Type": "application/json",
const response = await edgeRequest(
ResolveEntryByUrl,
{
locale: lang,
url,
},
body: JSON.stringify({
query: print(ResolveEntryByUrl as DocumentNode),
variables: {
locale: lang,
url,
},
}),
})
{
revalidate: 3600,
}
)
const { data } = await result.json()
const validatedData = validateEntryResolveSchema.safeParse(data)
const validatedData = validateEntryResolveSchema.safeParse(response.data)
if (!validatedData.success) {
throw internalServerError(validatedData.error)