Readme
A self-contained comments system Val. Just fork this val and you have a complete (but extremely minimal) comment system!
Call on the front-end using:
const MY_FORKED_VAL_URL = "https://vez-comments.web.val.run";
const getComments = async () => {
const response = await fetch(MY_FORKED_VAL_URL);
const json = await response.json();
return json;
};
const addComment = async (str) => {
try {
const response = await fetch(MY_FORKED_VAL_URL, {
method: "POST",
body: JSON.stringify(str),
});
if (response.status >= 400 && response.status < 600) {
/* error */
return false;
} else {
/* success */
return true;
}
} catch (e) {
/* error */
return false;
}
};
Here's an example of a blog post where I used the val for the comment system: https://vezwork.github.io/polylab/dist/demo/bidirectionalParse/. Check out "view source"!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { blob } from "https://esm.town/v/std/blob?v=10";
import { email } from "https://esm.town/v/std/email?v=9";
import { Hono } from "npm:hono@3.9.2";
const KEY = import.meta.url.split("?")[0];
export async function addComment(str) {
const comments = await blob.getJSON(KEY) as Array<string> ?? [];
comments.push(str);
await blob.setJSON(KEY, comments);
await email({ text: "New Comment Alert!: " + str });
}
export async function getComments() {
return await blob.getJSON(KEY) as Array<string> ?? [];
}
const app = new Hono();
app.get("/", async (c) => c.json(await getComments()));
app.post("/", async (c) => {
const str = await c.req.json();
await addComment(str);
return c.text("Added comment!", 200);
});
export default app.fetch;
👆 This is a val. Vals are TypeScript snippets of code, written in the browser and run on our servers. Create scheduled functions, email yourself, and persist small pieces of data — all from the browser.