feat(app): optional Supabase connection in runtime config + lazy typed client
- env.ts: supabase: SupabaseConnection | null resolved from env.js (supabaseUrl/supabaseAnonKey), VITE_SUPABASE_*, else null; half config refused - lib/supabase.ts: getSupabaseClient() null when unconfigured, one client per URL - entrypoint.sh writes the two public values; .env.example placeholders - Dockerfile copies packages/supabase manifest for the install layer - no UI change; 7 new tests (278 app)
This commit is contained in:
1 parent
ce76c6597a
commit
d239374d81
9 files changed
+153
-23
No files matched your search
+47
-22
@@ -1,10 +1,11 @@
|
||||
/// <reference types="vite/client" />
|
||||
import type { SupabaseConnection } from '@faunosteo/supabase'
|
||||
|
||||
/**
|
||||
* Runtime application config.
|
||||
* Resolution order:
|
||||
* 1. window.__APP_CONFIG__ (injected by docker/entrypoint.sh)
|
||||
* 2. import.meta.env.VITE_APP_* (build-time Vite defines)
|
||||
* 2. import.meta.env.VITE_* (build-time Vite defines)
|
||||
* 3. Hard-coded defaults
|
||||
*/
|
||||
export interface AppConfig {
|
||||
@@ -12,41 +13,65 @@ export interface AppConfig {
|
||||
brandName: string
|
||||
footerName: string
|
||||
tagline: string
|
||||
/** null = no backend configured; the app keeps its local-only behaviour. */
|
||||
supabase: SupabaseConnection | null
|
||||
}
|
||||
|
||||
/** Shape written by docker/entrypoint.sh (flat strings, empty = unset). */
|
||||
export interface RuntimeConfig {
|
||||
siteName?: string
|
||||
brandName?: string
|
||||
footerName?: string
|
||||
tagline?: string
|
||||
supabaseUrl?: string
|
||||
supabaseAnonKey?: string
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
__APP_CONFIG__?: Partial<AppConfig>
|
||||
__APP_CONFIG__?: RuntimeConfig
|
||||
}
|
||||
}
|
||||
|
||||
const DEFAULTS: AppConfig = {
|
||||
const DEFAULTS: Omit<AppConfig, 'supabase'> = {
|
||||
siteName: 'Faunosteo',
|
||||
brandName: 'Faunosteo',
|
||||
footerName: 'Faunosteo',
|
||||
tagline: 'Outils pour ostéopathes animaliers',
|
||||
}
|
||||
|
||||
function resolveConfig(): AppConfig {
|
||||
const runtime = typeof window !== 'undefined' ? window.__APP_CONFIG__ : {}
|
||||
/** Empty strings from env.js or Vite count as "not set". */
|
||||
function nonEmpty(value: string | undefined): string | undefined {
|
||||
return value && value.trim() !== '' ? value : undefined
|
||||
}
|
||||
|
||||
/** Both settings are required; one without the other is a misconfiguration. */
|
||||
export function resolveSupabaseConnection(
|
||||
url: string | undefined,
|
||||
anonKey: string | undefined,
|
||||
): SupabaseConnection | null {
|
||||
const cleanUrl = nonEmpty(url)
|
||||
const cleanKey = nonEmpty(anonKey)
|
||||
if (cleanUrl && cleanKey) return { url: cleanUrl, anonKey: cleanKey }
|
||||
if ((cleanUrl || cleanKey) && import.meta.env.DEV) {
|
||||
console.warn('[config] supabaseUrl and supabaseAnonKey must be set together; Supabase disabled')
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
export function resolveConfig(runtime: RuntimeConfig = {}): AppConfig {
|
||||
return {
|
||||
siteName:
|
||||
runtime?.siteName ??
|
||||
import.meta.env.VITE_APP_SITE_NAME ??
|
||||
DEFAULTS.siteName,
|
||||
brandName:
|
||||
runtime?.brandName ??
|
||||
import.meta.env.VITE_APP_BRAND_NAME ??
|
||||
DEFAULTS.brandName,
|
||||
footerName:
|
||||
runtime?.footerName ??
|
||||
import.meta.env.VITE_APP_FOOTER_NAME ??
|
||||
DEFAULTS.footerName,
|
||||
tagline:
|
||||
runtime?.tagline ??
|
||||
import.meta.env.VITE_APP_TAGLINE ??
|
||||
DEFAULTS.tagline,
|
||||
siteName: runtime.siteName ?? import.meta.env.VITE_APP_SITE_NAME ?? DEFAULTS.siteName,
|
||||
brandName: runtime.brandName ?? import.meta.env.VITE_APP_BRAND_NAME ?? DEFAULTS.brandName,
|
||||
footerName: runtime.footerName ?? import.meta.env.VITE_APP_FOOTER_NAME ?? DEFAULTS.footerName,
|
||||
tagline: runtime.tagline ?? import.meta.env.VITE_APP_TAGLINE ?? DEFAULTS.tagline,
|
||||
supabase: resolveSupabaseConnection(
|
||||
runtime.supabaseUrl ?? import.meta.env.VITE_SUPABASE_URL,
|
||||
runtime.supabaseAnonKey ?? import.meta.env.VITE_SUPABASE_ANON_KEY,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
export const appConfig: AppConfig = resolveConfig()
|
||||
export const appConfig: AppConfig = resolveConfig(
|
||||
typeof window !== 'undefined' ? window.__APP_CONFIG__ : undefined,
|
||||
)
|
||||
@@ -0,0 +1,25 @@
|
||||
import { createBrowserClient, type FaunosteoClient } from '@faunosteo/supabase'
|
||||
import { appConfig, type AppConfig } from '@/config/env'
|
||||
|
||||
let cached: { connectionUrl: string; client: FaunosteoClient } | null = null
|
||||
|
||||
/**
|
||||
* Lazily created Supabase client, or null when the deployment has no backend
|
||||
* configured. Callers must handle null: the current local-only flows do not
|
||||
* depend on Supabase. No UI uses this yet (foundation step).
|
||||
*/
|
||||
export function getSupabaseClient(config: AppConfig = appConfig): FaunosteoClient | null {
|
||||
if (!config.supabase) return null
|
||||
if (cached?.connectionUrl !== config.supabase.url) {
|
||||
cached = {
|
||||
connectionUrl: config.supabase.url,
|
||||
client: createBrowserClient(config.supabase),
|
||||
}
|
||||
}
|
||||
return cached.client
|
||||
}
|
||||
|
||||
/** True when the app can talk to Supabase. */
|
||||
export function isSupabaseConfigured(config: AppConfig = appConfig): boolean {
|
||||
return config.supabase !== null
|
||||
}
|
||||
Reference in new issue
Block a user