Skip to content
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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions src/pages/api/sendEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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}`,
},
);
}


};
31 changes: 22 additions & 9 deletions src/pages/contact.astro
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@ const page_data = {
};
let message = "";
let messageType = "";
let formUrl = ""
let fetchResponse = ""
Comment on lines +14 to +15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove debugging variables from production code.

The variables formUrl and fetchResponse 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.

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}`;
Expand All @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove debug information from UI.

Displaying formUrl and fetchResponse in the UI exposes internal implementation details and potentially sensitive information to end users. This debug information should be removed from production code.

Remove these lines:

-              <p>{formUrl}</p>
-              <p>{fetchResponse}</p>
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
<p>{formUrl}</p>
<p>{fetchResponse}</p>

</div>
)}
<form
Expand Down
7 changes: 3 additions & 4 deletions src/pages/programs.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Base from "@/layouts/Base.astro";
import Faq from "@/layouts/function-components/Faq.jsx";
import { getEntryBySlug } from "astro:content";
import MapSvg from "@/components/programs/map-svg.astro";
const pricing = await getEntryBySlug("pricing", "index");
const program = await getEntryBySlug("programs", "index");
import config from "@/config/config.json";
const { success_stories } = config.settings;
Expand All @@ -24,9 +23,9 @@ const page_data = {

<Base
title={program.data.title}
meta_title={pricing.data.meta_title}
description={pricing.data.description}
image={pricing.data.image}
meta_title={program.data.meta_title}
description={program.data.description}
image={program.data.image}
>
<section class="page-hero pb-14 pt-16">
<div class="container">
Expand Down