feat: improve structure and error handling

This commit is contained in:
Michael Zetterberg
2024-05-14 15:55:46 +02:00
parent 01587d7fd5
commit f5108d1a8e
104 changed files with 1505 additions and 1570 deletions
+50
View File
@@ -0,0 +1,50 @@
import { DocumentNode, print } from "graphql"
import { Lang } from "@/constants/languages"
import { env } from "@/env/server"
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",
},
body: JSON.stringify({
query: print(ResolveEntryByUrl as DocumentNode),
variables: {
locale: lang,
url,
},
}),
})
const { data } = await result.json()
const validatedData = validateEntryResolveSchema.safeParse(data)
if (!validatedData.success) {
console.error("Bad validation for `validateContentTypeDataSchema`")
console.error(validatedData.error)
throw internalServerError()
}
for (const value of Object.values(validatedData.data)) {
if (value.total) {
const { content_type_uid, uid } = value.items[0].system
return {
contentType: content_type_uid.replaceAll("_", "-"),
uid,
}
}
}
return {
contentType: null,
uid: null,
}
}