fix: improve accountPage tRPC query
This commit is contained in:
5
server/routers/contentstack/accountPage/input.ts
Normal file
5
server/routers/contentstack/accountPage/input.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
import { Lang } from "@/constants/languages"
|
||||||
|
|
||||||
|
export const getAccountPageInput = z.object({ lang: z.nativeEnum(Lang) })
|
||||||
122
server/routers/contentstack/accountPage/output.ts
Normal file
122
server/routers/contentstack/accountPage/output.ts
Normal file
@@ -0,0 +1,122 @@
|
|||||||
|
import { z } from "zod"
|
||||||
|
|
||||||
|
import {
|
||||||
|
ContentEntries,
|
||||||
|
DynamicContentComponents,
|
||||||
|
} from "@/types/requests/myPages/accountpage"
|
||||||
|
|
||||||
|
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.string(),
|
||||||
|
}),
|
||||||
|
url: z.string(),
|
||||||
|
title: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
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.string(),
|
||||||
|
}),
|
||||||
|
url: z.string(),
|
||||||
|
title: 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: jsonSchema,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
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({})),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
})
|
||||||
@@ -1,52 +1,82 @@
|
|||||||
import { z } from "zod"
|
|
||||||
|
|
||||||
import { Lang } from "@/constants/languages"
|
|
||||||
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
|
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
|
||||||
import { request } from "@/lib/graphql/request"
|
import { request } from "@/lib/graphql/request"
|
||||||
import { badRequestError } from "@/server/errors/trpc"
|
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||||
import { publicProcedure, router } from "@/server/trpc"
|
import { publicProcedure, router } from "@/server/trpc"
|
||||||
|
|
||||||
|
import { getAccountPageInput } from "./input"
|
||||||
|
import { validateAccountPageSchema } from "./output"
|
||||||
|
|
||||||
import type { GetAccountPageData } from "@/types/requests/myPages/accountpage"
|
import type { GetAccountPageData } from "@/types/requests/myPages/accountpage"
|
||||||
|
|
||||||
export const accountPageQueryRouter = router({
|
export const accountPageQueryRouter = router({
|
||||||
getOverview: publicProcedure
|
getOverview: publicProcedure
|
||||||
.input(z.object({ lang: z.nativeEnum(Lang) }))
|
.input(getAccountPageInput)
|
||||||
.query(async ({ input }) => {
|
.query(async ({ input }) => {
|
||||||
const url = "/my-pages/overview"
|
try {
|
||||||
const accountPage = await request<GetAccountPageData>(
|
const url = "/my-pages/overview"
|
||||||
GetAccountPage,
|
const response = await request<GetAccountPageData>(
|
||||||
{
|
GetAccountPage,
|
||||||
locale: input.lang,
|
{
|
||||||
url,
|
locale: input.lang,
|
||||||
},
|
url,
|
||||||
{
|
},
|
||||||
tags: [`${url}-${input.lang}`],
|
{
|
||||||
}
|
tags: [`${url}-${input.lang}`],
|
||||||
)
|
}
|
||||||
if (accountPage.data && accountPage.data.all_account_page.total) {
|
)
|
||||||
return accountPage.data.all_account_page.items[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
throw badRequestError()
|
if (!response.data) {
|
||||||
|
throw badRequestError()
|
||||||
|
}
|
||||||
|
|
||||||
|
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!validatedAccountPage.success) {
|
||||||
|
throw badRequestError()
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data.all_account_page.items[0]
|
||||||
|
} catch (error) {
|
||||||
|
console.info(`Get Account Page Overview Error`)
|
||||||
|
console.error(error)
|
||||||
|
throw internalServerError()
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
getBenefits: publicProcedure
|
getBenefits: publicProcedure
|
||||||
.input(z.object({ lang: z.nativeEnum(Lang) }))
|
.input(getAccountPageInput)
|
||||||
.query(async ({ input }) => {
|
.query(async ({ input }) => {
|
||||||
const url = "/my-pages/benefits"
|
try {
|
||||||
const accountPage = await request<GetAccountPageData>(
|
const url = "/my-pages/benefits"
|
||||||
GetAccountPage,
|
const response = await request<GetAccountPageData>(
|
||||||
{
|
GetAccountPage,
|
||||||
locale: input.lang,
|
{
|
||||||
url,
|
locale: input.lang,
|
||||||
},
|
url,
|
||||||
{
|
},
|
||||||
tags: [`${url}-${input.lang}`],
|
{
|
||||||
}
|
tags: [`${url}-${input.lang}`],
|
||||||
)
|
}
|
||||||
if (accountPage.data && accountPage.data.all_account_page.total) {
|
)
|
||||||
return accountPage.data.all_account_page.items[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
throw badRequestError()
|
if (!response.data) {
|
||||||
|
throw badRequestError()
|
||||||
|
}
|
||||||
|
|
||||||
|
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
||||||
|
response.data
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!validatedAccountPage.success) {
|
||||||
|
throw badRequestError()
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data.all_account_page.items[0]
|
||||||
|
} catch (error) {
|
||||||
|
console.info(`Get Account Page Benefits Error`)
|
||||||
|
console.error(error)
|
||||||
|
throw internalServerError()
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user