forked from
rocksky.app/rocksky
A decentralized music tracking and discovery platform built on AT Protocol 馃幍
1/**
2 * Welcome to Cloudflare Workers! This is your first worker.
3 *
4 * - Run `npm run dev` in your terminal to start a development server
5 * - Open a browser tab at http://localhost:8787/ to see your worker in action
6 * - Run `npm run deploy` to publish your worker
7 *
8 * Bind resources to your worker in `wrangler.jsonc`. After adding bindings, a type definition for the
9 * `Env` object can be regenerated with `npm run cf-typegen`.
10 *
11 * Learn more at https://developers.cloudflare.com/workers/
12 */
13import { Resend } from 'resend';
14import z from 'zod';
15
16const schema = z.object({
17 email: z.string().email(),
18});
19
20type Payload = z.infer<typeof schema>;
21
22export default {
23 async fetch(request, env, ctx): Promise<Response> {
24 if (request.method !== 'POST') {
25 return new Response('Method Not Allowed', {
26 status: 405,
27 headers: { 'content-type': 'text/plain' },
28 });
29 }
30
31 // verify bearer token
32 const auth = request.headers.get('Authorization');
33 if (!auth || auth.split(' ')[1] !== env.BEARER_TOKEN) {
34 return new Response('Unauthorized', {
35 status: 401,
36 headers: { 'content-type': 'text/plain' },
37 });
38 }
39
40 // Get the request body and validate it
41 const body = await request.json();
42 const payload = schema.parse(body) as Payload;
43
44 const resend = new Resend(env.RESEND_API_KEY);
45 await resend.emails.send({
46 from: 'beta@rocksky.app',
47 to: ['tsiry.sndr@gmail.com'],
48 subject: 'New beta user registered',
49 html: `<p>Hi, a new user has registered for the beta version of Rocksky: <strong>${payload.email}</strong></p>`,
50 });
51 return new Response('Email sent!', {
52 headers: { 'content-type': 'text/plain' },
53 });
54 },
55} satisfies ExportedHandler<Env>;