refactor: infer types from zod validation

This commit is contained in:
Christel Westerberg
2024-04-29 09:53:54 +02:00
parent 00f30811cf
commit 49b7aa89f8
18 changed files with 418 additions and 217 deletions

View File

@@ -1,8 +1,7 @@
import type { RTENode } from "../rte/node"
import type { Node } from "@/types/requests/utils/edges"
import type { RenderOptions } from "../rte/option"
import type { Embeds } from "@/types/requests/embeds"
import type { Node } from "@/types/requests/utils/edges"
import type { RTENode } from "../rte/node"
import type { RenderOptions } from "../rte/option"
export type JsonToHtmlProps = {
embeds: Node<Embeds>[]

View File

@@ -1,38 +1,28 @@
import { Embeds } from "@/types/requests/embeds"
import { DynamicContentBlock } from "@/types/requests/loyaltyPage"
import { PageLink } from "@/types/requests/myPages/navigation"
import { Edges } from "@/types/requests/utils/edges"
import { RTEDocument } from "@/types/rte/node"
import {
Block,
CardGrid,
CardGridCard,
DynamicContent,
RteBlockContent,
} from "@/server/routers/contentstack/loyaltyPage/output"
export type BlocksProps = {
blocks: Block[]
}
export type DynamicContentProps = {
dynamicContent: DynamicContentBlock["dynamic_content"]
dynamicContent: DynamicContent["dynamic_content"]
}
type Card = {
referenceConnection: Edges<PageLink>
title?: string
subtitle?: string
open_in_new_tab: boolean
export type DynamicComponentProps = {
component: DynamicContent["dynamic_content"]["component"]
}
export type CardProps = { card: Card }
export type CardProps = { card: CardGridCard }
export type CardGrid = {
card_grid: {
title?: string
subtitle?: string
cards: Card[]
}
}
export type CardGridProps = Pick<CardGrid, "card_grid">
export type CardGridProps = CardGrid
export type Content = {
content: {
embedded_itemsConnection: Edges<Embeds>
json: RTEDocument
}
}
export type Content = { content: RteBlockContent["content"]["content"] }
export type LevelCardProps = {
level: {

View File

@@ -1,20 +1,16 @@
import { ContactFields } from "@/types/requests/contactConfig"
import { Embeds } from "@/types/requests/embeds"
import { JoinLoyaltyContactContact } from "@/types/requests/loyaltyPage"
import { Edges } from "@/types/requests/utils/edges"
import { RTEDocument } from "@/types/rte/node"
import {
JoinLoyaltyContact,
Sidebar,
} from "@/server/routers/contentstack/loyaltyPage/output"
export type SidebarContent = {
content: {
embedded_itemsConnection: Edges<Embeds>
json: RTEDocument
}
export type SidebarProps = {
block: Sidebar
}
export type Contact = {
contact: ContactFields
export type JoinLoyaltyContactProps = {
block: JoinLoyaltyContact["join_loyalty_contact"]
}
export type ContactProps = {
contactBlock: JoinLoyaltyContactContact[]
contactBlock: JoinLoyaltyContact["join_loyalty_contact"]["contact"]
}

View File

@@ -4,7 +4,7 @@ export type Image = {
height: number
width: number
}
metadata: JSON
metadata: JSON | null
system: {
uid: string
}

View File

@@ -47,28 +47,7 @@ export type GetContactConfigData = {
all_contact_config: AllRequestResponse<ContactConfig>
}
// Utility types that extract the possible strings of ContactConfigField,
// Which is all the dot notated values of ContactConfig (for example: 'email.name')
// From: https://stackoverflow.com/questions/47057649/typescript-string-dot-notation-of-nested-object#47058976
type PathsToStringProps<T> = T extends string
? []
: {
[K in Extract<keyof T, string>]: [K, ...PathsToStringProps<T[K]>]
}[Extract<keyof T, string>]
type Join<T extends string[], D extends string> = T extends []
? never
: T extends [infer F]
? F
: T extends [infer F, ...infer R]
? F extends string
? `${F}${D}${Join<Extract<R, string[]>, D>}`
: never
: string
export type ContactConfigField = Join<PathsToStringProps<ContactConfig>, ".">
export type ContactFields = {
display_text?: string
contact_field: ContactConfigField
contact_field: string
}

View File

@@ -1,9 +1,4 @@
import { CardGrid, Content } from "../components/loyalty/blocks"
import { Contact, SidebarContent } from "../components/loyalty/sidebar"
import { PageLink } from "./myPages/navigation"
import { Edges } from "./utils/edges"
import type { AllRequestResponse } from "./utils/all"
import type { JoinLoyaltyContact } from "@/server/routers/contentstack/loyaltyPage/output"
import type { Typename } from "./utils/typename"
export enum JoinLoyaltyContactTypenameEnum {
@@ -11,18 +6,10 @@ export enum JoinLoyaltyContactTypenameEnum {
}
export type JoinLoyaltyContactContact = Typename<
Contact,
JoinLoyaltyContact["join_loyalty_contact"],
JoinLoyaltyContactTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContactBlockContactContact
>
export type JoinLoyaltyContact = {
join_loyalty_contact: {
title?: string
preamble?: string
contact: JoinLoyaltyContactContact[]
}
}
export enum SidebarTypenameEnum {
LoyaltyPageSidebarJoinLoyaltyContact = "LoyaltyPageSidebarJoinLoyaltyContact",
LoyaltyPageSidebarContent = "LoyaltyPageSidebarContent",
@@ -30,13 +17,6 @@ export enum SidebarTypenameEnum {
export type SidebarTypename = keyof typeof SidebarTypenameEnum
export type Sidebar =
| Typename<SidebarContent, SidebarTypenameEnum.LoyaltyPageSidebarContent>
| Typename<
JoinLoyaltyContact,
SidebarTypenameEnum.LoyaltyPageSidebarJoinLoyaltyContact
>
export enum LoyaltyComponentEnum {
loyalty_levels = "loyalty_levels",
how_it_works = "how_it_works",
@@ -45,57 +25,8 @@ export enum LoyaltyComponentEnum {
export type LoyaltyComponent = keyof typeof LoyaltyComponentEnum
export type DynamicContentBlock = {
dynamic_content: {
title?: string
subtitle?: string
component: LoyaltyComponent
link: {
text?: string
pageConnection: Edges<PageLink>
}
}
}
export enum LoyaltyBlocksTypenameEnum {
LoyaltyPageBlocksDynamicContent = "LoyaltyPageBlocksDynamicContent",
LoyaltyPageBlocksCardGrid = "LoyaltyPageBlocksCardGrid",
LoyaltyPageBlocksContent = "LoyaltyPageBlocksContent",
}
export type Blocks =
| Typename<CardGrid, LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid>
| Typename<
DynamicContentBlock,
LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent
>
| Typename<Content, LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent>
export type Breadcrumb = {
href: string
title: string
}
export type Breadcrumbs = {
parents: Breadcrumb[]
title: string
}
export type LoyaltyPage = {
sidebar: Sidebar[]
blocks: Blocks[]
web: {
breadcrumbs: Breadcrumbs
}
system: {
created_at: string
uid: string
updated_at: string
}
title: string
url: string
}
export type GetLoyaltyPageData = {
all_loyalty_page: AllRequestResponse<LoyaltyPage>
}

View File

@@ -1,11 +1,12 @@
import { RTETypeEnum } from "./enums"
import type { EmbedByUid } from "../components/jsontohtml"
import type {
Attributes,
RTEAnchorAttrs,
RTEAssetAttrs,
RTELinkAttrs,
} from "./attrs"
import type { EmbedByUid } from "../components/jsontohtml"
import type { RenderOptions } from "./option"
export interface RTEDefaultNode {