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
import { email as sendEmail } from "https://esm.town/v/std/email?v=11";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
export async function unsubscribe(req: Request) {
const searchParams = new URL(req.url).searchParams;
const emailAddress = searchParams.get("email");
if (!emailAddress) {
// No-op if email query param is missing
return Response.json("");
}
await sqlite.execute({
sql: `
UPDATE students
SET subscribed_at = NULL
WHERE email = ?
`,
args: [emailAddress],
});
sendEmail({
subject: `Someone unsubscribed from Make It Stick (in 10 days, via email)`,
text: `${emailAddress} unsubscribed 😢`,
});
const responseHtml = `
<main>
<p>You've successfully unsubscribed.</p>
<p>If you have any feedback, shoot me an email at <a href="mailto:pete@petemillspaugh.com">pete@petemillspaugh.com</a>.</p>
</main>
`;
return new Response(responseHtml, { headers: { "Content-Type": "text/html" } });
}
👆 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.