|
| 1 | +import { AppEnvironment } from './app-environment'; |
| 2 | + |
| 3 | +interface RuntimeEnvironmentOverrides { |
| 4 | + production?: boolean; |
| 5 | + apiBaseUrl?: string; |
| 6 | + authApiBaseUrl?: string; |
| 7 | + activityHubPath?: string; |
| 8 | + debugAuth?: { |
| 9 | + enabled?: boolean; |
| 10 | + allowedHosts?: string[]; |
| 11 | + }; |
| 12 | + auth?: { |
| 13 | + authority?: string; |
| 14 | + clientId?: string; |
| 15 | + redirectUri?: string; |
| 16 | + postLogoutRedirectUri?: string; |
| 17 | + responseType?: 'code'; |
| 18 | + scopes?: string[]; |
| 19 | + }; |
| 20 | +} |
| 21 | + |
| 22 | +export async function loadRuntimeEnvironment(baseEnvironment: AppEnvironment): Promise<AppEnvironment> { |
| 23 | + try { |
| 24 | + const response = await fetch('/assets/runtime-config.json', { cache: 'no-store' }); |
| 25 | + if (!response.ok) { |
| 26 | + return baseEnvironment; |
| 27 | + } |
| 28 | + |
| 29 | + const overrides = (await response.json()) as RuntimeEnvironmentOverrides; |
| 30 | + return mergeEnvironment(baseEnvironment, overrides); |
| 31 | + } catch { |
| 32 | + return baseEnvironment; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function mergeEnvironment(base: AppEnvironment, overrides: RuntimeEnvironmentOverrides): AppEnvironment { |
| 37 | + return { |
| 38 | + production: typeof overrides.production === 'boolean' ? overrides.production : base.production, |
| 39 | + apiBaseUrl: normalizeString(overrides.apiBaseUrl, base.apiBaseUrl), |
| 40 | + authApiBaseUrl: normalizeString(overrides.authApiBaseUrl, base.authApiBaseUrl), |
| 41 | + activityHubPath: normalizeString(overrides.activityHubPath, base.activityHubPath), |
| 42 | + debugAuth: { |
| 43 | + enabled: typeof overrides.debugAuth?.enabled === 'boolean' ? overrides.debugAuth.enabled : base.debugAuth.enabled, |
| 44 | + allowedHosts: |
| 45 | + Array.isArray(overrides.debugAuth?.allowedHosts) && overrides.debugAuth.allowedHosts.length > 0 |
| 46 | + ? overrides.debugAuth.allowedHosts |
| 47 | + : base.debugAuth.allowedHosts |
| 48 | + }, |
| 49 | + auth: { |
| 50 | + authority: normalizeString(overrides.auth?.authority, base.auth.authority), |
| 51 | + clientId: normalizeString(overrides.auth?.clientId, base.auth.clientId), |
| 52 | + redirectUri: normalizeString(overrides.auth?.redirectUri, base.auth.redirectUri), |
| 53 | + postLogoutRedirectUri: normalizeString(overrides.auth?.postLogoutRedirectUri, base.auth.postLogoutRedirectUri), |
| 54 | + responseType: overrides.auth?.responseType ?? base.auth.responseType, |
| 55 | + scopes: Array.isArray(overrides.auth?.scopes) && overrides.auth.scopes.length > 0 ? overrides.auth.scopes : base.auth.scopes |
| 56 | + } |
| 57 | + }; |
| 58 | +} |
| 59 | + |
| 60 | +function normalizeString(value: string | undefined, fallback: string): string { |
| 61 | + return typeof value === 'string' && value.trim().length > 0 ? value : fallback; |
| 62 | +} |
0 commit comments