262 lines
6.7 KiB
TypeScript
262 lines
6.7 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
|
|
import {
|
|
JoinLoyaltyContactTypenameEnum,
|
|
LoyaltyBlocksTypenameEnum,
|
|
LoyaltyComponentEnum,
|
|
SidebarTypenameEnum,
|
|
} from "@/types/components/loyalty/enums"
|
|
import { Embeds } from "@/types/requests/embeds"
|
|
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
|
import { RTEDocument } from "@/types/rte/node"
|
|
|
|
const loyaltyPageBlockCardGrid = z.object({
|
|
__typename: z.literal(LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid),
|
|
card_grid: z.object({
|
|
title: z.string().optional(),
|
|
subtitle: z.string().optional(),
|
|
cards: z.array(
|
|
z.object({
|
|
title: z.string().optional(),
|
|
subtitle: z.string().optional(),
|
|
referenceConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
system: z.object({
|
|
uid: z.string(),
|
|
}),
|
|
url: z.string(),
|
|
title: z.string(),
|
|
__typename: z.string(),
|
|
}),
|
|
})
|
|
),
|
|
totalCount: z.number(),
|
|
}),
|
|
open_in_new_tab: z.boolean(),
|
|
cta_text: z.string().optional(),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
const loyaltyPageDynamicContent = z.object({
|
|
__typename: z.literal(
|
|
LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent
|
|
),
|
|
dynamic_content: z.object({
|
|
title: z.string().optional(),
|
|
subtitle: z.string().optional(),
|
|
component: z.nativeEnum(LoyaltyComponentEnum),
|
|
link: z.object({
|
|
text: z.string().optional(),
|
|
pageConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
system: z.object({
|
|
uid: z.string(),
|
|
locale: z.nativeEnum(Lang),
|
|
}),
|
|
url: z.string(),
|
|
title: z.string(),
|
|
}),
|
|
})
|
|
),
|
|
totalCount: z.number(),
|
|
}),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const loyaltyPageShortcuts = z.object({
|
|
__typename: z.literal(LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksShortcuts),
|
|
shortcuts: z.object({
|
|
title: z.string().optional(),
|
|
preamble: z.string().optional(),
|
|
shortcuts: z.array(
|
|
z.object({
|
|
linkConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
system: z.object({
|
|
uid: z.string(),
|
|
locale: z.nativeEnum(Lang),
|
|
}),
|
|
url: z.string(),
|
|
web: z
|
|
.object({
|
|
original_url: z.string().optional(),
|
|
})
|
|
.optional(),
|
|
title: z.string(),
|
|
}),
|
|
})
|
|
),
|
|
totalCount: z.number(),
|
|
}),
|
|
text: z.string().optional(),
|
|
open_in_new_tab: z.boolean(),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
const loyaltyPageBlockTextContent = z.object({
|
|
__typename: z.literal(LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(z.any()),
|
|
totalCount: z.number(),
|
|
}),
|
|
json: z.any(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const loyaltyPageBlockItem = z.discriminatedUnion("__typename", [
|
|
loyaltyPageBlockCardGrid,
|
|
loyaltyPageDynamicContent,
|
|
loyaltyPageBlockTextContent,
|
|
loyaltyPageShortcuts,
|
|
])
|
|
|
|
const loyaltyPageSidebarTextContent = z.object({
|
|
__typename: z.literal(SidebarTypenameEnum.LoyaltyPageSidebarContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(z.any()),
|
|
totalCount: z.number(),
|
|
}),
|
|
json: z.any(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const loyaltyPageJoinLoyaltyContact = z.object({
|
|
__typename: z.literal(
|
|
SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact
|
|
),
|
|
join_loyalty_contact: z.object({
|
|
title: z.string().optional(),
|
|
preamble: z.string().optional(),
|
|
contact: z.array(
|
|
z.object({
|
|
__typename: z.literal(
|
|
JoinLoyaltyContactTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContactBlockContactContact
|
|
),
|
|
contact: z.object({
|
|
display_text: z.string().optional(),
|
|
|
|
contact_field: z.string(),
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
const loyaltyPageSidebarItem = z.discriminatedUnion("__typename", [
|
|
loyaltyPageSidebarTextContent,
|
|
loyaltyPageJoinLoyaltyContact,
|
|
])
|
|
|
|
export const validateLoyaltyPageSchema = z.object({
|
|
all_loyalty_page: z.object({
|
|
items: z.array(
|
|
z.object({
|
|
title: z.string(),
|
|
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
|
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
// Block types
|
|
type CardGridRaw = z.infer<typeof loyaltyPageBlockCardGrid>
|
|
|
|
export type CardGridCard = Omit<
|
|
CardGridRaw["card_grid"]["cards"][number],
|
|
"referenceConnection"
|
|
> & {
|
|
link:
|
|
| {
|
|
href: string
|
|
title: string
|
|
}
|
|
| undefined
|
|
}
|
|
|
|
export type CardGrid = Omit<CardGridRaw, "card_grid"> & {
|
|
card_grid: Omit<CardGridRaw["card_grid"], "cards"> & {
|
|
cards: CardGridCard[]
|
|
}
|
|
}
|
|
|
|
type DynamicContentRaw = z.infer<typeof loyaltyPageDynamicContent>
|
|
|
|
export type DynamicContent = Omit<DynamicContentRaw, "dynamic_content"> & {
|
|
dynamic_content: Omit<DynamicContentRaw["dynamic_content"], "link"> & {
|
|
link:
|
|
| {
|
|
href: string
|
|
title: string
|
|
text?: string
|
|
}
|
|
| undefined
|
|
}
|
|
}
|
|
type BlockContentRaw = z.infer<typeof loyaltyPageBlockTextContent>
|
|
|
|
export interface RteBlockContent extends BlockContentRaw {
|
|
content: {
|
|
content: {
|
|
json: RTEDocument
|
|
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
|
}
|
|
}
|
|
}
|
|
|
|
type ShortcutsRaw = z.infer<typeof loyaltyPageShortcuts>
|
|
|
|
export type Shortcuts = Omit<ShortcutsRaw, "shortcuts"> & {
|
|
shortcuts: Omit<ShortcutsRaw["shortcuts"], "shortcuts"> & {
|
|
shortcuts: {
|
|
text?: string
|
|
openInNewTab: boolean
|
|
url: string
|
|
title: string
|
|
}[]
|
|
}
|
|
}
|
|
|
|
export type Block = CardGrid | RteBlockContent | DynamicContent | Shortcuts
|
|
|
|
// Sidebar block types
|
|
type SidebarContentRaw = z.infer<typeof loyaltyPageSidebarTextContent>
|
|
|
|
export type RteSidebarContent = Omit<SidebarContentRaw, "content"> & {
|
|
content: {
|
|
content: {
|
|
json: RTEDocument
|
|
embedded_itemsConnection: EdgesWithTotalCount<Embeds>
|
|
}
|
|
}
|
|
}
|
|
export type JoinLoyaltyContact = z.infer<typeof loyaltyPageJoinLoyaltyContact>
|
|
export type Sidebar = JoinLoyaltyContact | RteSidebarContent
|
|
|
|
export type LoyaltyPageDataRaw = z.infer<typeof validateLoyaltyPageSchema>
|
|
|
|
type LoyaltyPageRaw = LoyaltyPageDataRaw["all_loyalty_page"]["items"][0]
|
|
|
|
export type LoyaltyPage = Omit<LoyaltyPageRaw, "blocks" | "sidebar"> & {
|
|
blocks: Block[]
|
|
sidebar: Sidebar[]
|
|
}
|