// packages/api-client/src/client.ts // Supabase 클라이언트 팩토리. 각 앱이 자신의 env에서 URL/KEY를 주입하여 생성. import { createClient, type SupabaseClient } from '@supabase/supabase-js' import type { Database } from './types' export type D3roSupabaseClient = SupabaseClient export interface CreateClientOptions { url: string | undefined anonKey: string | undefined /** * SSR 환경에서 세션 영속화를 쿠키 기반으로 하려면 별도 storage adapter를 주입. * 지정하지 않으면 기본(localStorage in browser, 메모리 in SSR). */ auth?: { persistSession?: boolean autoRefreshToken?: boolean storageKey?: string } } /** * Supabase 클라이언트 생성. * url 또는 anonKey가 비어있으면 "dummy" 클라이언트를 반환 — 모든 쿼리는 빈 결과. * 이렇게 해야 env가 세팅되지 않은 상태에서도 빌드/SSR이 실패하지 않는다. */ export function createD3roSupabaseClient(options: CreateClientOptions): D3roSupabaseClient { const { url, anonKey, auth } = options if (!url || !anonKey) { // Env가 없으면 fallback URL로 생성. 실제 호출은 런타임에 실패하지만 // 빌드/타입체크는 통과. 앱 상단에서 isClientConfigured()로 체크해야 함. return createClient('https://placeholder.supabase.co', 'placeholder-anon-key', { auth: { persistSession: false, autoRefreshToken: false } }) } return createClient(url, anonKey, { auth: { persistSession: auth?.persistSession ?? true, autoRefreshToken: auth?.autoRefreshToken ?? true, storageKey: auth?.storageKey } }) } /** * Env가 실제로 세팅되었는지 확인. UI에서 "Supabase 미설정" 경고 표시용. */ export function isClientConfigured(options: CreateClientOptions): boolean { return Boolean(options.url && options.anonKey) }