summaryrefslogtreecommitdiff
path: root/api/create-subscription.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/create-subscription.ts')
-rw-r--r--api/create-subscription.ts26
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,
+};