From ab9082158f7ee3fa54553a008c4bcc2d13b85431 Mon Sep 17 00:00:00 2001 From: "Builder.io" Date: Sat, 15 Nov 2025 02:05:03 +0000 Subject: [PATCH] Create API endpoint for getting user following list cgen-549c7a4fa7d849fcbe8fb250d866f5fb --- api/user/following.ts | 62 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 api/user/following.ts diff --git a/api/user/following.ts b/api/user/following.ts new file mode 100644 index 00000000..76040b14 --- /dev/null +++ b/api/user/following.ts @@ -0,0 +1,62 @@ +export const config = { + runtime: "nodejs", +}; + +import { createClient } from "@supabase/supabase-js"; + +const supabaseUrl = process.env.VITE_SUPABASE_URL; +const supabaseServiceRole = process.env.SUPABASE_SERVICE_ROLE; + +if (!supabaseUrl || !supabaseServiceRole) { + throw new Error("Missing Supabase configuration"); +} + +const supabase = createClient(supabaseUrl, supabaseServiceRole); + +export default async function handler(req: any, res: any) { + try { + // Enable CORS + res.setHeader("Access-Control-Allow-Origin", "*"); + res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS"); + res.setHeader("Access-Control-Allow-Headers", "Content-Type"); + + if (req.method === "OPTIONS") { + return res.status(200).end(); + } + + if (req.method !== "GET") { + return res.status(405).json({ error: "Method not allowed" }); + } + + const { userId } = req.query; + + if (!userId) { + return res.status(400).json({ error: "Missing userId parameter" }); + } + + // Query the user_follows table for users this person is following + const { data, error } = await supabase + .from("user_follows") + .select("following_id") + .eq("follower_id", userId); + + if (error) { + console.error("Error fetching following list:", error); + return res.status(500).json({ + error: "Failed to fetch following list", + details: (error as any).message, + }); + } + + // Extract the IDs from the result + const followingIds = (data || []).map((r: any) => r.following_id); + + return res.status(200).json({ data: followingIds }); + } catch (error) { + console.error("Unexpected error in following endpoint:", error); + return res.status(500).json({ + error: "Internal server error", + details: (error as any).message, + }); + } +}