forked from Talenttrust/Talenttrust-Backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.ts
More file actions
107 lines (92 loc) · 3.05 KB
/
Copy pathenvironment.ts
File metadata and controls
107 lines (92 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* Environment Configuration Module
*
* Manages environment-specific configurations for deployment across
* development, staging, and production environments.
*
* @module config/environment
*/
import { validateEnv, EnvConfig } from './env.schema';
export type Environment = 'development' | 'staging' | 'production' | 'test';
export interface EnvironmentConfig extends EnvConfig {
/** Current environment name (mapped from NODE_ENV for compatibility) */
environment: Environment;
/** Server port */
port: number;
/** Node environment */
nodeEnv: string;
/** API base URL */
apiBaseUrl: string;
/** Enable debug logging */
debug: boolean;
/** Database connection string (if applicable) */
databaseUrl?: string;
/** Stellar/Soroban network configuration */
stellarNetwork: 'testnet' | 'mainnet';
/** Maximum request body size */
maxRequestSize: string;
/** CORS allowed origins */
corsOrigins: string[];
}
/**
* Validates required environment variables using Zod schema.
* This is now a wrapper around validateEnv.
* @throws {Error} If required environment variables are missing or invalid
*/
export function validateEnvironment(): void {
validateEnv(process.env);
}
/**
* Gets the current environment from NODE_ENV
* @returns {Environment} The current environment
*/
export function getCurrentEnvironment(): Environment {
const env = process.env.NODE_ENV || 'development';
if (env === 'production' || env === 'staging' || env === 'development' || env === 'test') {
return env as Environment;
}
return 'development';
}
/**
* Loads environment-specific configuration and validates it against the schema.
* @returns {EnvironmentConfig} Configuration object for current environment
*/
export function loadEnvironmentConfig(): EnvironmentConfig {
const validated = validateEnv(process.env);
const environment = validated.NODE_ENV as Environment;
const port = validated.PORT;
const baseConfig: EnvironmentConfig = {
...validated,
environment,
port,
nodeEnv: validated.NODE_ENV,
apiBaseUrl: validated.API_BASE_URL || `http://localhost:${port}`,
debug: validated.DEBUG ?? false,
databaseUrl: validated.DATABASE_URL,
stellarNetwork: environment === 'production' ? 'mainnet' : 'testnet',
maxRequestSize: validated.MAX_REQUEST_SIZE,
corsOrigins: validated.CORS_ALLOWED_ORIGINS ?? (validated.NODE_ENV === 'production' ? [] : ['http://localhost:3000']),
};
return baseConfig;
}
/**
* Checks if the current environment is production
* @returns {boolean} True if running in production
*/
export function isProduction(): boolean {
return getCurrentEnvironment() === 'production';
}
/**
* Checks if the current environment is staging
* @returns {boolean} True if running in staging
*/
export function isStaging(): boolean {
return getCurrentEnvironment() === 'staging';
}
/**
* Checks if the current environment is development
* @returns {boolean} True if running in development
*/
export function isDevelopment(): boolean {
return getCurrentEnvironment() === 'development';
}