Feature/turbopack * . * . * pin import-in-the-middle * update marker * revert back to using *.graphql.ts Approved-by: Linus Flood
69 lines
1.7 KiB
TypeScript
69 lines
1.7 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
|
|
import { createLogger } from "@scandic-hotels/common/logger/createLogger"
|
|
|
|
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 = {}
|
|
) {
|
|
const serverClientLogger = createLogger("serverClient")
|
|
return createCaller(context, {
|
|
onError: (args) => {
|
|
const { ctx, error, input, path, type } = args
|
|
serverClientLogger.error(`error for ${type}: ${path}`, {
|
|
error: {
|
|
message: error.message,
|
|
stack: error.stack,
|
|
name: error.name,
|
|
code: error.code,
|
|
cause: error.cause,
|
|
},
|
|
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())
|
|
}
|