mirror of
https://github.com/kennethnym/aris.git
synced 2026-02-02 05:01:17 +00:00
- Add createLocationRouter with location.update mutation - Refactor tRPC to factory pattern (createTRPC, createTRPCRouter) - Protected procedure by default (all routes require auth) - Replace zod with arktype for input validation - Wire location router in main() with dependency injection Co-authored-by: Ona <no-reply@ona.com>
48 lines
1.0 KiB
TypeScript
48 lines
1.0 KiB
TypeScript
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<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>
|