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:
21
packages/trpc/lib/routers/appRouter.ts
Normal file
21
packages/trpc/lib/routers/appRouter.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/** Routers */
|
||||
import { autocompleteRouter } from "../routers/autocomplete"
|
||||
import { bookingRouter } from "../routers/booking"
|
||||
import { contentstackRouter } from "../routers/contentstack"
|
||||
import { hotelsRouter } from "../routers/hotels"
|
||||
import { navigationRouter } from "../routers/navigation"
|
||||
import { partnerRouter } from "../routers/partners"
|
||||
import { userRouter } from "../routers/user"
|
||||
import { router } from ".."
|
||||
|
||||
export const appRouter = router({
|
||||
booking: bookingRouter,
|
||||
contentstack: contentstackRouter,
|
||||
hotel: hotelsRouter,
|
||||
user: userRouter,
|
||||
partner: partnerRouter,
|
||||
navigation: navigationRouter,
|
||||
autocomplete: autocompleteRouter,
|
||||
})
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
59
packages/trpc/lib/serverClient.ts
Normal file
59
packages/trpc/lib/serverClient.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
import * as Sentry from "@sentry/nextjs"
|
||||
|
||||
import { appRouter } from "./routers/appRouter"
|
||||
import { createCallerFactory } from "."
|
||||
|
||||
import type { Context } from "./context"
|
||||
|
||||
const createCaller = createCallerFactory(appRouter)
|
||||
|
||||
export type CreateContextFn = () => Promise<Context>
|
||||
let createTrpcContext: CreateContextFn | null = null
|
||||
|
||||
export function configureServerClient(createContext: () => Promise<Context>) {
|
||||
createTrpcContext = createContext
|
||||
}
|
||||
|
||||
type OnError = Required<Parameters<typeof createCaller>>[1]["onError"]
|
||||
type ServerClientOptions = {
|
||||
onError?: OnError
|
||||
}
|
||||
export function appServerClient(
|
||||
context: Context,
|
||||
options: ServerClientOptions = {}
|
||||
) {
|
||||
return createCaller(context, {
|
||||
onError: (args) => {
|
||||
const { ctx, error, input, path, type } = args
|
||||
console.error(`[serverClient] error for ${type}: ${path}`, error)
|
||||
if (input) {
|
||||
console.error(`[serverClient] received input:`, input)
|
||||
}
|
||||
|
||||
options.onError?.(args)
|
||||
|
||||
Sentry.captureException(error, {
|
||||
extra: {
|
||||
input,
|
||||
path,
|
||||
type,
|
||||
url: ctx?.url,
|
||||
lang: ctx?.lang,
|
||||
pathname: ctx?.pathname,
|
||||
contentType: ctx?.contentType,
|
||||
uid: ctx?.uid,
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function packageServerClient() {
|
||||
if (!createTrpcContext) {
|
||||
throw new Error(
|
||||
"createTrpcContext is not defined. Did you forget to call configureServerClient?"
|
||||
)
|
||||
}
|
||||
|
||||
return appServerClient(await createTrpcContext())
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
"./errors": "./lib/errors.ts",
|
||||
"./procedures": "./lib/procedures.ts",
|
||||
"./transformer": "./lib/transformer.ts",
|
||||
"./serverClient": "./lib/serverClient.ts",
|
||||
"./utils/generateTag": "./lib/utils/generateTag.ts",
|
||||
"./graphql/request": "./lib/graphql/request.ts",
|
||||
"./graphql/batchRequest": "./lib/graphql/batchRequest.ts",
|
||||
@@ -28,6 +29,7 @@
|
||||
"./routers/partners/*": "./lib/routers/partners/*.ts",
|
||||
"./routers/autocomplete/*": "./lib/routers/autocomplete/*.ts",
|
||||
"./routers/navigation/*": "./lib/routers/navigation/*.ts",
|
||||
"./routers/appRouter": "./lib/routers/appRouter.ts",
|
||||
"./enums/*": "./lib/enums/*.ts",
|
||||
"./types/*": "./lib/types/*.ts",
|
||||
"./constants/*": "./lib/constants/*.ts",
|
||||
|
||||
Reference in New Issue
Block a user