85 lines
2.1 KiB
TypeScript
85 lines
2.1 KiB
TypeScript
import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql"
|
|
import {
|
|
GetCurrentHeader,
|
|
GetCurrentHeaderRef,
|
|
} from "@/lib/graphql/Query/CurrentHeader.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { internalServerError, notFound } from "@/server/errors/trpc"
|
|
import { contentstackProcedure, publicProcedure, router } from "@/server/trpc"
|
|
|
|
import { generateTag } from "@/utils/generateTag"
|
|
|
|
import {
|
|
type ContactConfigData,
|
|
HeaderData,
|
|
HeaderDataRaw,
|
|
HeaderRefDataRaw,
|
|
validateContactConfigSchema,
|
|
validateHeaderConfigSchema,
|
|
} from "./output"
|
|
|
|
export const configQueryRouter = router({
|
|
contact: contentstackProcedure.query(async ({ ctx }) => {
|
|
const { lang } = ctx
|
|
|
|
const response = await request<ContactConfigData>(GetContactConfig, {
|
|
locale: lang,
|
|
})
|
|
|
|
if (!response.data) {
|
|
throw notFound(response)
|
|
}
|
|
|
|
const validatedContactConfigConfig = validateContactConfigSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedContactConfigConfig.success) {
|
|
throw internalServerError(validatedContactConfigConfig.error)
|
|
}
|
|
|
|
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
|
}),
|
|
header: publicProcedure.query(async ({ ctx }) => {
|
|
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
|
|
locale: ctx.lang,
|
|
})
|
|
|
|
const response = await request<HeaderDataRaw>(
|
|
GetCurrentHeader,
|
|
{ locale: ctx.lang },
|
|
{
|
|
next: {
|
|
tags: [
|
|
generateTag(
|
|
ctx.lang,
|
|
responseRef.data.all_current_header.items[0].system.uid
|
|
),
|
|
],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
throw notFound(response)
|
|
}
|
|
|
|
const validatedHeaderConfig = validateHeaderConfigSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedHeaderConfig.success) {
|
|
throw internalServerError(validatedHeaderConfig.error)
|
|
}
|
|
|
|
const logo =
|
|
validatedHeaderConfig.data.all_current_header.items[0].logoConnection
|
|
.edges?.[0]?.node
|
|
|
|
return {
|
|
...validatedHeaderConfig.data.all_current_header.items[0],
|
|
logo,
|
|
} as HeaderData
|
|
}),
|
|
})
|