|
1 | 1 | import { Hono } from "hono"; |
2 | 2 | import { setCookie } from "hono/cookie"; |
3 | 3 | import { handle } from "hono/vercel"; |
4 | | -import { SignJWT } from "jose"; |
| 4 | +import { SignJWT, jwtVerify } from "jose"; |
| 5 | +import { Pool } from "pg"; |
| 6 | +import { withValidation, ReadonlyJSONValue } from "@rocicorp/zero"; |
| 7 | +import { PushProcessor } from "@rocicorp/zero/server"; |
| 8 | +import { zeroNodePg } from "@rocicorp/zero/server/adapters/pg"; |
| 9 | +import { handleGetQueriesRequest } from "@rocicorp/zero/server"; |
| 10 | +import { AuthData, schema } from "../src/schema"; |
| 11 | +import { createMutators } from "../src/mutators"; |
| 12 | +import { queries, allUsers } from "../src/queries"; |
5 | 13 |
|
6 | 14 | export const config = { |
7 | 15 | runtime: "edge", |
8 | 16 | }; |
9 | 17 |
|
| 18 | +const pool = new Pool({ |
| 19 | + connectionString: process.env.ZERO_UPSTREAM_DB, |
| 20 | +}); |
| 21 | + |
| 22 | +const pushProcessor = new PushProcessor(zeroNodePg(schema, pool)); |
| 23 | + |
10 | 24 | export const app = new Hono().basePath("/api"); |
11 | 25 |
|
12 | 26 | // See seed.sql |
@@ -45,6 +59,71 @@ app.get("/login", async (c) => { |
45 | 59 | return c.text("ok"); |
46 | 60 | }); |
47 | 61 |
|
| 62 | +async function getAuthData(request: Request): Promise<AuthData | undefined> { |
| 63 | + const authHeader = request.headers.get("Authorization"); |
| 64 | + const token = authHeader?.replace("Bearer ", ""); |
| 65 | + |
| 66 | + if (!token) { |
| 67 | + return undefined; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + const { payload } = await jwtVerify( |
| 72 | + token, |
| 73 | + new TextEncoder().encode(must(process.env.ZERO_AUTH_SECRET)), |
| 74 | + ); |
| 75 | + return { sub: payload.sub as string | null }; |
| 76 | + } catch { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// Get Queries endpoint for synced queries |
| 83 | +app.post("/get-queries", async (c) => { |
| 84 | + const authData = await getAuthData(c.req.raw); |
| 85 | + |
| 86 | + return c.json( |
| 87 | + await handleGetQueriesRequest( |
| 88 | + (name, args) => getQuery(authData, name, args), |
| 89 | + schema, |
| 90 | + c.req.raw, |
| 91 | + ), |
| 92 | + ); |
| 93 | +}); |
| 94 | + |
| 95 | +const validated = Object.fromEntries( |
| 96 | + Object.values(queries).map((q) => [q.queryName, withValidation(q)]) |
| 97 | +); |
| 98 | + |
| 99 | +function getQuery( |
| 100 | + authData: AuthData | undefined, |
| 101 | + name: string, |
| 102 | + args: readonly ReadonlyJSONValue[], |
| 103 | +) { |
| 104 | + const q = validated[name]; |
| 105 | + if (!q) { |
| 106 | + throw new Error(`No such query: ${name}`); |
| 107 | + } |
| 108 | + // withValidation returns a function that takes (context, ...args) |
| 109 | + // and returns a Query. We need to return { query: Query } |
| 110 | + return { |
| 111 | + query: q(authData, ...args), |
| 112 | + }; |
| 113 | +} |
| 114 | + |
| 115 | +// Push endpoint for custom mutators |
| 116 | +app.post("/push", async (c) => { |
| 117 | + const authData = await getAuthData(c.req.raw); |
| 118 | + |
| 119 | + const result = await pushProcessor.process( |
| 120 | + createMutators(authData), |
| 121 | + c.req.raw, |
| 122 | + ); |
| 123 | + |
| 124 | + return c.json(result); |
| 125 | +}); |
| 126 | + |
48 | 127 | export default handle(app); |
49 | 128 |
|
50 | 129 | function must<T>(val: T) { |
|
0 commit comments