Unlisted
HTTP (deprecated)
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { blob } from "https://esm.town/v/std/blob?v=10";
import { email } from "https://esm.town/v/std/email?v=11";
interface RSVP {
name: string;
email: string;
status?: "going" | "maybe";
}
/**
* This powers the RSVP feature for https://maxbo.me/html-in-hyde/.
*
* When hit with a GET, it returns a list of _only the names_ of the people who have RSVPed.
* When hit with a POST, it saves the name _and_ email of the person who has RSVPed.
*/
export default async function(req: Request): Promise<Response> {
let rsvps = await blob.getJSON("rsvps") as RSVP[];
rsvps ??= [];
if (req.method === "GET") {
return new Response(JSON.stringify(rsvps.map(rsvp => ({ name: rsvp.name, status: rsvp.status ?? "going" }))), {
headers: { "content-type": "application/json" },
});
}
if (req.method === "POST") {
throw new Response("The event has already happened :^) no more RSVPSs");
const emails = rsvps.map(rsvp => rsvp.email);
const formData = await req.formData();
const name = formData.get("name") as string;
const email = formData.get("email") as string;
const status = formData.get("status") as ("going" | "maybe" | undefined) ?? "maybe";
if (emails.includes(email)) {
// update the status
const rsvp = rsvps.find(rsvp => rsvp.email === email);
if (rsvp) {
rsvp.status = status;
await blob.setJSON("rsvps", rsvps);
return new Response(`Thanks! I've updated your RSVP status to ${status}.`);
}
}
if (!name || !email || !status) {
throw new Response("Missing name, email or status");
}
rsvps.push({ name, email });
await blob.setJSON("rsvps", rsvps);
return new Response("Thanks! You're signed up! I'll send you some reminders before the event :^)");
}
}
mbo-htmlinhydersvps.web.val.run
August 15, 2024