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;
}