diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-11-29 18:15:14 +0000 |
|---|---|---|
| committer | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-11-29 18:15:14 +0000 |
| commit | 9ced895772ebecb2fa330ae2eecd42060abf2a7b (patch) | |
| tree | b08b6a08fd39a11eaef2a2e2b92a661ad00247b0 /api/create-subscription.ts | |
| parent | 960b019a018c2faf8da1c4a4ade6c12b3c21e5bd (diff) | |
Diffstat (limited to 'api/create-subscription.ts')
| -rw-r--r-- | api/create-subscription.ts | 26 |
1 files changed, 26 insertions, 0 deletions
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<RequestBody> = 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, +}; |
