64});
65
66export default app.fetch;
67
58app.post("/comment", async c => {
59 const formData = await c.req.parseBody();
60 const text = formData.get('text'); // using get method to fetch the value
61
62 // Insert the comment into the database
67});
68
69export default app.fetch;
70
69});
70
71export default app.fetch;
72
71});
72
73export default app.fetch;
74
75
68});
69
70export default app.fetch;
71
35 }
36
37 // Fetch all comments from the database
38 const comments = await sqlite.all("SELECT content, created_at FROM comments ORDER BY created_at DESC");
39
21const app = new Hono();
22
23// Helper function to fetch all comments from the database
24async function fetchComments() {
25 const result = await sqlite.execute(`SELECT * FROM comments ORDER BY timestamp DESC`);
26 return result.rows;
29// Main route: serves the HTML form and displays comments
30app.get("/", async (c) => {
31 const comments = await fetchComments();
32 const commentsHTML = comments.map(
33 (row) => `<div><strong>${row[1]}</strong> (${row[3]}): <p>${row[2]}</p></div>`
76});
77
78// Export the fetch handler to be used by the runtime
79export default app.fetch;
80
64});
65
66export default app.fetch;
67
53});
54
55export default app.fetch;
56
1export async function fetchVal(valId: string) {
2 try {
3 const response = await fetch(`https://api.val.town/v1/vals/${valId}`);
4 if (!response.ok) {
5 console.error(`Error fetching val with ID ${valId}:`, response.statusText);
6 return null;
7 }
8 return await response.json();
9 } catch (fetchError) {
10 console.error(`Network error fetching val ${valId}:`, fetchError);
11 return null;
12 }
15export async function evalCode(code: string) {
16 try {
17 const response = await fetch(`https://api.val.town/v1/eval`, {
18 method: 'POST',
19 headers: {
40 console.log(`Code evaluation result:`, result);
41 return { result };
42 } catch (fetchError) {
43 console.error(`Network error evaluating code:`, fetchError);
44 return { error: 'Network error: Unable to evaluate code' };
45 }
104
105export function injectValContent(valId: string, elementId: string) {
106 return fetchVal(valId).then(data => {
107 if (data) {
108 const valElement = document.getElementById(elementId);