forked from StarkMindsHQ/StrellerMinds-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontribution.txt
More file actions
203 lines (160 loc) · 7.5 KB
/
contribution.txt
File metadata and controls
203 lines (160 loc) · 7.5 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# Environment Management and Validation Implementation
## Overview
This document explains the comprehensive environment management and validation system implemented for the StrellerMinds Frontend project. The implementation directly addresses the requirements specified in the issue raised on OnlyDust.
## Problem Statement
The project lacked:
- A `.env.example` file documenting required environment variables
- Runtime environment validation
- Graceful failure handling for missing/invalid environment variables
- Clear error messages for configuration issues
## Solution Implementation
### 1. Environment Variables Documentation (.env.example)
**What was implemented:**
- Created comprehensive `.env.example` file with 24+ environment variables
- Documented all required and optional variables with clear descriptions
- Included default values and usage examples
- Categorized variables by functionality (API, Auth, Database, etc.)
**Current vs. Future Variables:**
- **Currently Used**: Only `NODE_ENV` was actually used in the original codebase
- **Actually Needed**: 3-4 variables for current functionality (API URLs, app config)
- **Future-Ready**: 20+ variables prepared for features that will likely be needed as the app grows
**Why this approach:**
- Provides clear documentation for developers
- Ensures consistent environment setup across different deployments
- Reduces onboarding time for new developers
- Prevents configuration-related bugs in production
- **Future-proofs** the application for upcoming features (authentication, database, email, analytics)
**Files created:**
- `.env.example` - Complete environment variables template
- `.env` - To test
### 2. Runtime Environment Validation (Zod Schema)
**What was implemented:**
- Comprehensive Zod schema validation for all environment variables
- Type-safe environment variable access
- Runtime validation at both server and client startup
- Detailed error reporting with specific validation failures
**Why Zod was chosen:**
- Type-safe validation with TypeScript integration
- Excellent error messages for debugging
- Lightweight and performant
- Industry standard for environment validation
- Supports complex validation rules (URLs, enums, optional fields)
**Files created:**
- `src/lib/env.ts` - Main environment validation schema and utilities
- `src/lib/env-server.ts` - Server-side validation initialization
- `src/components/EnvironmentValidator.tsx` - Client-side validation component
### 3. Graceful Failure Implementation
**What was implemented:**
- Fail-fast validation at application startup
- Clear, actionable error messages
- Detailed validation error reporting
- Process exit with appropriate error codes
**Why this approach:**
- Prevents application from running with invalid configuration
- Provides immediate feedback to developers
- Reduces debugging time for configuration issues
- Ensures production deployments fail early if misconfigured
**Error handling features:**
- Specific error messages for each validation failure
- Clear indication of which variables are missing/invalid
- Helpful suggestions for fixing configuration issues
- Graceful degradation with informative console output
### 4. Integration Points
**What was implemented:**
- Integrated validation into app startup process
- Updated existing components to use environment configuration
- Enhanced logger with environment-based configuration
- Created utility functions for common environment checks
**Current State Analysis:**
- **Original Codebase**: Only used `NODE_ENV` in logger.ts
- **CourseService**: Used hardcoded `/api/courses` endpoint
- **CryptoTicker**: Used hardcoded CoinGecko API URL
- **No other environment variables** were actually used
**Why this integration approach:**
- Ensures validation runs automatically on every startup
- Provides type-safe access to environment variables throughout the app
- Maintains backward compatibility while adding new functionality
- Centralizes environment configuration management
- **Demonstrates best practices** for environment variable usage
**Files modified:**
- `src/app/layout.tsx` - Added environment validation to app startup
- `src/lib/logger.ts` - Enhanced with environment-based logging levels
- `src/components/CourseService.ts` - Updated to use environment URLs and timeouts (was hardcoded)
- `src/components/CryptoTicker.tsx` - Updated to use environment configuration (was hardcoded)
### 5. Developer Experience Enhancements
**What was implemented:**
- Standalone validation script (`npm run validate-env`)
- Comprehensive documentation in contribution.txt
- Type-safe environment utilities
- CI/CD ready validation
**Why these enhancements:**
- Allows developers to validate environment without starting the full app
- Provides clear documentation for all environment variables
- Enables automated validation in CI/CD pipelines
- Reduces support requests and configuration issues
**Files created:**
- `scripts/validate-env.js` - Standalone environment validation script
- `src/lib/__tests__/env.test.ts` - Test suite for environment validation
- Updated `package.json` with validation script
## Technical Implementation Details
### Environment Schema Structure
```typescript
const envSchema = z.object({
// Application Environment
NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
// Application Configuration
NEXT_PUBLIC_APP_NAME: z.string().min(1).default('StrellerMinds'),
NEXT_PUBLIC_APP_URL: z.string().url().default('http://localhost:3000'),
// API Configuration
NEXT_PUBLIC_API_BASE_URL: z.string().url().default('http://localhost:3000/api'),
NEXT_PUBLIC_API_TIMEOUT: z.coerce.number().positive().default(10000),
// Feature Flags
NEXT_PUBLIC_ENABLE_ANALYTICS: z.coerce.boolean().default(false),
NEXT_PUBLIC_ENABLE_DEBUG_MODE: z.coerce.boolean().default(true),
// ... additional variables
});
```
### Validation Flow
1. **Server Startup**: Environment validation runs immediately when the app starts
2. **Client Startup**: Client-side validation runs on component mount
3. **Error Handling**: Detailed error messages guide developers to fix issues
4. **Graceful Failure**: App exits with clear error codes if validation fails
### Utility Functions
```typescript
export const envUtils = {
isDevelopment: () => env.NODE_ENV === 'development',
isProduction: () => env.NODE_ENV === 'production',
isAnalyticsEnabled: () => env.NEXT_PUBLIC_ENABLE_ANALYTICS,
getStellarConfig: () => ({
network: env.NEXT_PUBLIC_STELLAR_NETWORK,
horizonUrl: env.NEXT_PUBLIC_STELLAR_HORIZON_URL,
}),
};
```
## Additional Benefits Delivered
Beyond the original requirements, this implementation provides:
1. **Type Safety**: All environment variables are typed and validated
2. **Developer Experience**: Standalone validation script and comprehensive documentation
3. **CI/CD Integration**: Validation script ready for automated testing
4. **Feature Flags**: Environment-controlled feature toggles
5. **Logging Integration**: Environment-based logging configuration
6. **Security**: Sensitive environment variables remain protected
7. **Maintainability**: Centralized environment configuration management
## Usage Examples
### Setting up environment:
```bash
# Copy template
cp .env.example .env
# Validate configuration
npm run validate-env
# Start development server
npm run dev
```
### Using environment variables in code:
```typescript
import { env, envUtils } from '@/lib/env';
// Type-safe access
const apiUrl = env.NEXT_PUBLIC_API_BASE_URL;
const isDebug = envUtils.isDebugMode();
const stellarConfig = envUtils.getStellarConfig();
```