Files
penx/server/trpc.ts
2024-08-05 10:06:02 +08:00

72 lines
1.8 KiB
TypeScript

/**
* This is your entry point to setup the root configuration for tRPC on the server.
* - `initTRPC` should only be used once per app.
* - We export only the functionality that we use so we can enforce which base procedures should be used
*
* Learn how to create protected base procedures and other things below:
* @link https://trpc.io/docs/v11/router
* @link https://trpc.io/docs/v11/procedures
*/
import { prisma } from '@/lib/prisma'
import { initTRPC, TRPCError } from '@trpc/server'
import superjson from 'superjson'
import type { Context } from './context'
const t = initTRPC.context<Context>().create({
/**
* @link https://trpc.io/docs/v11/data-transformers
*/
transformer: superjson,
/**
* @link https://trpc.io/docs/v11/error-formatting
*/
errorFormatter({ shape }) {
return shape
},
})
/**
* Create a router
* @link https://trpc.io/docs/v11/router
*/
export const router = t.router
/**
* Create an unprotected procedure
* @link https://trpc.io/docs/v11/procedures
**/
export const publicProcedure = t.procedure
/**
* Merge multiple routers together
* @link https://trpc.io/docs/v11/merging-routers
*/
export const mergeRouters = t.mergeRouters
/**
* Create a server-side caller
* @link https://trpc.io/docs/v11/server/server-side-calls
*/
export const createCallerFactory = t.createCallerFactory
// procedure that asserts that the user is logged in
export const protectedProcedure = t.procedure.use(
async ({ ctx, next, path, ...rest }) => {
if (!ctx.token?.uid) {
throw new TRPCError({
code: 'UNAUTHORIZED',
message: 'user not found',
})
}
// if (['spae.update'].includes(path)) {
// checkSpacePermission(ctx.token.uid, rest.input.id)
// }
return next({
ctx: { token: ctx.token },
})
},
)