From ff8ab42aba43e26f8dae88e40fcd2c7f76d9d451 Mon Sep 17 00:00:00 2001 From: Alok Date: Fri, 9 Aug 2024 16:19:36 +0530 Subject: [PATCH] Use JSON response in API --- src/pages/api/leadform.ts | 19 ++++++++++++++----- src/pages/api/newsletter.ts | 17 +++++++++++++---- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/pages/api/leadform.ts b/src/pages/api/leadform.ts index 2f83b1f7..3183034c 100644 --- a/src/pages/api/leadform.ts +++ b/src/pages/api/leadform.ts @@ -2,7 +2,7 @@ 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"); @@ -10,18 +10,27 @@ export const POST: APIRoute = async ({ request, locals}: APIContext) => { 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' } + }); }; diff --git a/src/pages/api/newsletter.ts b/src/pages/api/newsletter.ts index b6e217e3..ed85b687 100644 --- a/src/pages/api/newsletter.ts +++ b/src/pages/api/newsletter.ts @@ -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' } + }); };