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.
This commit is contained in:
Marco Gallegos
2026-01-18 22:51:45 -06:00
parent c220e7f30f
commit f6832c1e29
4 changed files with 30 additions and 22 deletions

View File

@@ -6,17 +6,13 @@ import { createClient } from '@supabase/supabase-js';
* @returns Statistics for dashboard display
*/
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'
if (!supabaseUrl || !supabaseServiceKey) {
throw new Error('Missing Supabase environment variables');
}
const supabase = createClient(supabaseUrl, supabaseServiceKey);
export async function GET() {
try {
const now = new Date();
const todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());
const todayEnd = new Date(todayStart);

View File

@@ -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,

View File

@@ -14,17 +14,20 @@ import { createClient } from '@supabase/supabase-js'
* @audit RELIABILITY: Cron job should run exactly at Monday 00:00 UTC weekly
*/
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) {
throw new Error('Missing Supabase environment variables')
return NextResponse.json(
{ success: false, error: 'Missing Supabase environment variables' },
{ status: 500 }
)
}
const supabase = createClient(supabaseUrl, supabaseServiceKey)
export async function GET(request: NextRequest) {
try {
const authHeader = request.headers.get('authorization')
if (!authHeader || !authHeader.startsWith('Bearer ')) {

View File

@@ -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