File
Blob: src/worker/routes/api/me.ts
| 1 | import { Hono } from "hono"; |
| 2 | |
| 3 | import { MeResponseSchema } from "@/worker/openapi/schemas"; |
| 4 | import { describeJson } from "@/worker/routes/api/openapi"; |
| 5 | import { subjectForSession } from "@/worker/routes/api/session"; |
| 6 | import type { AppContext, AppEnv } from "@/worker/types"; |
| 7 | |
| 8 | function controlPlaneBaseUrl(c: AppContext): URL { |
| 9 | const url = new URL(c.req.url); |
| 10 | url.pathname = "/"; |
| 11 | url.search = ""; |
| 12 | url.hash = ""; |
| 13 | return url; |
| 14 | } |
| 15 | |
| 16 | export async function meBody(c: AppContext) { |
| 17 | const subject = await subjectForSession(c); |
| 18 | if (!subject) throw new Error("Authenticated subject not found"); |
| 19 | const base = controlPlaneBaseUrl(c); |
| 20 | const protocol = base.protocol; |
| 21 | const controlHost = c.env.CONTROL_PLANE_HOST; |
| 22 | const davHost = `${subject.hostLabel}${c.env.SUBJECT_HOST_SUFFIX}`; |
| 23 | return { |
| 24 | subject_id: subject.id, |
| 25 | host_label: subject.hostLabel, |
| 26 | control_plane_host: controlHost, |
| 27 | dav_host: davHost, |
| 28 | urls: { |
| 29 | api: `${protocol}//${controlHost}/api/v1/`, |
| 30 | root: `${protocol}//${davHost}/`, |
| 31 | files: `${protocol}//${davHost}/files/`, |
| 32 | caldav: `${protocol}//${davHost}/calendars/`, |
| 33 | carddav: `${protocol}//${davHost}/addressbooks/`, |
| 34 | }, |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | export function createMeApiRoutes(): Hono<AppEnv> { |
| 39 | const api = new Hono<AppEnv>(); |
| 40 | api.get("/", describeJson("Get current subject metadata", "me", MeResponseSchema), async (c) => |
| 41 | c.json(await meBody(c)), |
| 42 | ); |
| 43 | return api; |
| 44 | } |