import { Injectable } from "@nestjs/common"; import { createClient, SupabaseClient } from "@supabase/supabase-js"; @Injectable() export class SupabaseService { private readonly client: SupabaseClient; constructor() { const url = this.requireEnv("SUPABASE_URL"); const serviceKey = process.env.SUPABASE_SERVICE_KEY ?? process.env.SUPABASE_ANON_KEY; if (!serviceKey) { throw new Error( "Missing SUPABASE_SERVICE_KEY (preferred) or SUPABASE_ANON_KEY environment variable." ); } this.client = createClient(url, serviceKey, { auth: { persistSession: false } }); } getClient() { return this.client; } private requireEnv(name: string) { const value = process.env[name]; if (!value) { throw new Error(`Missing ${name} environment variable.`); } return value; } }