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
88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { logger } from "@scandic-hotels/common/logger"
|
|
|
|
import { BlocksEnums } from "../../../../types/blocksEnum"
|
|
import { transformedVideoSchema } from "../video"
|
|
|
|
const cardStyleSchema = z
|
|
.string()
|
|
.transform((val): "primary-1" | "primary-2" => {
|
|
if (val === "primary-1" || val === "primary-2") {
|
|
return val
|
|
}
|
|
return "primary-1"
|
|
})
|
|
|
|
export const videoQuoteCardSchema = z.object({
|
|
__typename: z.literal("VideoQuoteCard"),
|
|
video: transformedVideoSchema,
|
|
style: cardStyleSchema,
|
|
quote: z.string(),
|
|
author: z.string(),
|
|
author_description: z.string().nullish(),
|
|
})
|
|
|
|
export const videoTextCardSchema = z.object({
|
|
__typename: z.literal("VideoTextCard"),
|
|
video: transformedVideoSchema,
|
|
style: cardStyleSchema,
|
|
heading: z.string(),
|
|
text: z.string().nullish(),
|
|
})
|
|
|
|
export const videoCardSchema = z.object({
|
|
typename: z
|
|
.literal(BlocksEnums.block.VideoCard)
|
|
.default(BlocksEnums.block.VideoCard),
|
|
video_card: z
|
|
.object({
|
|
video_cardConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.discriminatedUnion("__typename", [
|
|
videoQuoteCardSchema,
|
|
videoTextCardSchema,
|
|
]),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
.transform((data) => {
|
|
const videoCard = data.video_cardConnection.edges[0]?.node
|
|
if (!videoCard?.video) {
|
|
return null
|
|
}
|
|
|
|
const { __typename, style, video } = videoCard
|
|
|
|
switch (__typename) {
|
|
case "VideoTextCard": {
|
|
const { heading, text } = videoCard
|
|
return {
|
|
variant: "text" as const,
|
|
style,
|
|
video,
|
|
heading,
|
|
text: text || undefined,
|
|
}
|
|
}
|
|
case "VideoQuoteCard": {
|
|
const { quote, author, author_description } = videoCard
|
|
return {
|
|
variant: "quote" as const,
|
|
style,
|
|
video,
|
|
quote,
|
|
author,
|
|
authorDescription: author_description || undefined,
|
|
}
|
|
}
|
|
default:
|
|
const type: never = __typename
|
|
logger.error(`Unsupported content type given: ${type}`)
|
|
return null
|
|
}
|
|
}),
|
|
})
|