import { createClient as createSupabaseClient } from '@supabase/supabase-js' import type { Database } from 'types/database' import { isSupabaseConfigured as isSupabaseConfiguredEnv, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY, SUPABASE_URL } from './env' export const isSupabaseConfigured = isSupabaseConfiguredEnv import type { SupabaseClient } from '@supabase/supabase-js' export const createServerClient = async (opts?: { service?: boolean }): Promise> => { // If caller explicitly requests a service client and service role key exists, // return a service-role supabase client (for admin tasks). if (opts?.service && SUPABASE_URL && SUPABASE_SERVICE_ROLE_KEY) { return createSupabaseClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, { auth: { persistSession: false } }) } // For regular server route handlers, construct a Supabase client using // the anon/public key. We don't persist Supabase sessions; app-level // session is handled by our own cookie (`letschat_session`). if (!SUPABASE_URL || !SUPABASE_ANON_KEY) { // Return a proxy object that will throw only when its methods are invoked. // This prevents Next.js from crashing at startup while still surfacing // a clear error when code actually attempts to use Supabase. // @ts-ignore - create a lightweight proxy matching SupabaseClient shape at runtime const missingClient = new Proxy({}, { get() { return () => { throw new Error('Supabase not configured: missing NEXT_PUBLIC_SUPABASE_URL or NEXT_PUBLIC_SUPABASE_ANON_KEY') } } }) as SupabaseClient return missingClient } return createSupabaseClient(SUPABASE_URL, SUPABASE_ANON_KEY, { auth: { persistSession: false } }) } // Helper to explicitly create a service-role client when needed. export const createServiceClient = () => { if (!SUPABASE_URL || !SUPABASE_SERVICE_ROLE_KEY) { throw new Error('Service role key not configured') } // Provide a no-op async storage adapter for service clients on server. const noopAsyncStorage = { async getItem(_key: string) { return null }, async setItem(_key: string, _value: any) { return }, async removeItem(_key: string) { return } } return createSupabaseClient(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, { auth: { persistSession: false, // @ts-ignore - adapter shape is accepted at runtime storage: noopAsyncStorage } }) }