refactor: infer types from zod validation
This commit is contained in:
@@ -1,33 +1,132 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import GetLoyaltyPage from "@/lib/graphql/Query/LoyaltyPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
import GetLoyaltyPage from "@/lib/graphql/Query/LoyaltyPage.graphql"
|
||||
import { getLoyaltyPageInput } from "./input"
|
||||
import { type LoyaltyPage, validateLoyaltyPageSchema } from "./output"
|
||||
|
||||
import type { GetLoyaltyPageData } from "@/types/requests/loyaltyPage"
|
||||
import { Embeds } from "@/types/requests/embeds"
|
||||
import {
|
||||
LoyaltyBlocksTypenameEnum,
|
||||
SidebarTypenameEnum,
|
||||
} from "@/types/requests/loyaltyPage"
|
||||
import { Edges } from "@/types/requests/utils/edges"
|
||||
import { RTEDocument } from "@/types/rte/node"
|
||||
|
||||
export const loyaltyPageQueryRouter = router({
|
||||
get: publicProcedure
|
||||
.input(z.object({ uri: z.string(), lang: z.nativeEnum(Lang) }))
|
||||
.query(async ({ input }) => {
|
||||
const loyaltyPage = await request<GetLoyaltyPageData>(
|
||||
GetLoyaltyPage,
|
||||
{
|
||||
locale: input.lang,
|
||||
url: input.uri,
|
||||
},
|
||||
{
|
||||
tags: [`${input.uri}-${input.lang}`],
|
||||
}
|
||||
)
|
||||
get: publicProcedure.input(getLoyaltyPageInput).query(async ({ input }) => {
|
||||
try {
|
||||
const loyaltyPageRes = await request<LoyaltyPage>(GetLoyaltyPage, {
|
||||
locale: input.locale,
|
||||
url: input.href,
|
||||
})
|
||||
|
||||
if (loyaltyPage.data && loyaltyPage.data.all_loyalty_page.items.length) {
|
||||
return loyaltyPage.data.all_loyalty_page.items[0]
|
||||
if (!loyaltyPageRes.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const validatedLoyaltyPage = validateLoyaltyPageSchema.safeParse(
|
||||
loyaltyPageRes.data
|
||||
)
|
||||
|
||||
if (!validatedLoyaltyPage.success) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const sidebar =
|
||||
validatedLoyaltyPage.data.all_loyalty_page.items[0].sidebar.map(
|
||||
(block) => {
|
||||
if (
|
||||
block.__typename == SidebarTypenameEnum.LoyaltyPageSidebarContent
|
||||
) {
|
||||
return {
|
||||
...block,
|
||||
content: {
|
||||
content: {
|
||||
json: block.content.content.json as RTEDocument,
|
||||
embedded_itemsConnection: block.content.content
|
||||
.embedded_itemsConnection as Edges<Embeds>,
|
||||
},
|
||||
},
|
||||
}
|
||||
} else {
|
||||
return block
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const blocks =
|
||||
validatedLoyaltyPage.data.all_loyalty_page.items[0].blocks.map(
|
||||
(block) => {
|
||||
switch (block.__typename) {
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksCardGrid:
|
||||
return {
|
||||
...block,
|
||||
card_grid: {
|
||||
...block.card_grid,
|
||||
cards: block.card_grid.cards.map((card) => {
|
||||
return {
|
||||
...card,
|
||||
link:
|
||||
card.referenceConnection.totalCount > 0
|
||||
? {
|
||||
href: card.referenceConnection.edges[0].node
|
||||
.url,
|
||||
title:
|
||||
card.referenceConnection.edges[0].node.title,
|
||||
}
|
||||
: undefined,
|
||||
}
|
||||
}),
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent:
|
||||
return {
|
||||
...block,
|
||||
dynamic_content: {
|
||||
...block.dynamic_content,
|
||||
link:
|
||||
block.dynamic_content.link.pageConnection.totalCount > 0
|
||||
? {
|
||||
text: block.dynamic_content.link.text,
|
||||
href: block.dynamic_content.link.pageConnection
|
||||
.edges[0].node.url,
|
||||
title:
|
||||
block.dynamic_content.link.pageConnection.edges[0]
|
||||
.node.title,
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
}
|
||||
case LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksContent:
|
||||
return {
|
||||
...block,
|
||||
content: {
|
||||
content: {
|
||||
json: block.content.content.json as RTEDocument,
|
||||
embedded_itemsConnection: block.content.content
|
||||
.embedded_itemsConnection as Edges<Embeds>,
|
||||
},
|
||||
},
|
||||
}
|
||||
default:
|
||||
return block
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const loyaltyPage = {
|
||||
...validatedLoyaltyPage.data.all_loyalty_page.items[0],
|
||||
blocks,
|
||||
sidebar,
|
||||
} as LoyaltyPage
|
||||
|
||||
return loyaltyPage
|
||||
} catch (error) {
|
||||
console.info(`Get Loyalty Page Error`)
|
||||
console.error(error)
|
||||
throw badRequestError()
|
||||
}),
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user