From d239374d81783a10561fbb36f33c172af6008e97 Mon Sep 17 00:00:00 2001 From: bchanot Date: Thu, 8 Oct 2026 22:43:18 +0200 Subject: [PATCH] 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) --- .env.example | 6 +++ Dockerfile | 1 + apps/app/package.json | 1 + apps/app/src/config/env.ts | 69 ++++++++++++++++-------- apps/app/src/lib/supabase.ts | 25 +++++++++ apps/app/tests/unit/config.env.test.ts | 34 ++++++++++++ apps/app/tests/unit/lib.supabase.test.ts | 29 ++++++++++ docker/entrypoint.sh | 8 ++- pnpm-lock.yaml | 3 ++ 9 files changed, 153 insertions(+), 23 deletions(-) create mode 100644 apps/app/src/lib/supabase.ts create mode 100644 apps/app/tests/unit/config.env.test.ts create mode 100644 apps/app/tests/unit/lib.supabase.test.ts diff --git a/.env.example b/.env.example index 45113aec..9d5e9711 100644 --- a/.env.example +++ b/.env.example @@ -8,5 +8,11 @@ APP_BRAND_NAME=Osteo Report APP_FOOTER_NAME=Osteo Report APP_TAGLINE=Bilans d'ostéopathie animalière +# Supabase connection exposed to the browser (public values, RLS protects data). +# Empty = app runs without backend. Local stack: `pnpm db:start` prints both. +# NEVER put the service_role key here or in any browser-facing variable. +APP_SUPABASE_URL= +APP_SUPABASE_ANON_KEY= + # Docker Compose project namespace COMPOSE_PROJECT_NAME=osteo-report diff --git a/Dockerfile b/Dockerfile index 47a8c9dd..ec79d059 100644 --- a/Dockerfile +++ b/Dockerfile @@ -11,6 +11,7 @@ RUN corepack enable && corepack prepare pnpm@10.13.1 --activate # Workspace manifests first for layer caching (install depends only on these) COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./ COPY apps/app/package.json apps/app/ +COPY packages/supabase/package.json packages/supabase/ RUN pnpm install --frozen-lockfile diff --git a/apps/app/package.json b/apps/app/package.json index 3a6d373d..4e352035 100644 --- a/apps/app/package.json +++ b/apps/app/package.json @@ -15,6 +15,7 @@ "scaffold:species": "node scripts/scaffold-species.mjs" }, "dependencies": { + "@faunosteo/supabase": "workspace:*", "motion": "^11.11.17", "pdf-lib": "^1.17.1", "react": "^19.0.0", diff --git a/apps/app/src/config/env.ts b/apps/app/src/config/env.ts index 531c62dc..630032aa 100644 --- a/apps/app/src/config/env.ts +++ b/apps/app/src/config/env.ts @@ -1,10 +1,11 @@ /// +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 + __APP_CONFIG__?: RuntimeConfig } } -const DEFAULTS: AppConfig = { +const DEFAULTS: Omit = { 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, +) diff --git a/apps/app/src/lib/supabase.ts b/apps/app/src/lib/supabase.ts new file mode 100644 index 00000000..d278c5b4 --- /dev/null +++ b/apps/app/src/lib/supabase.ts @@ -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 +} diff --git a/apps/app/tests/unit/config.env.test.ts b/apps/app/tests/unit/config.env.test.ts new file mode 100644 index 00000000..7f41f567 --- /dev/null +++ b/apps/app/tests/unit/config.env.test.ts @@ -0,0 +1,34 @@ +import { describe, it, expect } from 'vitest' +import { resolveConfig, resolveSupabaseConnection } from '@/config/env' + +describe('resolveConfig', () => { + it('keeps the identity defaults and no Supabase when nothing is injected', () => { + const config = resolveConfig({}) + expect(config.brandName).toBe('Faunosteo') + expect(config.supabase).toBeNull() + }) + + it('reads the runtime Supabase connection from env.js values', () => { + const config = resolveConfig({ + supabaseUrl: 'http://127.0.0.1:54321', + supabaseAnonKey: 'anon', + }) + expect(config.supabase).toEqual({ url: 'http://127.0.0.1:54321', anonKey: 'anon' }) + }) + + it('still resolves identity strings injected at runtime', () => { + expect(resolveConfig({ brandName: 'Clinic' }).brandName).toBe('Clinic') + }) +}) + +describe('resolveSupabaseConnection', () => { + it('treats empty strings as unset', () => { + expect(resolveSupabaseConnection('', '')).toBeNull() + expect(resolveSupabaseConnection(' ', 'key')).toBeNull() + }) + + it('refuses a half configuration', () => { + expect(resolveSupabaseConnection('http://x', undefined)).toBeNull() + expect(resolveSupabaseConnection(undefined, 'key')).toBeNull() + }) +}) diff --git a/apps/app/tests/unit/lib.supabase.test.ts b/apps/app/tests/unit/lib.supabase.test.ts new file mode 100644 index 00000000..9d471ec1 --- /dev/null +++ b/apps/app/tests/unit/lib.supabase.test.ts @@ -0,0 +1,29 @@ +import { describe, it, expect } from 'vitest' +import { getSupabaseClient, isSupabaseConfigured } from '@/lib/supabase' +import type { AppConfig } from '@/config/env' + +const base: AppConfig = { + siteName: 'S', + brandName: 'B', + footerName: 'F', + tagline: 'T', + supabase: null, +} + +describe('getSupabaseClient', () => { + it('returns null when no backend is configured (current default)', () => { + expect(getSupabaseClient(base)).toBeNull() + expect(isSupabaseConfigured(base)).toBe(false) + }) + + it('creates one client per connection and reuses it', () => { + const configured: AppConfig = { + ...base, + supabase: { url: 'http://127.0.0.1:54321', anonKey: 'anon' }, + } + const first = getSupabaseClient(configured) + expect(first).not.toBeNull() + expect(getSupabaseClient(configured)).toBe(first) + expect(isSupabaseConfigured(configured)).toBe(true) + }) +}) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index e3cf2372..55231199 100644 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -17,13 +17,19 @@ SITE_NAME=$(js_escape "${APP_SITE_NAME:-Faunosteo}") BRAND_NAME=$(js_escape "${APP_BRAND_NAME:-Faunosteo}") FOOTER_NAME=$(js_escape "${APP_FOOTER_NAME:-Faunosteo}") TAGLINE=$(js_escape "${APP_TAGLINE:-Outils pour ostéopathes animaliers}") +# Supabase public connection (anon key only; empty = backend disabled). +# The service-role key must never be set here. +SUPABASE_URL=$(js_escape "${APP_SUPABASE_URL:-}") +SUPABASE_ANON_KEY=$(js_escape "${APP_SUPABASE_ANON_KEY:-}") cat > "$ENV_JS" <