Feat(SW-3708): refactor contentstack fetching (removing all refs) and cache invalidation * Remove all REFS * Revalidate correct language * PR fixes * PR fixes * Throw when errors from contentstack api Approved-by: Joakim Jäderberg
104 lines
3.0 KiB
TypeScript
104 lines
3.0 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { transformedImageVaultAssetSchema } from "@scandic-hotels/common/utils/imageVault"
|
|
|
|
import { CollectionPageEnum } from "../../../types/collectionPage"
|
|
import { discriminatedUnionArray } from "../../../utils/discriminatedUnion"
|
|
import { cardsGridSchema } from "../schemas/blocks/cardsGrid"
|
|
import { dynamicContentSchema as blockDynamicContentSchema } from "../schemas/blocks/dynamicContent"
|
|
import { shortcutsSchema } from "../schemas/blocks/shortcuts"
|
|
import { uspGridSchema } from "../schemas/blocks/uspGrid"
|
|
import { videoCardSchema } from "../schemas/blocks/videoCard"
|
|
import { linkAndTitleSchema } from "../schemas/linkConnection"
|
|
import { internalOrExternalLinkSchema } from "../schemas/pageLinks"
|
|
import { systemSchema } from "../schemas/system"
|
|
import { transformedVideoSchema } from "../schemas/video"
|
|
|
|
// Block schemas
|
|
export const collectionPageCards = z
|
|
.object({
|
|
__typename: z.literal(CollectionPageEnum.ContentStack.blocks.CardsGrid),
|
|
})
|
|
.merge(cardsGridSchema)
|
|
|
|
export const collectionPageShortcuts = z
|
|
.object({
|
|
__typename: z.literal(CollectionPageEnum.ContentStack.blocks.Shortcuts),
|
|
})
|
|
.merge(shortcutsSchema)
|
|
|
|
export const collectionPageUspGrid = z
|
|
.object({
|
|
__typename: z.literal(CollectionPageEnum.ContentStack.blocks.UspGrid),
|
|
})
|
|
.merge(uspGridSchema)
|
|
|
|
export const collectionPageDynamicContent = z
|
|
.object({
|
|
__typename: z.literal(
|
|
CollectionPageEnum.ContentStack.blocks.DynamicContent
|
|
),
|
|
})
|
|
.merge(blockDynamicContentSchema)
|
|
|
|
export const collectionPageVideoCard = z
|
|
.object({
|
|
__typename: z.literal(CollectionPageEnum.ContentStack.blocks.VideoCard),
|
|
})
|
|
.merge(videoCardSchema)
|
|
|
|
export const blocksSchema = z.discriminatedUnion("__typename", [
|
|
collectionPageCards,
|
|
collectionPageDynamicContent,
|
|
collectionPageShortcuts,
|
|
collectionPageUspGrid,
|
|
collectionPageVideoCard,
|
|
])
|
|
|
|
const navigationLinksSchema = z
|
|
.array(linkAndTitleSchema)
|
|
.nullable()
|
|
.transform((data) => {
|
|
if (!data) {
|
|
return null
|
|
}
|
|
|
|
return data
|
|
.filter((item) => !!item.link)
|
|
.map((item) => ({
|
|
url: item.link!.url,
|
|
title: item.title || item.link!.title,
|
|
}))
|
|
})
|
|
|
|
// Content Page Schema and types
|
|
export const collectionPageSchema = z.object({
|
|
collection_page: z.object({
|
|
hero_image: transformedImageVaultAssetSchema,
|
|
hero_video: transformedVideoSchema,
|
|
blocks: discriminatedUnionArray(blocksSchema.options).nullable(),
|
|
title: z.string(),
|
|
header: z.object({
|
|
heading: z.string(),
|
|
preamble: z.string(),
|
|
top_primary_button: internalOrExternalLinkSchema.nullish(),
|
|
navigation_links: navigationLinksSchema,
|
|
}),
|
|
meeting_package: z
|
|
.object({
|
|
show_widget: z.boolean(),
|
|
location: z.string(),
|
|
})
|
|
.nullable(),
|
|
system: systemSchema.merge(
|
|
z.object({
|
|
created_at: z.string(),
|
|
updated_at: z.string(),
|
|
})
|
|
),
|
|
}),
|
|
trackingProps: z.object({
|
|
url: z.string(),
|
|
}),
|
|
})
|