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:
bchanot committed 2026-10-08 22:43:18 +02:00
1 parent ce76c6597a
commit d239374d81
9 files changed
+153 -23

No files matched your search

+6
View File
@@ -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
+1
View File
@@ -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
+1
View File
@@ -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",
+47 -22
View File
@@ -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,
)
+25
View File
@@ -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
}
+34
View File
@@ -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()
})
})
+29
View File
@@ -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)
})
})
+7 -1
View File
@@ -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" <<EOF
window.__APP_CONFIG__ = {
siteName: "${SITE_NAME}",
brandName: "${BRAND_NAME}",
footerName: "${FOOTER_NAME}",
tagline: "${TAGLINE}"
tagline: "${TAGLINE}",
supabaseUrl: "${SUPABASE_URL}",
supabaseAnonKey: "${SUPABASE_ANON_KEY}"
};
EOF
+3
View File
@@ -20,6 +20,9 @@ importers:
apps/app:
dependencies:
'@faunosteo/supabase':
specifier: workspace:*
version: link:../../packages/supabase
motion:
specifier: ^11.11.17
version: 11.18.2(react-dom@19.2.6(react@19.2.6))(react@19.2.6)