Skip to content

Commit 35548da

Browse files
committed
update api
1 parent c945b48 commit 35548da

File tree

5 files changed

+236
-76
lines changed

5 files changed

+236
-76
lines changed

apps/api/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"cf-typegen": "wrangler types --env-interface CloudflareBindings"
88
},
99
"dependencies": {
10-
"hono": "^4.9.10"
10+
"@supabase/supabase-js": "^2.58.0",
11+
"@t3-oss/env-core": "^0.13.8",
12+
"hono": "^4.9.10",
13+
"zod": "^4.1.12"
1114
},
1215
"devDependencies": {
1316
"wrangler": "^4.42.0"

apps/api/src/env.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { createEnv } from "@t3-oss/env-core";
2+
import type { Context } from "hono";
3+
import { env } from "hono/adapter";
4+
import { z } from "zod";
5+
6+
export const getEnv = (c: Context) =>
7+
createEnv({
8+
server: {
9+
PORT: z.coerce.number().default(3000),
10+
OPENAI_DEFAULT_MODEL: z.string().min(1),
11+
OPENAI_BASE_URL: z.string().min(1),
12+
OPENAI_API_KEY: z.string().min(1),
13+
SUPABASE_URL: z.string().min(1),
14+
SUPABASE_SERVICE_ROLE_KEY: z.string().min(1),
15+
},
16+
runtimeEnv: env(c),
17+
emptyStringAsUndefined: true,
18+
});

apps/api/src/index.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,38 @@
11
import { Hono } from "hono";
2+
import { proxy } from "hono/proxy";
3+
4+
import { getEnv } from "./env";
5+
import { supabaseMiddleware } from "./middleware/supabase";
26

37
const app = new Hono();
8+
app.use("/v1", supabaseMiddleware());
9+
10+
app.get("/health", (c) => c.text("OK"));
11+
12+
app.post("/v1/write", async (c) => {
13+
return c.json({ message: "OK" });
14+
});
15+
16+
app.post("/v1/chat/completions", async (c) => {
17+
const { OPENAI_BASE_URL, OPENAI_DEFAULT_MODEL, OPENAI_API_KEY } = getEnv(c);
18+
19+
const data = await c.req.json();
20+
21+
const res = await proxy(
22+
`${OPENAI_BASE_URL}/chat/completions`,
23+
{
24+
method: "POST",
25+
body: JSON.stringify({
26+
...data,
27+
model: OPENAI_DEFAULT_MODEL,
28+
}),
29+
headers: {
30+
Authorization: `Bearer ${OPENAI_API_KEY}`,
31+
},
32+
},
33+
);
434

5-
app.get("/", (c) => {
6-
return c.text("Hello Hono!");
35+
return res;
736
});
837

938
export default app;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createClient } from "@supabase/supabase-js";
2+
import { createMiddleware } from "hono/factory";
3+
4+
import { getEnv } from "../env";
5+
6+
export const supabaseMiddleware = () => {
7+
return createMiddleware(async (c, next) => {
8+
const authHeader = c.req.header("Authorization");
9+
const token = authHeader?.replace("Bearer ", "");
10+
11+
if (!token) {
12+
return c.json({ error: "missing_authorization_header" }, 401);
13+
}
14+
15+
const { SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY } = getEnv(c);
16+
const supabase = createClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY);
17+
const { data, error } = await supabase.auth.getUser(token);
18+
if (error || !data?.user) {
19+
return c.json({ error: "invalid_authorization_header" }, 401);
20+
}
21+
22+
c.set("supabase", supabase);
23+
c.set("user", data.user);
24+
await next();
25+
});
26+
};

0 commit comments

Comments
 (0)