mirror of
https://github.com/marcogll/AnchorOS.git
synced 2026-03-15 08:24:24 +00:00
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:
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 ')) {
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user