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]
|
return validatedContactConfigConfig.data.all_contact_config.items[0]
|
||||||
}),
|
}),
|
||||||
header: publicProcedure.input(langInput).query(async ({ input }) => {
|
header: contentstackBaseProcedure
|
||||||
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
|
.input(langInput)
|
||||||
locale: input.lang,
|
.query(async ({ input }) => {
|
||||||
})
|
const responseRef = await request<HeaderRefDataRaw>(GetCurrentHeaderRef, {
|
||||||
|
|
||||||
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,
|
|
||||||
{
|
|
||||||
locale: input.lang,
|
locale: input.lang,
|
||||||
},
|
})
|
||||||
{
|
|
||||||
tags: [
|
const response = await request<HeaderDataRaw>(
|
||||||
generateTag(
|
GetCurrentHeader,
|
||||||
input.lang,
|
{ locale: input.lang },
|
||||||
responseRef.data.all_current_footer.items[0].system.uid
|
{
|
||||||
),
|
tags: [
|
||||||
],
|
generateTag(
|
||||||
|
input.lang,
|
||||||
|
responseRef.data.all_current_header.items[0].system.uid
|
||||||
|
),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!response.data) {
|
||||||
|
throw notFound(response)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
const validatedFooterConfig = validateFooterConfigSchema.safeParse(
|
const validatedHeaderConfig = validateHeaderConfigSchema.safeParse(
|
||||||
response.data
|
response.data
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!validatedFooterConfig.success) {
|
if (!validatedHeaderConfig.success) {
|
||||||
console.info(`Failed to validate Footer - (lang: ${input.lang})`)
|
console.info(`Failed to validate Header - (lang: ${input.lang})`)
|
||||||
console.error(validatedFooterConfig.error)
|
console.error(validatedHeaderConfig.error)
|
||||||
return null
|
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 { badRequestError, sessionExpiredError } from "./errors/trpc"
|
||||||
import { transformer } from "./transformer"
|
import { transformer } from "./transformer"
|
||||||
|
import { langInput } from "./utils"
|
||||||
|
|
||||||
import type { Meta } from "@/types/trpc/meta"
|
import type { Meta } from "@/types/trpc/meta"
|
||||||
import type { Context } from "./context"
|
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 { createCallerFactory, mergeRouters, router } = t
|
||||||
export const publicProcedure = t.procedure
|
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) {
|
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({
|
return opts.next({
|
||||||
ctx: {
|
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