mirror of
https://github.com/kennethnym/aris.git
synced 2026-02-02 13:11:17 +00:00
Compare commits
4 Commits
feat/locat
...
feat/locat
| Author | SHA1 | Date | |
|---|---|---|---|
| 949b7c8571 | |||
| bd6cc3c963 | |||
| aff9464245 | |||
| 0db6cae82b |
@@ -12,6 +12,9 @@
|
|||||||
"@aris/core": "workspace:*",
|
"@aris/core": "workspace:*",
|
||||||
"@aris/source-location": "workspace:*",
|
"@aris/source-location": "workspace:*",
|
||||||
"@aris/source-weatherkit": "workspace:*",
|
"@aris/source-weatherkit": "workspace:*",
|
||||||
|
"@hono/trpc-server": "^0.3",
|
||||||
|
"@trpc/server": "^11",
|
||||||
|
"arktype": "^2.1.29",
|
||||||
"better-auth": "^1",
|
"better-auth": "^1",
|
||||||
"hono": "^4",
|
"hono": "^4",
|
||||||
"pg": "^8"
|
"pg": "^8"
|
||||||
|
|||||||
33
apps/aris-backend/src/location/router.ts
Normal file
33
apps/aris-backend/src/location/router.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { TRPCError } from "@trpc/server"
|
||||||
|
import { type } from "arktype"
|
||||||
|
|
||||||
|
import { UserNotFoundError } from "../lib/error.ts"
|
||||||
|
import type { TRPC } from "../trpc/router.ts"
|
||||||
|
import type { LocationService } from "./service.ts"
|
||||||
|
|
||||||
|
const locationInput = type({
|
||||||
|
lat: "number",
|
||||||
|
lng: "number",
|
||||||
|
accuracy: "number",
|
||||||
|
timestamp: "Date",
|
||||||
|
})
|
||||||
|
|
||||||
|
export function createLocationRouter(t: TRPC, { locationService }: { locationService: LocationService }) {
|
||||||
|
return t.router({
|
||||||
|
update: t.procedure.input(locationInput).mutation(({ input, ctx }) => {
|
||||||
|
try {
|
||||||
|
locationService.updateUserLocation(ctx.user.id, {
|
||||||
|
lat: input.lat,
|
||||||
|
lng: input.lng,
|
||||||
|
accuracy: input.accuracy,
|
||||||
|
timestamp: input.timestamp,
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof UserNotFoundError) {
|
||||||
|
throw new TRPCError({ code: "NOT_FOUND", message: error.message })
|
||||||
|
}
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,12 +1,34 @@
|
|||||||
|
import { trpcServer } from "@hono/trpc-server"
|
||||||
import { Hono } from "hono"
|
import { Hono } from "hono"
|
||||||
|
|
||||||
import { registerAuthHandlers } from "./auth/http.ts"
|
import { registerAuthHandlers } from "./auth/http.ts"
|
||||||
|
import { LocationService } from "./location/service.ts"
|
||||||
|
import { createContext } from "./trpc/context.ts"
|
||||||
|
import { createTRPCRouter } from "./trpc/router.ts"
|
||||||
|
|
||||||
const app = new Hono()
|
function main() {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
|
||||||
app.get("/health", (c) => c.json({ status: "ok" }))
|
const trpcRouter = createTRPCRouter({ locationService })
|
||||||
|
|
||||||
registerAuthHandlers(app)
|
const app = new Hono()
|
||||||
|
|
||||||
|
app.get("/health", (c) => c.json({ status: "ok" }))
|
||||||
|
|
||||||
|
registerAuthHandlers(app)
|
||||||
|
|
||||||
|
app.use(
|
||||||
|
"/trpc/*",
|
||||||
|
trpcServer({
|
||||||
|
router: trpcRouter,
|
||||||
|
createContext,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
return app
|
||||||
|
}
|
||||||
|
|
||||||
|
const app = main()
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
port: 3000,
|
port: 3000,
|
||||||
|
|||||||
14
apps/aris-backend/src/trpc/context.ts
Normal file
14
apps/aris-backend/src/trpc/context.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import type { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch"
|
||||||
|
|
||||||
|
import { auth } from "../auth/index.ts"
|
||||||
|
|
||||||
|
export async function createContext(opts: FetchCreateContextFnOptions) {
|
||||||
|
const session = await auth.api.getSession({ headers: opts.req.headers })
|
||||||
|
|
||||||
|
return {
|
||||||
|
user: session?.user ?? null,
|
||||||
|
session: session?.session ?? null,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type Context = Awaited<ReturnType<typeof createContext>>
|
||||||
47
apps/aris-backend/src/trpc/router.ts
Normal file
47
apps/aris-backend/src/trpc/router.ts
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
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>
|
||||||
7
bun.lock
7
bun.lock
@@ -20,6 +20,9 @@
|
|||||||
"@aris/core": "workspace:*",
|
"@aris/core": "workspace:*",
|
||||||
"@aris/source-location": "workspace:*",
|
"@aris/source-location": "workspace:*",
|
||||||
"@aris/source-weatherkit": "workspace:*",
|
"@aris/source-weatherkit": "workspace:*",
|
||||||
|
"@hono/trpc-server": "^0.3",
|
||||||
|
"@trpc/server": "^11",
|
||||||
|
"arktype": "^2.1.29",
|
||||||
"better-auth": "^1",
|
"better-auth": "^1",
|
||||||
"hono": "^4",
|
"hono": "^4",
|
||||||
"pg": "^8",
|
"pg": "^8",
|
||||||
@@ -91,6 +94,8 @@
|
|||||||
|
|
||||||
"@better-fetch/fetch": ["@better-fetch/fetch@1.1.21", "", {}, "sha512-/ImESw0sskqlVR94jB+5+Pxjf+xBwDZF/N5+y2/q4EqD7IARUTSpPfIo8uf39SYpCxyOCtbyYpUrZ3F/k0zT4A=="],
|
"@better-fetch/fetch": ["@better-fetch/fetch@1.1.21", "", {}, "sha512-/ImESw0sskqlVR94jB+5+Pxjf+xBwDZF/N5+y2/q4EqD7IARUTSpPfIo8uf39SYpCxyOCtbyYpUrZ3F/k0zT4A=="],
|
||||||
|
|
||||||
|
"@hono/trpc-server": ["@hono/trpc-server@0.3.4", "", { "peerDependencies": { "@trpc/server": "^10.10.0 || >11.0.0-rc", "hono": ">=4.*" } }, "sha512-xFOPjUPnII70FgicDzOJy1ufIoBTu8eF578zGiDOrYOrYN8CJe140s9buzuPkX+SwJRYK8LjEBHywqZtxdm8aA=="],
|
||||||
|
|
||||||
"@noble/ciphers": ["@noble/ciphers@2.1.1", "", {}, "sha512-bysYuiVfhxNJuldNXlFEitTVdNnYUc+XNJZd7Qm2a5j1vZHgY+fazadNFWFaMK/2vye0JVlxV3gHmC0WDfAOQw=="],
|
"@noble/ciphers": ["@noble/ciphers@2.1.1", "", {}, "sha512-bysYuiVfhxNJuldNXlFEitTVdNnYUc+XNJZd7Qm2a5j1vZHgY+fazadNFWFaMK/2vye0JVlxV3gHmC0WDfAOQw=="],
|
||||||
|
|
||||||
"@noble/hashes": ["@noble/hashes@2.0.1", "", {}, "sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw=="],
|
"@noble/hashes": ["@noble/hashes@2.0.1", "", {}, "sha512-XlOlEbQcE9fmuXxrVTXCTlG2nlRXa9Rj3rr5Ue/+tX+nmkgbX720YHh0VR3hBF9xDvwnb8D2shVGOwNx+ulArw=="],
|
||||||
@@ -129,6 +134,8 @@
|
|||||||
|
|
||||||
"@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],
|
"@standard-schema/spec": ["@standard-schema/spec@1.1.0", "", {}, "sha512-l2aFy5jALhniG5HgqrD6jXLi/rUWrKvqN/qJx6yoJsgKhblVd+iqqU4RCXavm/jPityDo5TCvKMnpjKnOriy0w=="],
|
||||||
|
|
||||||
|
"@trpc/server": ["@trpc/server@11.8.1", "", { "peerDependencies": { "typescript": ">=5.7.2" } }, "sha512-P4rzZRpEL7zDFgjxK65IdyH0e41FMFfTkQkuq0BA5tKcr7E6v9/v38DEklCpoDN6sPiB1Sigy/PUEzHENhswDA=="],
|
||||||
|
|
||||||
"@types/bun": ["@types/bun@1.3.6", "", { "dependencies": { "bun-types": "1.3.6" } }, "sha512-uWCv6FO/8LcpREhenN1d1b6fcspAB+cefwD7uti8C8VffIv0Um08TKMn98FynpTiU38+y2dUO55T11NgDt8VAA=="],
|
"@types/bun": ["@types/bun@1.3.6", "", { "dependencies": { "bun-types": "1.3.6" } }, "sha512-uWCv6FO/8LcpREhenN1d1b6fcspAB+cefwD7uti8C8VffIv0Um08TKMn98FynpTiU38+y2dUO55T11NgDt8VAA=="],
|
||||||
|
|
||||||
"@types/node": ["@types/node@25.0.9", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw=="],
|
"@types/node": ["@types/node@25.0.9", "", { "dependencies": { "undici-types": "~7.16.0" } }, "sha512-/rpCXHlCWeqClNBwUhDcusJxXYDjZTyE8v5oTO7WbL8eij2nKhUeU89/6xgjU7N4/Vh3He0BtyhJdQbDyhiXAw=="],
|
||||||
|
|||||||
Reference in New Issue
Block a user