fix: refactor lang handling in contentstack base procedure
This commit is contained in:
@@ -56,77 +56,81 @@ export const baseQueryRouter = router({
|
||||
|
||||
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
||||
}),
|
||||
header: publicProcedure.input(langInput).query(async ({ input }) => {
|
||||
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
|
||||
locale: input.lang,
|
||||
})
|
||||
|
||||
const response = await request<HeaderDataRaw>(
|
||||
GetCurrentHeader,
|
||||
{ locale: input.lang },
|
||||
{
|
||||
tags: [
|
||||
generateTag(
|
||||
input.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) {
|
||||
console.info(`Failed to validate Header - (lang: ${input.lang})`)
|
||||
console.error(validatedHeaderConfig.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const logo =
|
||||
validatedHeaderConfig.data.all_current_header.items[0].logoConnection
|
||||
.edges?.[0]?.node
|
||||
|
||||
return {
|
||||
...validatedHeaderConfig.data.all_current_header.items[0],
|
||||
logo,
|
||||
} as HeaderData
|
||||
}),
|
||||
footer: publicProcedure.input(langInput).query(async ({ input }) => {
|
||||
const responseRef = await request<FooterRefDataRaw>(GetCurrentFooterRef, {
|
||||
locale: input.lang,
|
||||
})
|
||||
|
||||
const response = await request<FooterDataRaw>(
|
||||
GetCurrentFooter,
|
||||
{
|
||||
header: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input }) => {
|
||||
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
|
||||
locale: input.lang,
|
||||
},
|
||||
{
|
||||
tags: [
|
||||
generateTag(
|
||||
input.lang,
|
||||
responseRef.data.all_current_footer.items[0].system.uid
|
||||
),
|
||||
],
|
||||
})
|
||||
|
||||
const response = await request<HeaderDataRaw>(
|
||||
GetCurrentHeader,
|
||||
{ locale: input.lang },
|
||||
{
|
||||
tags: [
|
||||
generateTag(
|
||||
input.lang,
|
||||
responseRef.data.all_current_header.items[0].system.uid
|
||||
),
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
)
|
||||
|
||||
const validatedFooterConfig = validateFooterConfigSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
const validatedHeaderConfig = validateHeaderConfigSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedFooterConfig.success) {
|
||||
console.info(`Failed to validate Footer - (lang: ${input.lang})`)
|
||||
console.error(validatedFooterConfig.error)
|
||||
return null
|
||||
}
|
||||
if (!validatedHeaderConfig.success) {
|
||||
console.info(`Failed to validate Header - (lang: ${input.lang})`)
|
||||
console.error(validatedHeaderConfig.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return validatedFooterConfig.data.all_current_footer.items[0]
|
||||
}),
|
||||
const logo =
|
||||
validatedHeaderConfig.data.all_current_header.items[0].logoConnection
|
||||
.edges?.[0]?.node
|
||||
|
||||
return {
|
||||
...validatedHeaderConfig.data.all_current_header.items[0],
|
||||
logo,
|
||||
} as HeaderData
|
||||
}),
|
||||
footer: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input }) => {
|
||||
const responseRef = await request<FooterRefDataRaw>(GetCurrentFooterRef, {
|
||||
locale: input.lang,
|
||||
})
|
||||
|
||||
const response = await request<FooterDataRaw>(
|
||||
GetCurrentFooter,
|
||||
{
|
||||
locale: input.lang,
|
||||
},
|
||||
{
|
||||
tags: [
|
||||
generateTag(
|
||||
input.lang,
|
||||
responseRef.data.all_current_footer.items[0].system.uid
|
||||
),
|
||||
],
|
||||
}
|
||||
)
|
||||
|
||||
const validatedFooterConfig = validateFooterConfigSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedFooterConfig.success) {
|
||||
console.info(`Failed to validate Footer - (lang: ${input.lang})`)
|
||||
console.error(validatedFooterConfig.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return validatedFooterConfig.data.all_current_footer.items[0]
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -4,6 +4,7 @@ import { env } from "@/env/server"
|
||||
|
||||
import { badRequestError, sessionExpiredError } from "./errors/trpc"
|
||||
import { transformer } from "./transformer"
|
||||
import { langInput } from "./utils"
|
||||
|
||||
import type { Meta } from "@/types/trpc/meta"
|
||||
import type { Context } from "./context"
|
||||
@@ -12,9 +13,19 @@ const t = initTRPC.context<Context>().meta<Meta>().create({ transformer })
|
||||
|
||||
export const { createCallerFactory, mergeRouters, router } = t
|
||||
export const publicProcedure = t.procedure
|
||||
export const contentstackBaseProcedure = t.procedure.use(function (opts) {
|
||||
export const contentstackBaseProcedure = t.procedure.use(async function (opts) {
|
||||
if (!opts.ctx.lang) {
|
||||
throw badRequestError("Missing Lang in tRPC context")
|
||||
const input = await opts.getRawInput()
|
||||
|
||||
const parsedInput = langInput.safeParse(input)
|
||||
if (!parsedInput.success) {
|
||||
throw badRequestError("Missing Lang in tRPC context")
|
||||
}
|
||||
return opts.next({
|
||||
ctx: {
|
||||
lang: parsedInput.data.lang,
|
||||
},
|
||||
})
|
||||
}
|
||||
return opts.next({
|
||||
ctx: {
|
||||
|
||||
7
server/utils.ts
Normal file
7
server/utils.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
|
||||
export const langInput = z.object({
|
||||
lang: z.nativeEnum(Lang),
|
||||
})
|
||||
Reference in New Issue
Block a user