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

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