feat: improve structure and error handling

This commit is contained in:
Michael Zetterberg
2024-05-14 15:55:46 +02:00
parent 01587d7fd5
commit f5108d1a8e
104 changed files with 1505 additions and 1570 deletions
+12 -1
View File
@@ -2,7 +2,7 @@ import { initTRPC } from "@trpc/server"
import { env } from "@/env/server"
import { unauthorizedError } from "./errors/trpc"
import { badRequestError, unauthorizedError } from "./errors/trpc"
import { transformer } from "./transformer"
import type { Meta } from "@/types/trpc/meta"
@@ -12,6 +12,17 @@ const t = initTRPC.context<Context>().meta<Meta>().create({ transformer })
export const { createCallerFactory, mergeRouters, router } = t
export const publicProcedure = t.procedure
export const contentstackProcedure = t.procedure.use(async function (opts) {
if (!opts.ctx.uid) {
throw badRequestError("Missing UID in tRPC context")
}
return opts.next({
ctx: {
uid: opts.ctx.uid,
},
})
})
export const protectedProcedure = t.procedure.use(async function (opts) {
const authRequired = opts.meta?.authRequired ?? true
const session = await opts.ctx.auth()