Files
faunosteo/apps/app/src/config/env.ts
T
bchanot d239374d81 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)
2026-10-08 22:43:18 +02:00

78 lines
2.5 KiB
TypeScript

/// <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_* (build-time Vite defines)
* 3. Hard-coded defaults
*/
export interface AppConfig {
siteName: string
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__?: RuntimeConfig
}
}
const DEFAULTS: Omit<AppConfig, 'supabase'> = {
siteName: 'Faunosteo',
brandName: 'Faunosteo',
footerName: 'Faunosteo',
tagline: 'Outils pour ostéopathes animaliers',
}
/** 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,
supabase: resolveSupabaseConnection(
runtime.supabaseUrl ?? import.meta.env.VITE_SUPABASE_URL,
runtime.supabaseAnonKey ?? import.meta.env.VITE_SUPABASE_ANON_KEY,
),
}
}
export const appConfig: AppConfig = resolveConfig(
typeof window !== 'undefined' ? window.__APP_CONFIG__ : undefined,
)