From 9ced895772ebecb2fa330ae2eecd42060abf2a7b Mon Sep 17 00:00:00 2001 From: Joe Carstairs <65492573+Sycamost@users.noreply.github.com> Date: Wed, 29 Nov 2023 18:15:14 +0000 Subject: WIP --- api/create-subscription.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 api/create-subscription.ts (limited to 'api/create-subscription.ts') diff --git a/api/create-subscription.ts b/api/create-subscription.ts new file mode 100644 index 0000000..70b1499 --- /dev/null +++ b/api/create-subscription.ts @@ -0,0 +1,26 @@ +import type { VercelRequest, VercelResponse } from '@vercel/node'; + +import { sql } from '@vercel/postgres'; +import { tableSubscriptions } from '_postgres'; + +export default async function handler(request: VercelRequest, response: VercelResponse) { + const { subscriptionId, emailAddress }: Partial = request.body; + + if (!subscriptionId) throw new Error('Missing parameter: subscriptionId.'); + if (!emailAddress) throw new Error('Missing parameter: emailAddress.'); + + try { + const result = await sql` + INSERT INTO ${tableSubscriptions} (id, email_address) + VALUES (${subscriptionId}, ${emailAddress}) + ;`; + return response.status(200).json({ result }); + } catch (error) { + return response.status(500).json({ error }); + } +} + +type RequestBody = { + subscriptionId: string, + emailAddress: string, +}; -- cgit v1.2.3