120 lines
3.1 KiB
TypeScript
120 lines
3.1 KiB
TypeScript
import { Lang } from "@/constants/languages"
|
|
import {
|
|
GetContentPage,
|
|
GetContentPageBlocksBatch1,
|
|
GetContentPageBlocksBatch2,
|
|
} from "@/lib/graphql/Query/ContentPage/ContentPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
|
|
|
import { contentPageSchema } from "./output"
|
|
import {
|
|
fetchContentPageRefs,
|
|
generatePageTags,
|
|
getContentPageCounter,
|
|
validateContentPageRefs,
|
|
} from "./utils"
|
|
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import type { GetContentPageSchema } from "@/types/trpc/routers/contentstack/contentPage"
|
|
|
|
export const contentPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const contentPageRefsData = await fetchContentPageRefs(lang, uid)
|
|
|
|
const contentPageRefs = validateContentPageRefs(
|
|
contentPageRefsData,
|
|
lang,
|
|
uid
|
|
)
|
|
if (!contentPageRefs) {
|
|
return null
|
|
}
|
|
|
|
const tags = generatePageTags(contentPageRefs, lang)
|
|
|
|
getContentPageCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.contentPage start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
const [mainResponse, blocksResponse1, blocksResponse2] = await Promise.all([
|
|
request<GetContentPageSchema>(
|
|
GetContentPage,
|
|
{ locale: lang, uid },
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
),
|
|
request<GetContentPageSchema>(
|
|
GetContentPageBlocksBatch1,
|
|
{ locale: lang, uid },
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
),
|
|
request<GetContentPageSchema>(
|
|
GetContentPageBlocksBatch2,
|
|
{ locale: lang, uid },
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
),
|
|
])
|
|
|
|
const responseData = {
|
|
...mainResponse.data,
|
|
content_page: {
|
|
...mainResponse.data.content_page,
|
|
blocks: [
|
|
blocksResponse1.data.content_page.blocks,
|
|
blocksResponse2.data.content_page.blocks,
|
|
]
|
|
.flat(2)
|
|
.filter((obj) => !(obj && Object.keys(obj).length < 2)), // Remove empty objects and objects with only typename
|
|
},
|
|
}
|
|
|
|
const contentPage = contentPageSchema.safeParse(responseData)
|
|
|
|
if (!contentPage.success) {
|
|
console.error(
|
|
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
|
)
|
|
console.error(contentPage.error?.format())
|
|
return null
|
|
}
|
|
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: contentPage.data.content_page.system.uid,
|
|
lang: contentPage.data.content_page.system.locale as Lang,
|
|
publishedDate: contentPage.data.content_page.system.updated_at,
|
|
createdDate: contentPage.data.content_page.system.created_at,
|
|
channel: TrackingChannelEnum["static-content-page"],
|
|
pageType: "staticcontentpage",
|
|
}
|
|
|
|
return {
|
|
contentPage: contentPage.data.content_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|