Skip to content

Commit

Permalink
Use JSON response in API
Browse files Browse the repository at this point in the history
  • Loading branch information
theWhiteWulfy committed Aug 9, 2024
1 parent be536da commit ff8ab42
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/pages/api/leadform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,35 @@ export const prerender = false; //This will not work without this line

import type { APIRoute, APIContext } from 'astro';

export const POST: APIRoute = async ({ request, locals}: APIContext) => {
export const POST: APIRoute = async ({ request, locals }: APIContext) => {
const data = await request.formData();
const name = data.get("usrname");
const email = data.get("email");
const message = data.get("msg");
const refer = data.get("ref");

if (!locals || !locals.runtime || !locals.runtime.env || !locals.runtime.env.DB) {
return new Response('Database not configured', { status: 500 });
return new Response(JSON.stringify({ error: 'Database not configured' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}

const { DB } = locals.runtime.env;

if (!name || !email || !message || !refer) {
return new Response('Missing required fields', { status: 400 });
return new Response(JSON.stringify({ error: 'Missing required fields' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}

// Call a function to save the email to the D1 database
const query = 'INSERT INTO leads (name, email, refer, message, timestamp) VALUES (?1, ?2, ?3, ?4, CURRENT_TIMESTAMP)';
await DB.prepare(query).bind(name, email, refer, message).run();

return new Response('Email submitted successfully', { status: 200 });
return new Response(JSON.stringify({ message: 'Submitted successfully' }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};
17 changes: 13 additions & 4 deletions src/pages/api/newsletter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@ export const prerender = false; //This will not work without this line

import type { APIRoute, APIContext } from 'astro';

export const POST: APIRoute = async ({ request, locals}: APIContext) => {
export const POST: APIRoute = async ({ request, locals }: APIContext) => {
const formData = await request.formData();
const email = formData.get('subsemail');

if (!locals || !locals.runtime || !locals.runtime.env || !locals.runtime.env.DB) {
return new Response('Database not configured', { status: 500 });
return new Response(JSON.stringify({ error: 'Database not configured' }), {
status: 500,
headers: { 'Content-Type': 'application/json' }
});
}

const { DB } = locals.runtime.env;

if (typeof email !== 'string') {
return new Response('Invalid email', { status: 400 });
return new Response(JSON.stringify({ error: 'Missing or wrong input' }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
});
}

// Call a function to save the email to the D1 database
const query = 'INSERT INTO newsletter (email, timestamp) VALUES (?1, CURRENT_TIMESTAMP)';
await DB.prepare(query).bind(email).run();

return new Response('Email submitted successfully', { status: 200 });
return new Response(JSON.stringify({ message: 'Submitted successfully' }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
});
};

0 comments on commit ff8ab42

Please sign in to comment.