Merged in feat/sw-2872-dependency-inject-app-context-in-trpc-package (pull request #2478)
feat(SW-2872) Dependency inject app context in trpc package * Move appRouter to trpc package * WIP Move serverClient to trpc package Doesn't handle errors yet * Don't use global * Use trpc everywhere Approved-by: Linus Flood
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import { fetchRequestHandler } from "@trpc/server/adapters/fetch"
|
||||
|
||||
import { appRouter } from "@scandic-hotels/trpc/routers/appRouter"
|
||||
|
||||
import { createAppContext } from "@/lib/trpc/server"
|
||||
import { appRouter } from "@/server"
|
||||
|
||||
async function handler(req: Request) {
|
||||
return fetchRequestHandler({
|
||||
|
||||
@@ -4,7 +4,6 @@ import { headers } from "next/headers"
|
||||
import { cache } from "react"
|
||||
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import { languageSchema } from "@scandic-hotels/common/utils/languages"
|
||||
|
||||
const getRef = cache(() => ({ current: undefined as Lang | undefined }))
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
import { createTRPCReact } from "@trpc/react-query"
|
||||
|
||||
import type { AppRouter } from "@scandic-hotels/trpc/routers/appRouter"
|
||||
import type { inferRouterInputs, inferRouterOutputs } from "@trpc/server"
|
||||
|
||||
import type { AppRouter } from "@/server"
|
||||
|
||||
export const trpc = createTRPCReact<AppRouter>()
|
||||
|
||||
export type RouterInput = inferRouterInputs<AppRouter>
|
||||
|
||||
@@ -1,22 +1,21 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
import { TRPCError } from "@trpc/server"
|
||||
import { cookies, headers } from "next/headers"
|
||||
import { redirect } from "next/navigation"
|
||||
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { createCallerFactory } from "@scandic-hotels/trpc"
|
||||
import { createContext } from "@scandic-hotels/trpc/context"
|
||||
import {
|
||||
appServerClient,
|
||||
configureServerClient,
|
||||
} from "@scandic-hotels/trpc/serverClient"
|
||||
|
||||
import { login } from "@/constants/routes/handleAuth"
|
||||
import { webviews } from "@/constants/routes/webviews"
|
||||
import { appRouter } from "@/server"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
|
||||
import type { Session } from "next-auth"
|
||||
|
||||
const createCaller = createCallerFactory(appRouter)
|
||||
|
||||
export async function createAppContext() {
|
||||
const headersList = await headers()
|
||||
const cookie = await cookies()
|
||||
@@ -49,17 +48,13 @@ export async function createAppContext() {
|
||||
return ctx
|
||||
}
|
||||
|
||||
configureServerClient(createAppContext)
|
||||
|
||||
export async function serverClient() {
|
||||
const ctx = await createAppContext()
|
||||
|
||||
return createCaller(ctx, {
|
||||
onError: ({ ctx, error, input, path, type }) => {
|
||||
console.error(`[serverClient] error for ${type}: ${path}`, error)
|
||||
|
||||
if (input) {
|
||||
console.error(`[serverClient] received input:`, input)
|
||||
}
|
||||
|
||||
return appServerClient(ctx, {
|
||||
onError: ({ ctx, error }) => {
|
||||
if (error instanceof TRPCError) {
|
||||
if (error.code === "UNAUTHORIZED") {
|
||||
let lang = Lang.en
|
||||
@@ -90,19 +85,6 @@ export async function serverClient() {
|
||||
redirect(redirectUrl)
|
||||
}
|
||||
}
|
||||
|
||||
Sentry.captureException(error, {
|
||||
extra: {
|
||||
input,
|
||||
path,
|
||||
type,
|
||||
url: ctx?.url,
|
||||
lang: ctx?.lang,
|
||||
pathname: ctx?.pathname,
|
||||
contentType: ctx?.contentType,
|
||||
uid: ctx?.uid,
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -66,7 +66,6 @@
|
||||
"@vercel/otel": "^1.12.0",
|
||||
"@vis.gl/react-google-maps": "^1.5.2",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clean-deep": "^3.4.0",
|
||||
"contentstack": "^3.25.3",
|
||||
"date-fns": "^4.1.0",
|
||||
"dayjs": "^1.11.13",
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
/** Routers */
|
||||
import { router } from "@scandic-hotels/trpc"
|
||||
import { autocompleteRouter } from "@scandic-hotels/trpc/routers/autocomplete"
|
||||
import { bookingRouter } from "@scandic-hotels/trpc/routers/booking"
|
||||
import { contentstackRouter } from "@scandic-hotels/trpc/routers/contentstack"
|
||||
import { hotelsRouter } from "@scandic-hotels/trpc/routers/hotels"
|
||||
import { navigationRouter } from "@scandic-hotels/trpc/routers/navigation"
|
||||
import { partnerRouter } from "@scandic-hotels/trpc/routers/partners"
|
||||
import { userRouter } from "@scandic-hotels/trpc/routers/user"
|
||||
|
||||
export const appRouter = router({
|
||||
booking: bookingRouter,
|
||||
contentstack: contentstackRouter,
|
||||
hotel: hotelsRouter,
|
||||
user: userRouter,
|
||||
partner: partnerRouter,
|
||||
navigation: navigationRouter,
|
||||
autocomplete: autocompleteRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
@@ -1,15 +0,0 @@
|
||||
import cleaner from "clean-deep"
|
||||
|
||||
/**
|
||||
* Function to remove empty objects from a fetched content type.
|
||||
* Used since Contentstack returns empty objects for all non
|
||||
* queried in modular blocks.
|
||||
*/
|
||||
export function removeEmptyObjects<T>(obj: T) {
|
||||
return cleaner(obj, {
|
||||
emptyArrays: false,
|
||||
emptyStrings: false,
|
||||
nullValues: false,
|
||||
undefinedValues: false,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user