Skip to content

Commit 843ef23

Browse files
committed
refactor(config): improve environment config
- Extracted helper function - Reduced code duplication - Improved error handling - Enhanced readability - Simplified accessors
1 parent 3f6c092 commit 843ef23

File tree

1 file changed

+17
-52
lines changed

1 file changed

+17
-52
lines changed

lib/src/config/environment_config.dart

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,32 @@ abstract final class EnvironmentConfig {
6060
return env; // Return even if fallback
6161
}
6262

63+
static String _getRequiredEnv(String key) {
64+
final value = _env[key];
65+
if (value == null || value.isEmpty) {
66+
_log.severe('$key not found in environment variables.');
67+
throw StateError(
68+
'FATAL: $key environment variable is not set.',
69+
);
70+
}
71+
return value;
72+
}
73+
6374
/// Retrieves the database connection URI from the environment.
6475
///
6576
/// The value is read from the `DATABASE_URL` environment variable.
6677
///
6778
/// Throws a [StateError] if the `DATABASE_URL` environment variable is not
6879
/// set, as the application cannot function without it.
69-
static String get databaseUrl {
70-
final dbUrl = _env['DATABASE_URL'];
71-
if (dbUrl == null || dbUrl.isEmpty) {
72-
_log.severe('DATABASE_URL not found in environment variables.');
73-
throw StateError(
74-
'FATAL: DATABASE_URL environment variable is not set. '
75-
'The application cannot start without a database connection.',
76-
);
77-
}
78-
return dbUrl;
79-
}
80+
static String get databaseUrl => _getRequiredEnv('DATABASE_URL');
8081

8182
/// Retrieves the JWT secret key from the environment.
8283
///
8384
/// The value is read from the `JWT_SECRET_KEY` environment variable.
8485
///
8586
/// Throws a [StateError] if the `JWT_SECRET_KEY` environment variable is not
8687
/// set, as the application cannot function without it.
87-
static String get jwtSecretKey {
88-
final jwtKey = _env['JWT_SECRET_KEY'];
89-
if (jwtKey == null || jwtKey.isEmpty) {
90-
_log.severe('JWT_SECRET_KEY not found in environment variables.');
91-
throw StateError(
92-
'FATAL: JWT_SECRET_KEY environment variable is not set. '
93-
'The application cannot start without a JWT secret.',
94-
);
95-
}
96-
return jwtKey;
97-
}
88+
static String get jwtSecretKey => _getRequiredEnv('JWT_SECRET_KEY');
9889

9990
/// Retrieves the current environment mode (e.g., 'development').
10091
///
@@ -127,44 +118,18 @@ abstract final class EnvironmentConfig {
127118
/// Retrieves the SendGrid API key from the environment.
128119
///
129120
/// Throws a [StateError] if the `SENDGRID_API_KEY` is not set.
130-
static String get sendGridApiKey {
131-
final apiKey = _env['SENDGRID_API_KEY'];
132-
if (apiKey == null || apiKey.isEmpty) {
133-
_log.severe('SENDGRID_API_KEY not found in environment variables.');
134-
throw StateError(
135-
'FATAL: SENDGRID_API_KEY environment variable is not set.',
136-
);
137-
}
138-
return apiKey;
139-
}
121+
static String get sendGridApiKey => _getRequiredEnv('SENDGRID_API_KEY');
140122

141123
/// Retrieves the default sender email from the environment.
142124
///
143125
/// Throws a [StateError] if the `DEFAULT_SENDER_EMAIL` is not set.
144-
static String get defaultSenderEmail {
145-
final email = _env['DEFAULT_SENDER_EMAIL'];
146-
if (email == null || email.isEmpty) {
147-
_log.severe('DEFAULT_SENDER_EMAIL not found in environment variables.');
148-
throw StateError(
149-
'FATAL: DEFAULT_SENDER_EMAIL environment variable is not set.',
150-
);
151-
}
152-
return email;
153-
}
126+
static String get defaultSenderEmail =>
127+
_getRequiredEnv('DEFAULT_SENDER_EMAIL');
154128

155129
/// Retrieves the SendGrid OTP template ID from the environment.
156130
///
157131
/// Throws a [StateError] if the `OTP_TEMPLATE_ID` is not set.
158-
static String get otpTemplateId {
159-
final templateId = _env['OTP_TEMPLATE_ID'];
160-
if (templateId == null || templateId.isEmpty) {
161-
_log.severe('OTP_TEMPLATE_ID not found in environment variables.');
162-
throw StateError(
163-
'FATAL: OTP_TEMPLATE_ID environment variable is not set.',
164-
);
165-
}
166-
return templateId;
167-
}
132+
static String get otpTemplateId => _getRequiredEnv('OTP_TEMPLATE_ID');
168133

169134
/// Retrieves the SendGrid API URL from the environment, if provided.
170135
///

0 commit comments

Comments
 (0)