Skip to content

⚡️⚡️⚡️ Self-hosted collaborative bookmark manager to collect, read, annotate, and fully preserve what matters, all in one place.

small refactoring

daniel31x13c5070a9c0c0e
diff --git a/apps/web/pages/api/v1/webhook/index.ts b/apps/web/pages/api/v1/webhook/index.ts
index f44eee6e..3f93f430 100644
--- a/apps/web/pages/api/v1/webhook/index.ts
+++ b/apps/web/pages/api/v1/webhook/index.ts
@@ -1,8 +1,4 @@
-// Will be deprecated soon! Moved to /api/v1/webhooks/stripe
-
-import type { NextApiRequest, NextApiResponse } from "next";
-import handleSubscription from "@/lib/api/billing/handleStripeSubscription";
-import stripeSDK from "@/lib/api/billing/stripeSDK";
+import webhook from "../webhooks/stripe";
export const config = {
api: {
@@ -10,107 +6,4 @@ export const config = {
},
};
-const buffer = (req: NextApiRequest) => {
- return new Promise<Buffer>((resolve, reject) => {
- const chunks: Buffer[] = [];
-
- req.on("data", (chunk: Buffer) => {
- chunks.push(chunk);
- });
-
- req.on("end", () => {
- resolve(Buffer.concat(chunks as any));
- });
-
- req.on("error", reject);
- });
-};
-
-export default async function webhook(
- req: NextApiRequest,
- res: NextApiResponse
-) {
- if (process.env.NEXT_PUBLIC_DEMO === "true")
- return res.status(400).json({
- response:
- "This action is disabled because this is a read-only demo of Linkwarden.",
- });
-
- // see if stripe is already initialized
- if (!process.env.STRIPE_SECRET_KEY || !process.env.STRIPE_WEBHOOK_SECRET) {
- return res.status(400).json({
- response: "This action is disabled because Stripe is not initialized.",
- });
- }
-
- let event = req.body;
-
- const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
-
- const stripe = stripeSDK();
-
- const signature = req.headers["stripe-signature"] as any;
-
- try {
- const body = await buffer(req);
- event = stripe.webhooks.constructEvent(body, signature, endpointSecret);
- } catch (err) {
- console.error(err);
- return res.status(400).send("Webhook signature verification failed.");
- }
-
- const eventType = event.type;
- const data = event.data.object;
-
- const item = data?.items?.data?.[0];
- const periodStart = item?.current_period_start ?? null;
- const periodEnd = item?.current_period_end ?? null;
- const quantity = item?.quantity ?? 1;
-
- try {
- switch (eventType) {
- case "customer.subscription.created":
- await handleSubscription({
- id: data.id,
- active: data.status === "active" || data.status === "trialing",
- quantity,
- periodStart,
- periodEnd,
- action: "customer.subscription.created",
- });
- break;
-
- case "customer.subscription.updated":
- await handleSubscription({
- id: data.id,
- active: data.status === "active" || data.status === "trialing",
- quantity,
- periodStart,
- periodEnd,
- action: "customer.subscription.updated",
- });
- break;
-
- case "customer.subscription.deleted":
- await handleSubscription({
- id: data.id,
- active: false,
- quantity,
- periodStart,
- periodEnd,
- action: "customer.subscription.deleted",
- });
- break;
-
- default:
- console.log(`Unhandled event type ${eventType}`);
- }
- } catch (error) {
- console.error("Error handling webhook event:", error);
- return res.status(500).send("Server Error");
- }
-
- return res.status(200).json({
- response: "Done!",
- });
-}
+export default webhook;
diff --git a/apps/web/pages/api/v1/webhooks/stripe.ts b/apps/web/pages/api/v1/webhooks/stripe.ts
index 37b62e53..f44eee6e 100644
--- a/apps/web/pages/api/v1/webhooks/stripe.ts
+++ b/apps/web/pages/api/v1/webhooks/stripe.ts
@@ -1,4 +1,8 @@
-import webhook from "../webhook/index";
+// Will be deprecated soon! Moved to /api/v1/webhooks/stripe
+
+import type { NextApiRequest, NextApiResponse } from "next";
+import handleSubscription from "@/lib/api/billing/handleStripeSubscription";
+import stripeSDK from "@/lib/api/billing/stripeSDK";
export const config = {
api: {
@@ -6,4 +10,107 @@ export const config = {
},
};
-export default webhook;
+const buffer = (req: NextApiRequest) => {
+ return new Promise<Buffer>((resolve, reject) => {
+ const chunks: Buffer[] = [];
+
+ req.on("data", (chunk: Buffer) => {
+ chunks.push(chunk);
+ });
+
+ req.on("end", () => {
+ resolve(Buffer.concat(chunks as any));
+ });
+
+ req.on("error", reject);
+ });
+};
+
+export default async function webhook(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ if (process.env.NEXT_PUBLIC_DEMO === "true")
+ return res.status(400).json({
+ response:
+ "This action is disabled because this is a read-only demo of Linkwarden.",
+ });
+
+ // see if stripe is already initialized
+ if (!process.env.STRIPE_SECRET_KEY || !process.env.STRIPE_WEBHOOK_SECRET) {
+ return res.status(400).json({
+ response: "This action is disabled because Stripe is not initialized.",
+ });
+ }
+
+ let event = req.body;
+
+ const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
+
+ const stripe = stripeSDK();
+
+ const signature = req.headers["stripe-signature"] as any;
+
+ try {
+ const body = await buffer(req);
+ event = stripe.webhooks.constructEvent(body, signature, endpointSecret);
+ } catch (err) {
+ console.error(err);
+ return res.status(400).send("Webhook signature verification failed.");
+ }
+
+ const eventType = event.type;
+ const data = event.data.object;
+
+ const item = data?.items?.data?.[0];
+ const periodStart = item?.current_period_start ?? null;
+ const periodEnd = item?.current_period_end ?? null;
+ const quantity = item?.quantity ?? 1;
+
+ try {
+ switch (eventType) {
+ case "customer.subscription.created":
+ await handleSubscription({
+ id: data.id,
+ active: data.status === "active" || data.status === "trialing",
+ quantity,
+ periodStart,
+ periodEnd,
+ action: "customer.subscription.created",
+ });
+ break;
+
+ case "customer.subscription.updated":
+ await handleSubscription({
+ id: data.id,
+ active: data.status === "active" || data.status === "trialing",
+ quantity,
+ periodStart,
+ periodEnd,
+ action: "customer.subscription.updated",
+ });
+ break;
+
+ case "customer.subscription.deleted":
+ await handleSubscription({
+ id: data.id,
+ active: false,
+ quantity,
+ periodStart,
+ periodEnd,
+ action: "customer.subscription.deleted",
+ });
+ break;
+
+ default:
+ console.log(`Unhandled event type ${eventType}`);
+ }
+ } catch (error) {
+ console.error("Error handling webhook event:", error);
+ return res.status(500).send("Server Error");
+ }
+
+ return res.status(200).json({
+ response: "Done!",
+ });
+}