import { initTRPC, TRPCError } from "@trpc/server" import { createLocationRouter } from "../location/router.ts" import type { LocationService } from "../location/service.ts" import type { Context } from "./context.ts" interface AuthedContext { user: NonNullable session: NonNullable } function createTRPC() { const t = initTRPC.context().create() const isAuthed = t.middleware(({ ctx, next }) => { if (!ctx.user || !ctx.session) { throw new TRPCError({ code: "UNAUTHORIZED" }) } return next({ ctx: { user: ctx.user, session: ctx.session, }, }) }) return { router: t.router, procedure: t.procedure.use(isAuthed), } } export type TRPC = ReturnType export interface TRPCRouterDeps { locationService: LocationService } export function createTRPCRouter({ locationService }: TRPCRouterDeps) { const t = createTRPC() return t.router({ location: createLocationRouter(t, { locationService }), }) } export type TRPCRouter = ReturnType