Unlisted
Code
HTTP
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)) {
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
mbo-htmlinhydersvps.web.val.run
Updated: August 15, 2024