mirror of
https://github.com/kennethnym/aris.git
synced 2026-02-02 05:01:17 +00:00
feat(backend): add location router with tRPC factory pattern
- 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>
This commit is contained in:
@@ -1,5 +1,47 @@
|
||||
import { router } from "./trpc.ts"
|
||||
import { initTRPC, TRPCError } from "@trpc/server"
|
||||
|
||||
export const appRouter = router({})
|
||||
import { createLocationRouter } from "../location/router.ts"
|
||||
import type { LocationService } from "../location/service.ts"
|
||||
import type { Context } from "./context.ts"
|
||||
|
||||
export type AppRouter = typeof appRouter
|
||||
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>
|
||||
|
||||
Reference in New Issue
Block a user