80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
import { Lang } from "@/constants/languages"
|
|
import { GetContentPage } from "@/lib/graphql/Query/ContentPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
|
|
|
import { generateTag } from "@/utils/generateTag"
|
|
import { makeImageVaultImage } from "@/utils/imageVault"
|
|
|
|
import { validateContentPageSchema } from "./output"
|
|
|
|
import { ImageVaultAsset } from "@/types/components/imageVault"
|
|
import {
|
|
TrackingChannelEnum,
|
|
TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import {
|
|
ContentPage,
|
|
ContentPageDataRaw,
|
|
} from "@/types/trpc/routers/contentstack/contentPage"
|
|
|
|
export const contentPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
// TODO: Refs request should be done when adding more data to this query
|
|
// which has references to other pages.
|
|
|
|
const response = await request<ContentPageDataRaw>(
|
|
GetContentPage,
|
|
{ locale: lang, uid },
|
|
{ cache: "force-cache", next: { tags: [generateTag(lang, uid)] } }
|
|
)
|
|
|
|
const { content_page } = response.data
|
|
if (!content_page) {
|
|
throw notFound(response)
|
|
}
|
|
|
|
const validatedContentPage = validateContentPageSchema.safeParse({
|
|
content_page: {
|
|
...content_page,
|
|
hero_image: makeImageVaultImage(content_page.hero_image),
|
|
},
|
|
})
|
|
|
|
if (!validatedContentPage.success) {
|
|
console.error(
|
|
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
|
|
)
|
|
console.error(validatedContentPage.error?.format())
|
|
return null
|
|
}
|
|
|
|
// Destructure hero_image and rename it to heroImage
|
|
const { hero_image: heroImage, ...restContentPage } =
|
|
validatedContentPage.data.content_page
|
|
|
|
// Construct the contentPage object with the correct structure
|
|
const contentPage: ContentPage = {
|
|
...restContentPage,
|
|
heroImage: heroImage as ImageVaultAsset | undefined,
|
|
}
|
|
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: contentPage.system.uid,
|
|
lang: contentPage.system.locale as Lang,
|
|
publishedDate: contentPage.system.updated_at,
|
|
createdDate: contentPage.system.created_at,
|
|
channel: TrackingChannelEnum["static-content-page"],
|
|
pageType: "staticcontentpage",
|
|
}
|
|
|
|
return {
|
|
contentPage,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|