fix: break out content type uid fetch to utils function
This commit is contained in:
45
utils/contentType.ts
Normal file
45
utils/contentType.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { DocumentNode } from "graphql"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { env } from "@/env/server"
|
||||
import GetContentTypeUid from "@/lib/graphql/Query/ContentTypeUid.graphql"
|
||||
|
||||
import type { GetContentTypeUidType } from "@/types/requests/contentTypeUid"
|
||||
|
||||
export enum PageTypeEnum {
|
||||
CurrentBlocksPage = "CurrentBlocksPage",
|
||||
LoyaltyPage = "LoyaltyPage",
|
||||
ContentPage = "ContentPage",
|
||||
}
|
||||
|
||||
export async function getContentTypeByPathName(
|
||||
pathNameWithoutLang: string,
|
||||
lang = Lang.en
|
||||
) {
|
||||
const print = (await import("graphql/language/printer")).print
|
||||
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 pageType = pageTypeData.data as GetContentTypeUidType
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user