async function server(request: Request): Promise<Response> {
const { blob } = await import("https://esm.town/v/std/blob");
const KEY = new URL(import.meta.url).pathname.split("/").at(-1);
console.log(await blob.getJSON(`${KEY}_subscribers`));
if (request.method === "POST") {
const formData = await request.formData();
const email = formData.get("email");
if (email) {
try {
const subscribers = await blob.getJSON(`${KEY}_subscribers`) || [];
if (subscribers.includes(email)) {
return new Response("this email is already subscribed (◕‿◕)");
}
subscribers.push(email);
await blob.setJSON(`${KEY}_subscribers`, subscribers);
return new Response("you've signed up \(≧▽≦)/");
} catch (error) {
console.error("Error saving email:", error);
return new Response("an error occurred. please try again. (。•́︿•̀。)");
}
} else {
return new Response("please provide an email address (◕‿◕) ");
}
}
return new Response("Send a POST request with an 'email' field to sign up (◕‿◕)");
}
export default server;