|
| 1 | +export type PlatformEnvironment = 'dev' | 'stage' | 'prod'; |
| 2 | + |
| 3 | +export interface PlatformConfig { |
| 4 | + readonly environment: PlatformEnvironment; |
| 5 | + readonly owner: string; |
| 6 | + readonly costCenter: string; |
| 7 | + readonly dataClassification: 'public' | 'internal' | 'confidential' | 'restricted'; |
| 8 | + readonly project: string; |
| 9 | +} |
| 10 | + |
| 11 | +const CONFIG_BY_ENV: Record<PlatformEnvironment, Omit<PlatformConfig, 'environment'>> = { |
| 12 | + dev: { |
| 13 | + owner: 'platform-engineering', |
| 14 | + costCenter: 'ENG-PLATFORM', |
| 15 | + dataClassification: 'internal', |
| 16 | + project: 'DemoAPI', |
| 17 | + }, |
| 18 | + stage: { |
| 19 | + owner: 'platform-engineering', |
| 20 | + costCenter: 'ENG-PLATFORM', |
| 21 | + dataClassification: 'confidential', |
| 22 | + project: 'DemoAPI', |
| 23 | + }, |
| 24 | + prod: { |
| 25 | + owner: 'platform-engineering', |
| 26 | + costCenter: 'ENG-PLATFORM', |
| 27 | + dataClassification: 'confidential', |
| 28 | + project: 'DemoAPI', |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +export const resolvePlatformEnvironment = (value?: string): PlatformEnvironment => { |
| 33 | + const normalized = value?.trim().toLowerCase(); |
| 34 | + |
| 35 | + if (!normalized) { |
| 36 | + return 'dev'; |
| 37 | + } |
| 38 | + |
| 39 | + if (normalized === 'dev' || normalized === 'stage' || normalized === 'prod') { |
| 40 | + return normalized; |
| 41 | + } |
| 42 | + |
| 43 | + throw new Error( |
| 44 | + `Invalid platform environment \"${value}\". Allowed values: dev, stage, prod.`, |
| 45 | + ); |
| 46 | +}; |
| 47 | + |
| 48 | +export const loadPlatformConfig = (environmentValue?: string): PlatformConfig => { |
| 49 | + const environment = resolvePlatformEnvironment(environmentValue); |
| 50 | + |
| 51 | + return { |
| 52 | + environment, |
| 53 | + ...CONFIG_BY_ENV[environment], |
| 54 | + }; |
| 55 | +}; |
0 commit comments