feat: improve structure and error handling
This commit is contained in:
@@ -3,8 +3,8 @@ import {
|
||||
GetNavigationMyPagesRefs,
|
||||
} from "@/lib/graphql/Query/NavigationMyPages.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router } from "@/server/trpc"
|
||||
import { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
import { contentstackProcedure, publicProcedure, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
@@ -13,7 +13,6 @@ import {
|
||||
} from "@/utils/generateTag"
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import { getNavigationInputSchema } from "./input"
|
||||
import {
|
||||
getNavigationSchema,
|
||||
navigationPayloadSchema,
|
||||
@@ -48,82 +47,70 @@ export function mapMenuItems(navigationItems: NavigationItem[]) {
|
||||
}
|
||||
|
||||
export const navigationQueryRouter = router({
|
||||
get: publicProcedure.input(getNavigationInputSchema).query(async function ({
|
||||
input: lang,
|
||||
}) {
|
||||
try {
|
||||
const refsResponse = await request<GetNavigationMyPagesRefsData>(
|
||||
GetNavigationMyPagesRefs,
|
||||
{ locale: lang },
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, "navigation_my_pages")],
|
||||
},
|
||||
}
|
||||
)
|
||||
get: contentstackProcedure.query(async function ({ ctx }) {
|
||||
const { lang } = ctx
|
||||
|
||||
if (!refsResponse.data) {
|
||||
console.error("Bad response for `GetNavigationMyPagesRefs`")
|
||||
console.error({ refsResponse })
|
||||
throw internalServerError()
|
||||
const refsResponse = await request<GetNavigationMyPagesRefsData>(
|
||||
GetNavigationMyPagesRefs,
|
||||
{ locale: lang },
|
||||
{
|
||||
next: {
|
||||
tags: [generateRefsResponseTag(lang, "navigation_my_pages")],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
const validatedMyPagesNavigationRefs =
|
||||
navigationRefsPayloadSchema.safeParse(refsResponse.data)
|
||||
if (!validatedMyPagesNavigationRefs.success) {
|
||||
console.error("Bad validation for `GetNavigationMyPagesRefs`")
|
||||
console.error(validatedMyPagesNavigationRefs.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedMyPagesNavigationRefs.data)
|
||||
const tags = generateTags(lang, connections)
|
||||
const navigation =
|
||||
validatedMyPagesNavigationRefs.data.all_navigation_my_pages.items[0]
|
||||
tags.push(generateTag(lang, navigation.system.uid))
|
||||
|
||||
const response = await request<GetNavigationMyPagesData>(
|
||||
GetNavigationMyPages,
|
||||
{ locale: lang },
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
console.error("Bad response for `GetNavigationMyPages`")
|
||||
console.error({ input: lang })
|
||||
console.error({ response })
|
||||
throw internalServerError()
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigation = navigationPayloadSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
if (!validatedMyPagesNavigation.success) {
|
||||
console.error("Bad validation for `GetNavigationMyPages`")
|
||||
console.error(validatedMyPagesNavigation.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const menuItem =
|
||||
validatedMyPagesNavigation.data.all_navigation_my_pages.items[0]
|
||||
|
||||
const nav = {
|
||||
items: mapMenuItems(menuItem.items),
|
||||
title: menuItem.title,
|
||||
}
|
||||
|
||||
const validatedNav = getNavigationSchema.safeParse(nav)
|
||||
if (!validatedNav.success) {
|
||||
console.error("Bad validation for `getNavigationSchema`")
|
||||
console.error(validatedNav.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return validatedNav.data
|
||||
} catch (error) {
|
||||
console.info(`Get My Pages Navigation Error`)
|
||||
console.error(error)
|
||||
throw internalServerError()
|
||||
if (!refsResponse.data) {
|
||||
throw notFound(refsResponse)
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigationRefs =
|
||||
navigationRefsPayloadSchema.safeParse(refsResponse.data)
|
||||
if (!validatedMyPagesNavigationRefs.success) {
|
||||
throw internalServerError(validatedMyPagesNavigationRefs.error)
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedMyPagesNavigationRefs.data)
|
||||
|
||||
const tags = [
|
||||
generateTags(lang, connections),
|
||||
generateTag(
|
||||
lang,
|
||||
validatedMyPagesNavigationRefs.data.all_navigation_my_pages.items[0]
|
||||
.system.uid
|
||||
),
|
||||
].flat()
|
||||
|
||||
const response = await request<GetNavigationMyPagesData>(
|
||||
GetNavigationMyPages,
|
||||
{ locale: lang },
|
||||
{ next: { tags } }
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const validatedMyPagesNavigation = navigationPayloadSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
if (!validatedMyPagesNavigation.success) {
|
||||
throw internalServerError(validatedMyPagesNavigation.error)
|
||||
}
|
||||
|
||||
const menuItem =
|
||||
validatedMyPagesNavigation.data.all_navigation_my_pages.items[0]
|
||||
|
||||
const nav = {
|
||||
items: mapMenuItems(menuItem.items),
|
||||
title: menuItem.title,
|
||||
}
|
||||
|
||||
const validatedNav = getNavigationSchema.safeParse(nav)
|
||||
if (!validatedNav.success) {
|
||||
throw internalServerError(validatedNav.error)
|
||||
}
|
||||
|
||||
return validatedNav.data
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user