-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fixing fetch link #24
base: main
Are you sure you want to change the base?
Changes from all commits
21c29c1
d0bb4d6
f766184
70cb0fc
19e181b
279e20a
a66dfcf
fb72ef1
00db1d1
8b55e18
267f99c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,6 @@ const resend = new Resend(import.meta.env.RESEND_API_KEY); | |
|
||
export const POST: APIRoute = async ({ request }) => { | ||
const data = await request.formData(); | ||
|
||
const name = data.get("name"); | ||
const email = data.get("email"); | ||
const message = data.get("message"); | ||
|
@@ -23,37 +22,51 @@ export const POST: APIRoute = async ({ request }) => { | |
); | ||
} // Sending information to Resend | ||
|
||
const sendResend = await resend.emails.send({ | ||
from: "[email protected]", | ||
to: "[email protected]", | ||
subject: `Submission from ${name}`, | ||
html: `<p> | ||
try { | ||
const sendResend = await resend.emails.send({ | ||
from: "[email protected]", | ||
to: "[email protected]", | ||
subject: `Submission from ${name}`, | ||
html: `<p> | ||
From: ${name} | ||
Email: ${email} | ||
Message: ${message} | ||
</p>`, | ||
}); | ||
}); | ||
|
||
if (sendResend.data) { | ||
if (sendResend.data) { | ||
return new Response( | ||
JSON.stringify({ | ||
message: `Message successfully sent!`, | ||
}), | ||
{ | ||
status: 200, | ||
statusText: "OK", | ||
}, | ||
); | ||
} else { | ||
return new Response( | ||
JSON.stringify({ | ||
message: `Message failed to send: ${sendResend.error}`, | ||
}), | ||
{ | ||
status: 500, | ||
statusText: `Internal Server Error: ${sendResend.error}`, | ||
}, | ||
); | ||
} | ||
} catch (error) { | ||
console.log(error) | ||
return new Response( | ||
JSON.stringify({ | ||
message: `Message successfully sent!`, | ||
}), | ||
{ | ||
status: 200, | ||
statusText: "OK", | ||
}, | ||
); | ||
} else { | ||
console.log(sendResend.error) | ||
return new Response( | ||
JSON.stringify({ | ||
message: `Message failed to send: ${sendResend.error}`, | ||
message: `Message failed to send: ${error}`, | ||
}), | ||
{ | ||
status: 500, | ||
statusText: `Internal Server Error: ${sendResend.error}`, | ||
statusText: `Internal Server Error: ${error}`, | ||
}, | ||
); | ||
} | ||
|
||
|
||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,24 +11,35 @@ const page_data = { | |||||
}; | ||||||
let message = ""; | ||||||
let messageType = ""; | ||||||
let formUrl = "" | ||||||
let fetchResponse = "" | ||||||
if (Astro.request.method === "POST") { | ||||||
try { | ||||||
const formData = await Astro.request.formData(); | ||||||
const url = new URL(Astro.request.url).href; | ||||||
const response = await fetch(url.replace('/contact', '/api/sendEmail'), { | ||||||
const url = new URL(Astro.request.url).origin; | ||||||
const response = await fetch(`${url}/api/sendEmail`, { | ||||||
method: "POST", | ||||||
body: formData, | ||||||
}); | ||||||
|
||||||
const data = await response.json(); | ||||||
formUrl = `${url}/api/sendEmail` | ||||||
|
||||||
if (response.status === 200) { | ||||||
message = data.message; | ||||||
messageType = "success"; // Success | ||||||
} else { | ||||||
message = data.message || "Something went wrong. Please try again."; | ||||||
messageType = "error"; // Failure | ||||||
const clonedResponse = response.clone(); | ||||||
try { | ||||||
const data = await response.json(); | ||||||
if (response.status === 200) { | ||||||
message = data.message; | ||||||
messageType = "success"; // Success | ||||||
} else { | ||||||
message = "Something went wrong. Please try again."; | ||||||
messageType = "error"; // Failure | ||||||
} | ||||||
} catch (e) { | ||||||
message = "JSON parsing error"; | ||||||
messageType = "error"; | ||||||
fetchResponse = await clonedResponse.text(); | ||||||
} | ||||||
|
||||||
} catch (error) { | ||||||
if (error instanceof Error) { | ||||||
message = `Error: ${error.message}`; | ||||||
|
@@ -55,6 +66,8 @@ if (Astro.request.method === "POST") { | |||||
{message && ( | ||||||
<div class={`message ${messageType === 'success' ? 'success' : 'error'}`}> | ||||||
<p>{message}</p> | ||||||
<p>{formUrl}</p> | ||||||
<p>{fetchResponse}</p> | ||||||
Comment on lines
+69
to
+70
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove debug information from UI. Displaying Remove these lines: - <p>{formUrl}</p>
- <p>{fetchResponse}</p> 📝 Committable suggestion
Suggested change
|
||||||
</div> | ||||||
)} | ||||||
<form | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove debugging variables from production code.
The variables
formUrl
andfetchResponse
appear to be used for debugging purposes. Exposing internal URLs and raw error responses to end users is not recommended in production code as it could reveal sensitive implementation details.Remove these debug variables and their corresponding display in the UI.