-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworker.js
More file actions
66 lines (59 loc) · 1.59 KB
/
worker.js
File metadata and controls
66 lines (59 loc) · 1.59 KB
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
56
57
58
59
60
61
62
63
64
65
66
const CORS_HEADERS = {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, OPTIONS",
}
export default {
async fetch(request, env) {
if (request.method === "OPTIONS") {
return new Response(null, {
headers: {
...CORS_HEADERS,
"Access-Control-Allow-Headers":
request.headers.get("Access-Control-Request-Headers") || "",
},
})
}
if (request.method !== "POST") {
return new Response("Supported request methods: OPTIONS, POST", {
headers: CORS_HEADERS,
})
}
const body = await request.json()
if (!body.toEmail) {
return new Response(
JSON.stringify({ error: "Target email missing" }),
{ status: 400, headers: CORS_HEADERS }
)
}
const apiKey = env.RESEND_API_KEY
const fromAddress =
env.RESEND_FROM_ADDRESS || "Nexment <no-reply@nexment.ouorz.com>"
const response = await fetch("https://api.resend.com/emails", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
},
body: JSON.stringify({
from: fromAddress,
to: [body.toEmail],
subject: `You received a new reply from ${body.fromName}!`,
html: `
<br/>
<h3><b>You received a new reply on Nexment.</b></h3>
<br/>
<p>From <i>${body.fromName}</i>:</p>
<p>> ${body.content}</p>
<br/>
<p>Click <a href="${body.url}">here</a> to reply back.</p>
`,
}),
})
const result = await response.json()
return new Response(JSON.stringify(result), {
status: response.ok ? 200 : 500,
headers: CORS_HEADERS,
})
},
}