Files
web/server/routers/contentstack/accountPage/output.ts
2024-05-03 08:16:52 +02:00

161 lines
4.1 KiB
TypeScript

import { z } from "zod"
import { Lang } from "@/constants/languages"
import { Embeds } from "@/types/requests/embeds"
import {
ContentEntries,
DynamicContentComponents,
} from "@/types/requests/myPages/accountpage"
import { Edges } from "@/types/requests/utils/edges"
import { RTEDocument } from "@/types/rte/node"
const accountPageShortcuts = z.object({
__typename: z.literal(ContentEntries.AccountPageContentShortcuts),
title: z.string().optional(),
preamble: z.string().optional(),
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(),
title: z.string(),
}),
})
),
totalCount: z.number(),
}),
text: z.string().optional(),
open_in_new_tab: z.boolean(),
})
),
}),
})
const accountPageDynamicContent = z.object({
__typename: z.literal(ContentEntries.AccountPageContentDynamicContent),
dynamic_content: z.object({
title: z.string().optional(),
preamble: z.string().optional(),
component: z.nativeEnum(DynamicContentComponents),
link: 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(),
title: z.string(),
}),
})
),
totalCount: z.number(),
}),
link_text: z.string(),
}),
// .optional(),
}),
})
// To validate the JSON content
// https://zod.dev/?id=json-type
const literalSchema = z.union([z.string(), z.number(), z.boolean(), z.null()])
type Literal = z.infer<typeof literalSchema>
type Json = Literal | { [key: string]: Json } | Json[]
const jsonSchema: z.ZodType<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
)
const accountPageTextContent = z.object({
__typename: z.literal(ContentEntries.AccountPageContentTextContent),
text_content: z.object({
content: z.object({
json: z.any(),
embedded_itemsConnection: z.object({
edges: z.array(z.any()),
totalCount: z.number(),
}),
}),
}),
})
type TextContentRaw = z.infer<typeof accountPageTextContent>
type DynamicContentRaw = z.infer<typeof accountPageDynamicContent>
type ShortcutsRaw = z.infer<typeof accountPageShortcuts>
export type RteTextContent = Omit<TextContentRaw, "text_content"> & {
text_content: {
content: {
json: RTEDocument
embedded_itemsConnection: Edges<Embeds>
}
}
}
type AccountPageContentItem = DynamicContentRaw | ShortcutsRaw | RteTextContent
const accountPageContentItem = z.discriminatedUnion("__typename", [
accountPageShortcuts,
accountPageDynamicContent,
accountPageTextContent,
])
export const validateAccountPageSchema = z.object({
all_account_page: z.object({
items: z.array(
z.object({
url: z.string(),
title: z.string(),
content: z.array(accountPageContentItem),
})
),
}),
})
export const validateAccountPageOverviewSchema = z.object({
all_account_page: z.object({
items: z.array(
z.object({
url: z.string(),
title: z.string(),
content: z.array(accountPageContentItem),
})
),
}),
})
export const validateAccountPageBenefitsSchema = z.object({
all_account_page: z.object({
items: z.array(
z.object({
url: z.string(),
title: z.string(),
content: z.array(z.object({})),
})
),
}),
})
type AccountPageDataRaw = z.infer<typeof validateAccountPageOverviewSchema>
type AccountPageRaw = AccountPageDataRaw["all_account_page"]["items"][0]
export type AccountPage = Omit<AccountPageRaw, "content"> & {
url: string
title: string
content: AccountPageContentItem[]
}