From f6832c1e291912f092053f462c0903750fdb4bb2 Mon Sep 17 00:00:00 2001 From: Marco Gallegos Date: Sun, 18 Jan 2026 22:51:45 -0600 Subject: [PATCH] fix: Improve API initialization with lazy Supabase client and validation - Move Supabase/Stripe initialization inside GET/POST handlers for lazy loading - Add validation for missing environment variables in runtime - Improve error handling in payment intent creation - Clean up next.config.js environment variable configuration This fixes potential build-time failures when environment variables are not available during static generation. --- app/api/aperture/stats/route.ts | 14 +++++--------- app/api/create-payment-intent/route.ts | 10 ++++++++-- app/api/cron/reset-invitations/route.ts | 21 ++++++++++++--------- next.config.js | 7 +++++-- 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/app/api/aperture/stats/route.ts b/app/api/aperture/stats/route.ts index 2a90cf5..605b700 100644 --- a/app/api/aperture/stats/route.ts +++ b/app/api/aperture/stats/route.ts @@ -6,17 +6,13 @@ import { createClient } from '@supabase/supabase-js'; * @returns Statistics for dashboard display */ -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || 'https://your-project.supabase.co' -const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || 'your-service-role-key-here' - -if (!supabaseUrl || !supabaseServiceKey) { - throw new Error('Missing Supabase environment variables'); -} - -const supabase = createClient(supabaseUrl, supabaseServiceKey); - export async function GET() { try { + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || 'https://your-project.supabase.co' + const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY || 'your-service-role-key-here' + + const supabase = createClient(supabaseUrl, supabaseServiceKey); + const now = new Date(); const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate()); const todayEnd = new Date(todayStart); diff --git a/app/api/create-payment-intent/route.ts b/app/api/create-payment-intent/route.ts index 7ca798e..dfa8903 100644 --- a/app/api/create-payment-intent/route.ts +++ b/app/api/create-payment-intent/route.ts @@ -2,8 +2,6 @@ import { NextRequest, NextResponse } from 'next/server' import Stripe from 'stripe' import { supabaseAdmin } from '@/lib/supabase/admin' -const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!) - /** * @description Creates a Stripe payment intent for booking deposit (50% of service price, max $200) * @param {NextRequest} request - Request containing booking details @@ -11,6 +9,14 @@ const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!) */ export async function POST(request: NextRequest) { try { + const stripeSecretKey = process.env.STRIPE_SECRET_KEY + + if (!stripeSecretKey) { + return NextResponse.json({ error: 'Stripe not configured' }, { status: 500 }) + } + + const stripe = new Stripe(stripeSecretKey) + const { customer_email, customer_phone, diff --git a/app/api/cron/reset-invitations/route.ts b/app/api/cron/reset-invitations/route.ts index 13973ed..168b6fb 100644 --- a/app/api/cron/reset-invitations/route.ts +++ b/app/api/cron/reset-invitations/route.ts @@ -14,17 +14,20 @@ import { createClient } from '@supabase/supabase-js' * @audit RELIABILITY: Cron job should run exactly at Monday 00:00 UTC weekly */ -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL -const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY - -if (!supabaseUrl || !supabaseServiceKey) { - throw new Error('Missing Supabase environment variables') -} - -const supabase = createClient(supabaseUrl, supabaseServiceKey) - export async function GET(request: NextRequest) { try { + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL + const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY + + if (!supabaseUrl || !supabaseServiceKey) { + return NextResponse.json( + { success: false, error: 'Missing Supabase environment variables' }, + { status: 500 } + ) + } + + const supabase = createClient(supabaseUrl, supabaseServiceKey) + const authHeader = request.headers.get('authorization') if (!authHeader || !authHeader.startsWith('Bearer ')) { diff --git a/next.config.js b/next.config.js index 89862bc..1235f9e 100644 --- a/next.config.js +++ b/next.config.js @@ -14,7 +14,7 @@ const nextConfig = { { protocol: 'https', hostname: '**.supabase.co', - }, + } ], }, env: { @@ -23,4 +23,7 @@ const nextConfig = { }, compiler: { removeConsole: false, // Temporarily enable logs for debugging 500 errors -module.exports = nextConfig + } +} + +module.exports = nextConfig \ No newline at end of file