Files
web/server/routers/contentstack/contentPage/query.ts
Erik Tiekstra a0053335a6 chore: cleanup
2024-08-22 09:49:02 +02:00

66 lines
1.7 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 { makeImageVaultImage } from "@/utils/imageVault"
import {
ContentPage,
ContentPageDataRaw,
validateContentPageSchema,
} from "./output"
import {
TrackingChannelEnum,
TrackingSDKPageData,
} from "@/types/components/tracking"
export const contentPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
const response = await request<ContentPageDataRaw>(GetContentPage, {
locale: lang,
uid,
})
if (!response.data) {
throw notFound(response)
}
const validatedContentPage = validateContentPageSchema.safeParse(
response.data
)
if (!validatedContentPage.success) {
console.error(
`Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})`
)
console.error(validatedContentPage.error)
return null
}
const contentPageData = validatedContentPage.data.content_page
const contentPage: ContentPage = {
...contentPageData,
hero_image: makeImageVaultImage(contentPageData.hero_image),
}
const tracking: TrackingSDKPageData = {
pageId: contentPageData.system.uid,
lang: contentPageData.system.locale as Lang,
publishedDate: contentPageData.system.updated_at,
createdDate: contentPageData.system.created_at,
channel: TrackingChannelEnum["static-content-page"],
pageType: "staticcontentpage",
}
return {
contentPage,
tracking,
}
}),
})