feat(SW-2859): Create trpc package * Add isEdge, safeTry and dataCache to new common package * Add eslint and move prettier config * Clean up tests * Create trpc package and move initialization * Move errors and a few procedures * Move telemetry to common package * Move tokenManager to common package * Add Sentry to procedures * Clean up procedures * Fix self-referencing imports * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * Fix lang imports Approved-by: Linus Flood
101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
import { router } from "@scandic-hotels/trpc"
|
|
import { contentstackExtendedProcedureUID } from "@scandic-hotels/trpc/procedures"
|
|
|
|
import { batchRequest } from "@/lib/graphql/batchRequest"
|
|
import {
|
|
GetContentPage,
|
|
GetContentPageBlocksBatch1,
|
|
GetContentPageBlocksBatch2,
|
|
} from "@/lib/graphql/Query/ContentPage/ContentPage.graphql"
|
|
|
|
import { contentPageSchema } from "./output"
|
|
import {
|
|
createChannel,
|
|
createPageType,
|
|
fetchContentPageRefs,
|
|
generatePageTags,
|
|
} from "./utils"
|
|
|
|
import 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 contentPageRefs = await fetchContentPageRefs(lang, uid)
|
|
|
|
if (!contentPageRefs) {
|
|
return null
|
|
}
|
|
|
|
const tags = generatePageTags(contentPageRefs, lang)
|
|
|
|
const getContentPageCounter = createCounter(
|
|
"trpc.contentstack",
|
|
"contentPage.get"
|
|
)
|
|
const metricsGetContentPage = getContentPageCounter.init({
|
|
lang,
|
|
uid,
|
|
})
|
|
|
|
metricsGetContentPage.start()
|
|
|
|
const contentPageRequest = await batchRequest<GetContentPageSchema>([
|
|
{
|
|
document: GetContentPage,
|
|
variables: { locale: lang, uid },
|
|
cacheOptions: {
|
|
key: `${tags.join(",")}:contentPage`,
|
|
ttl: "max",
|
|
},
|
|
},
|
|
|
|
{
|
|
document: GetContentPageBlocksBatch1,
|
|
variables: { locale: lang, uid },
|
|
cacheOptions: {
|
|
key: `${tags.join(",")}:contentPageBlocksBatch1`,
|
|
ttl: "max",
|
|
},
|
|
},
|
|
|
|
{
|
|
document: GetContentPageBlocksBatch2,
|
|
variables: { locale: lang, uid },
|
|
cacheOptions: {
|
|
key: `${tags.join(",")}:contentPageBlocksBatch2`,
|
|
ttl: "max",
|
|
},
|
|
},
|
|
])
|
|
|
|
const contentPage = contentPageSchema.safeParse(contentPageRequest.data)
|
|
if (!contentPage.success) {
|
|
metricsGetContentPage.validationError(contentPage.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetContentPage.success()
|
|
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: contentPage.data.content_page.system.uid,
|
|
domainLanguage: lang,
|
|
publishDate: contentPage.data.content_page.system.updated_at,
|
|
createDate: contentPage.data.content_page.system.created_at,
|
|
channel: createChannel(contentPage.data.content_page.system.uid),
|
|
pageType: createPageType(contentPage.data.content_page.system.uid),
|
|
pageName: contentPage.data.trackingProps.url,
|
|
siteSections: contentPage.data.trackingProps.url,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
contentPage: contentPage.data.content_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|