summaryrefslogtreecommitdiff
path: root/api
diff options
context:
space:
mode:
Diffstat (limited to 'api')
-rw-r--r--api/_postgres.ts4
-rw-r--r--api/create-subscription.ts26
2 files changed, 30 insertions, 0 deletions
diff --git a/api/_postgres.ts b/api/_postgres.ts
new file mode 100644
index 0000000..70b25c8
--- /dev/null
+++ b/api/_postgres.ts
@@ -0,0 +1,4 @@
+import env from './_env';
+
+export const tablePrefix = env.ENVIRONMENT === 'prod' ? '' : 'dev_';
+export const tableSubscriptions = `${tablePrefix}subscriptions`;
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,
+};