fix: refactor lang handling in contentstack base procedure
This commit is contained in:
@@ -56,7 +56,9 @@ export const baseQueryRouter = router({
|
||||
|
||||
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
||||
}),
|
||||
header: publicProcedure.input(langInput).query(async ({ input }) => {
|
||||
header: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input }) => {
|
||||
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
|
||||
locale: input.lang,
|
||||
})
|
||||
@@ -97,7 +99,9 @@ export const baseQueryRouter = router({
|
||||
logo,
|
||||
} as HeaderData
|
||||
}),
|
||||
footer: publicProcedure.input(langInput).query(async ({ input }) => {
|
||||
footer: contentstackBaseProcedure
|
||||
.input(langInput)
|
||||
.query(async ({ input }) => {
|
||||
const responseRef = await request<FooterRefDataRaw>(GetCurrentFooterRef, {
|
||||
locale: input.lang,
|
||||
})
|
||||
|
||||
@@ -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,10 +13,20 @@ 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) {
|
||||
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: {
|
||||
lang: opts.ctx.lang,
|
||||
|
||||
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