pr_01m47d15m3e54sn21z27rpy5n9/apps/api/src/index.ts
| 1 | import { Hono } from "hono"; |
| 2 | |
| 3 | import { |
| 4 | type ServiceBinding, |
| 5 | type Viewer, |
| 6 | httpStatus, |
| 7 | identityClient, |
| 8 | reposClient, |
| 9 | } from "@g1t/contracts"; |
| 10 | |
| 11 | import { handleMcp } from "./mcp"; |
| 12 | import { type ApiEnv, operations, operationsByName } from "./operations"; |
| 13 | |
| 14 | type Input = Record<string, unknown>; |
| 15 | /** The Worker's raw bindings; Rust services are reached through clients. */ |
| 16 | type Bindings = Omit<ApiEnv, "IDENTITY" | "REPOS"> & { |
| 17 | IDENTITY: ServiceBinding; |
| 18 | REPOS: ServiceBinding; |
| 19 | }; |
| 20 | type App = { Bindings: Bindings; Variables: { viewer: Viewer; services: ApiEnv } }; |
| 21 | |
| 22 | /** |
| 23 | * REST routes. Each maps an HTTP request onto one operation; `input` builds |
| 24 | * the operation's input from the path, query string and JSON body. |
| 25 | */ |
| 26 | const ROUTES: { |
| 27 | method: "GET" | "POST"; |
| 28 | path: string; |
| 29 | operation: string; |
| 30 | input?: (params: Record<string, string>, query: Input, body: Input) => Input; |
| 31 | }[] = [ |
| 32 | { method: "GET", path: "/v1/user", operation: "whoami" }, |
| 33 | { method: "GET", path: "/v1/repos", operation: "list_repos", input: (_p, q) => ({ query: q.q }) }, |
| 34 | { method: "POST", path: "/v1/repos", operation: "create_repo", input: (_p, _q, b) => b }, |
| 35 | { method: "GET", path: "/v1/repos/:owner/:name", operation: "get_repo", input: repo }, |
| 36 | { method: "GET", path: "/v1/repos/:owner/:name/intents", operation: "list_intents", input: (p, q) => ({ ...repo(p), status: q.status }) }, |
| 37 | { method: "POST", path: "/v1/repos/:owner/:name/intents", operation: "open_intent", input: (p, _q, b) => ({ ...b, ...repo(p) }) }, |
| 38 | { method: "GET", path: "/v1/repos/:owner/:name/intents/:number", operation: "get_intent", input: (p) => ({ ...repo(p), number: Number(p.number) }) }, |
| 39 | { method: "GET", path: "/v1/repos/:owner/:name/events", operation: "list_events", input: (p, q) => ({ ...repo(p), before: q.before }) }, |
| 40 | { method: "POST", path: "/v1/intents/:intent_id/attempts", operation: "start_attempt", input: (p, _q, b) => ({ ...b, intent_id: p.intent_id }) }, |
| 41 | { method: "GET", path: "/v1/attempts/:attempt_id", operation: "get_attempt", input: (p) => p }, |
| 42 | { method: "GET", path: "/v1/attempts/:attempt_id/session", operation: "read_session", input: (p, q) => ({ ...p, after: Number(q.after) || 0 }) }, |
| 43 | { method: "POST", path: "/v1/attempts/:attempt_id/session", operation: "record_session", input: (p, _q, b) => ({ ...b, ...p }) }, |
| 44 | { method: "POST", path: "/v1/attempts/:attempt_id/submit", operation: "submit_attempt", input: (p, _q, b) => ({ ...b, ...p }) }, |
| 45 | { method: "POST", path: "/v1/attempts/:attempt_id/abandon", operation: "abandon_attempt", input: (p) => p }, |
| 46 | { method: "POST", path: "/v1/attempts/:attempt_id/ship", operation: "ship_attempt", input: (p) => p }, |
| 47 | ]; |
| 48 | |
| 49 | function repo(params: Record<string, string>): Input { |
| 50 | return { repo: `${params.owner}/${params.name}` }; |
| 51 | } |
| 52 | |
| 53 | const app = new Hono<App>(); |
| 54 | |
| 55 | // `Authorization: Bearer g1t_…`. A missing token is an anonymous viewer; a |
| 56 | // wrong one is rejected so a typo does not silently look signed out. |
| 57 | app.use(async (c, next) => { |
| 58 | const [scheme, token] = (c.req.header("authorization") ?? "").split(" "); |
| 59 | const services: ApiEnv = { |
| 60 | ...c.env, |
| 61 | IDENTITY: identityClient(c.env.IDENTITY), |
| 62 | REPOS: reposClient(c.env.REPOS), |
| 63 | }; |
| 64 | c.set("services", services); |
| 65 | let viewer: Viewer = null; |
| 66 | if (scheme?.toLowerCase() === "bearer" && token) { |
| 67 | viewer = await services.IDENTITY.userForAccessToken(token); |
| 68 | if (!viewer) { |
| 69 | return c.json( |
| 70 | { error: { code: "unauthenticated", message: "Invalid access token." } }, |
| 71 | 401, |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | c.set("viewer", viewer); |
| 76 | await next(); |
| 77 | }); |
| 78 | |
| 79 | app.all("*", async (c, next) => { |
| 80 | if (new URL(c.req.url).hostname.startsWith("mcp.")) { |
| 81 | return handleMcp(c.req.raw, c.get("services"), c.get("viewer")); |
| 82 | } |
| 83 | await next(); |
| 84 | }); |
| 85 | |
| 86 | app.get("/", (c) => |
| 87 | c.json({ |
| 88 | name: "g1t API", |
| 89 | version: "v1", |
| 90 | documentation: "https://g1t.sh/syntaqx/g1t", |
| 91 | operations: operations.map(({ name, description }) => ({ name, description })), |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | for (const route of ROUTES) { |
| 96 | const operation = operationsByName.get(route.operation)!; |
| 97 | app.on(route.method, route.path, async (c) => { |
| 98 | let body: Input = {}; |
| 99 | if (route.method === "POST") { |
| 100 | try { |
| 101 | body = await c.req.json(); |
| 102 | } catch { |
| 103 | // An empty or non-JSON body is treated as no input. |
| 104 | } |
| 105 | } |
| 106 | const input = route.input?.(c.req.param(), c.req.query(), body) ?? {}; |
| 107 | const outcome = await operation.run(c.get("services"), c.get("viewer"), input); |
| 108 | if (outcome.ok) return c.json(outcome.value); |
| 109 | return c.json( |
| 110 | { error: outcome.error }, |
| 111 | httpStatus(outcome.error) as 401 | 403 | 404 | 409 | 422, |
| 112 | ); |
| 113 | }); |
| 114 | } |
| 115 | |
| 116 | app.notFound((c) => |
| 117 | c.json({ error: { code: "not_found", message: "No such endpoint." } }, 404), |
| 118 | ); |
| 119 | |
| 120 | export default app; |