1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Hono } from "jsr:@hono/hono";
import { createClient } from "npm:@libsql/client";
export function createTursoProxy(databaseUrl: string, authToken: string) {
const client = createClient({
url: `${databaseUrl}?authToken=${Deno.env.get("TURSO_AUTH_TOKEN")}`,
});
const app = new Hono();
app.post("/v1/sqlite/batch", async (c) => {
const { statements, mode } = await c.req.json();
if (!statements) {
return new Response("No statements", { status: 400 });
}
const res = await client.batch(statements, mode);
return new Response(JSON.stringify(res));
});
app.post("/v1/sqlite/execute", async (c) => {
const { statement } = await c.req.json();
if (!statement) {
return new Response("No statement", { status: 400 });
}
const res = await client.execute(statement);
return new Response(JSON.stringify(res));
});
app.all("*", async (c) => {
const { path } = c.req;
return await fetch(`https://api.val.town${path}`, {
method: c.req.method,
headers: c.req.raw.headers,
body: c.req.raw.body,
});
});
return app.fetch;
}