feat: improve structure and error handling
This commit is contained in:
@@ -1,55 +0,0 @@
|
||||
import { DocumentNode, print } from "graphql"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
import { GetContentTypeUid } from "@/lib/graphql/Query/ContentTypeUid.graphql"
|
||||
|
||||
import { validateContentTypeUid } from "@/types/requests/contentTypeUid"
|
||||
|
||||
export enum PageTypeEnum {
|
||||
CurrentBlocksPage = "CurrentBlocksPage",
|
||||
LoyaltyPage = "LoyaltyPage",
|
||||
ContentPage = "ContentPage",
|
||||
}
|
||||
|
||||
export async function getContentTypeByPathName(
|
||||
pathNameWithoutLang: 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(GetContentTypeUid as DocumentNode),
|
||||
variables: {
|
||||
locale: lang,
|
||||
url: pathNameWithoutLang,
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
const pageTypeData = await result.json()
|
||||
|
||||
const validatedContentTypeUid = validateContentTypeUid.safeParse(
|
||||
pageTypeData.data
|
||||
)
|
||||
|
||||
if (!validatedContentTypeUid.success) {
|
||||
console.error("Bad validation for `validatedContentTypeUid`")
|
||||
console.error(validatedContentTypeUid.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const pageType = validatedContentTypeUid.data
|
||||
|
||||
if (pageType.all_content_page.total) {
|
||||
return PageTypeEnum.ContentPage
|
||||
} else if (pageType.all_loyalty_page.total) {
|
||||
return PageTypeEnum.LoyaltyPage
|
||||
} else if (pageType.all_current_blocks_page.total) {
|
||||
return PageTypeEnum.CurrentBlocksPage
|
||||
}
|
||||
}
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import type { Lang } from "@/constants/languages"
|
||||
* Function to generate tag for initial refs request
|
||||
*
|
||||
* @param lang Lang
|
||||
* @param identifier Should be uri for all pages and content_type_uid for
|
||||
* @param identifier Should be uid for all pages and content_type_uid for
|
||||
* everything else
|
||||
* @param affix possible extra value to add to string, e.g lang:identifier:breadcrumbs:refs
|
||||
* as it is the same entity as the actual page tag otherwise
|
||||
|
||||
Reference in New Issue
Block a user