2026-01-25 22:47:53 +00:00
|
|
|
import { initTRPC, TRPCError } from "@trpc/server"
|
2026-01-25 22:19:05 +00:00
|
|
|
|
2026-01-25 22:47:53 +00:00
|
|
|
import { createLocationRouter } from "../location/router.ts"
|
|
|
|
|
import type { LocationService } from "../location/service.ts"
|
|
|
|
|
import type { Context } from "./context.ts"
|
2026-01-25 22:19:05 +00:00
|
|
|
|
2026-01-25 22:47:53 +00:00
|
|
|
interface AuthedContext {
|
|
|
|
|
user: NonNullable<Context["user"]>
|
|
|
|
|
session: NonNullable<Context["session"]>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function createTRPC() {
|
|
|
|
|
const t = initTRPC.context<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<typeof createTRPC>
|
|
|
|
|
|
|
|
|
|
export interface TRPCRouterDeps {
|
|
|
|
|
locationService: LocationService
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createTRPCRouter({ locationService }: TRPCRouterDeps) {
|
|
|
|
|
const t = createTRPC()
|
|
|
|
|
|
|
|
|
|
return t.router({
|
|
|
|
|
location: createLocationRouter(t, { locationService }),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type TRPCRouter = ReturnType<typeof createTRPCRouter>
|