diff --git a/src/lib/resend.ts b/src/lib/resend.ts index 455052f..7ad9cbb 100644 --- a/src/lib/resend.ts +++ b/src/lib/resend.ts @@ -1,7 +1,12 @@ import { Resend } from 'resend' import { generateQRCodeBuffer } from './qr-code' -const resend = new Resend(process.env.RESEND_API_KEY) +function getResend() { + if (!process.env.RESEND_API_KEY) { + throw new Error('RESEND_API_KEY is not configured') + } + return new Resend(process.env.RESEND_API_KEY) +} export interface TicketEmailData { attendeeName: string @@ -15,6 +20,7 @@ export interface TicketEmailData { } export async function sendTicketEmail(data: TicketEmailData) { + const resend = getResend() try { // Generate QR code image const qrCodeBuffer = await generateQRCodeBuffer(data.qrCodeData) diff --git a/src/lib/supabase.ts b/src/lib/supabase.ts index 2e597a3..10a3e55 100644 --- a/src/lib/supabase.ts +++ b/src/lib/supabase.ts @@ -1,18 +1,54 @@ import { createClient } from '@supabase/supabase-js' import { Database } from './database.types' -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL! -const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! -const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY! - -// Client for client-side operations -export const supabase = createClient(supabaseUrl, supabaseAnonKey) - -// Admin client for server-side operations with service role key -export const supabaseAdmin = createClient(supabaseUrl, supabaseServiceKey, { - auth: { - autoRefreshToken: false, - persistSession: false +// Lazy-load client for client-side operations +let supabaseInstance: ReturnType> | null = null +export function getSupabase() { + if (!supabaseInstance) { + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL + const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY + + if (!supabaseUrl || !supabaseAnonKey) { + throw new Error('Supabase URL and Anon Key are required') + } + + supabaseInstance = createClient(supabaseUrl, supabaseAnonKey) + } + return supabaseInstance +} + +// For backwards compatibility +export const supabase = new Proxy({} as ReturnType>, { + get: (target, prop) => { + return getSupabase()[prop as keyof ReturnType>] + } +}) + +// Lazy-load admin client for server-side operations with service role key +let supabaseAdminInstance: ReturnType> | null = null +export function getSupabaseAdmin() { + if (!supabaseAdminInstance) { + const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL + const supabaseServiceKey = process.env.SUPABASE_SERVICE_ROLE_KEY + + if (!supabaseUrl || !supabaseServiceKey) { + throw new Error('Supabase URL and Service Role Key are required') + } + + supabaseAdminInstance = createClient(supabaseUrl, supabaseServiceKey, { + auth: { + autoRefreshToken: false, + persistSession: false + } + }) + } + return supabaseAdminInstance +} + +// For backwards compatibility +export const supabaseAdmin = new Proxy({} as ReturnType>, { + get: (target, prop) => { + return getSupabaseAdmin()[prop as keyof ReturnType>] } })