fix: refactor lang handling in contentstack base procedure

This commit is contained in:
Christel Westerberg
2024-06-18 16:51:06 +02:00
parent 928b2af698
commit 74f875d30b
3 changed files with 92 additions and 70 deletions
+13 -2
View File
@@ -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: {