refactor: zod validation and pr comments
This commit is contained in:
@@ -1,32 +1,34 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.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 GetContactConfig from "@/lib/graphql/Query/ContactConfig.graphql"
|
||||
|
||||
import type { GetContactConfigData } from "@/types/requests/contactConfig"
|
||||
import { getConfigInput } from "./input"
|
||||
import { type ContactConfigData, validateContactConfigSchema } from "./output"
|
||||
|
||||
export const contactConfigQueryRouter = router({
|
||||
get: publicProcedure
|
||||
.input(z.object({ lang: z.nativeEnum(Lang) }))
|
||||
.query(async ({ input }) => {
|
||||
const contactConfig = await request<GetContactConfigData>(
|
||||
GetContactConfig,
|
||||
{
|
||||
locale: input.lang,
|
||||
},
|
||||
{
|
||||
tags: [`contact-config-${input.lang}`],
|
||||
}
|
||||
)
|
||||
get: publicProcedure.input(getConfigInput).query(async ({ input }) => {
|
||||
try {
|
||||
const contactConfig = await request<ContactConfigData>(GetContactConfig, {
|
||||
locale: input.lang,
|
||||
})
|
||||
|
||||
if (contactConfig.data && contactConfig.data.all_contact_config.total) {
|
||||
return contactConfig.data.all_contact_config.items[0]
|
||||
if (!contactConfig.data) {
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const validatedContactConfigConfig =
|
||||
validateContactConfigSchema.safeParse(contactConfig.data)
|
||||
|
||||
if (!validatedContactConfigConfig.success) {
|
||||
console.error(validatedContactConfigConfig.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
throw badRequestError()
|
||||
}),
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user