2023-11-13 15:27:36 +01:00
|
|
|
import { TRPCError, initTRPC } from '@trpc/server'
|
2022-11-29 10:02:40 +01:00
|
|
|
import { OpenApiMeta } from 'trpc-openapi'
|
|
|
|
|
import superjson from 'superjson'
|
2023-02-22 11:40:04 +01:00
|
|
|
import { Context } from './context'
|
2023-10-07 12:03:42 +02:00
|
|
|
import * as Sentry from '@sentry/nextjs'
|
2022-11-29 10:02:40 +01:00
|
|
|
|
2023-02-22 11:40:04 +01:00
|
|
|
const t = initTRPC.context<Context>().meta<OpenApiMeta>().create({
|
2022-11-29 10:02:40 +01:00
|
|
|
transformer: superjson,
|
|
|
|
|
})
|
|
|
|
|
|
2023-11-13 15:27:36 +01:00
|
|
|
export const router = t.router
|
|
|
|
|
|
2023-10-07 12:03:42 +02:00
|
|
|
const sentryMiddleware = t.middleware(
|
|
|
|
|
Sentry.Handlers.trpcMiddleware({
|
|
|
|
|
attachRpcInput: true,
|
|
|
|
|
})
|
|
|
|
|
)
|
|
|
|
|
|
2023-11-13 15:27:36 +01:00
|
|
|
export const publicProcedure = t.procedure.use(sentryMiddleware)
|
|
|
|
|
|
|
|
|
|
const isAuthed = t.middleware(({ next, ctx }) => {
|
|
|
|
|
if (!ctx.user?.id) {
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'UNAUTHORIZED',
|
|
|
|
|
message: 'You need to be authenticated to perform this action',
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-02-22 11:40:04 +01:00
|
|
|
return next({
|
|
|
|
|
ctx: {
|
|
|
|
|
user: ctx.user,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2023-11-13 15:27:36 +01:00
|
|
|
export const authenticatedProcedure = t.procedure.use(
|
|
|
|
|
sentryMiddleware.unstable_pipe(isAuthed)
|
|
|
|
|
)
|