@abdokouta/react-config 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +2 -1
- package/config/config.config.ts +9 -9
- package/dist/{index.d.mts → index.d.cts} +34 -2
- package/dist/index.d.ts +34 -2
- package/dist/index.js +28 -11
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +28 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +55 -25
- package/.examples/01-basic-usage.ts +0 -289
- package/.examples/02-env-helper.ts +0 -282
- package/.examples/README.md +0 -285
- package/.prettierrc.js +0 -1
- package/__tests__/config.module.test.ts +0 -244
- package/__tests__/drivers/env.driver.test.ts +0 -259
- package/__tests__/services/config.service.test.ts +0 -328
- package/__tests__/setup.d.ts +0 -11
- package/__tests__/vitest.setup.ts +0 -30
- package/eslint.config.js +0 -13
- package/src/config.module.ts +0 -154
- package/src/constants/index.ts +0 -5
- package/src/constants/tokens.constant.ts +0 -38
- package/src/drivers/env.driver.ts +0 -194
- package/src/drivers/file.driver.ts +0 -81
- package/src/drivers/index.ts +0 -6
- package/src/index.ts +0 -92
- package/src/interfaces/config-driver.interface.ts +0 -30
- package/src/interfaces/config-module-options.interface.ts +0 -84
- package/src/interfaces/config-service.interface.ts +0 -71
- package/src/interfaces/index.ts +0 -8
- package/src/interfaces/vite-config-plugin-options.interface.ts +0 -56
- package/src/plugins/index.ts +0 -5
- package/src/plugins/vite.plugin.ts +0 -115
- package/src/services/config.service.ts +0 -172
- package/src/services/index.ts +0 -5
- package/src/utils/get-nested-value.util.ts +0 -56
- package/src/utils/index.ts +0 -6
- package/src/utils/load-config-file.util.ts +0 -37
- package/src/utils/scan-config-files.util.ts +0 -40
- package/tsconfig.json +0 -28
- package/tsup.config.ts +0 -105
- package/vitest.config.ts +0 -66
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Env Helper Example
|
|
3
|
-
*
|
|
4
|
-
* This example demonstrates the standalone Env utility for quick
|
|
5
|
-
* environment variable access without dependency injection.
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* Run this example:
|
|
9
|
-
* ```bash
|
|
10
|
-
* ts-node examples/02-env-helper.ts
|
|
11
|
-
* ```
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import { Env } from '@abdokouta/config';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Setup example environment variables
|
|
18
|
-
*/
|
|
19
|
-
function setupEnv() {
|
|
20
|
-
process.env.APP_NAME = 'My Application';
|
|
21
|
-
process.env.APP_PORT = '3000';
|
|
22
|
-
process.env.APP_DEBUG = 'true';
|
|
23
|
-
process.env.NODE_ENV = 'development';
|
|
24
|
-
process.env.API_TIMEOUT = '5000';
|
|
25
|
-
process.env.ENABLE_CACHE = '1';
|
|
26
|
-
process.env.ALLOWED_ORIGINS = 'http://localhost:3000,http://localhost:4000';
|
|
27
|
-
process.env.FEATURE_FLAGS = JSON.stringify({
|
|
28
|
-
newUI: true,
|
|
29
|
-
betaFeatures: false,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Example 1: Basic Env usage
|
|
35
|
-
*/
|
|
36
|
-
function basicUsage() {
|
|
37
|
-
console.log('\n=== Example 1: Basic Env Usage ===\n');
|
|
38
|
-
|
|
39
|
-
// Get string values
|
|
40
|
-
const appName = Env.get('APP_NAME');
|
|
41
|
-
console.log(`✓ APP_NAME: "${appName}"`);
|
|
42
|
-
|
|
43
|
-
// Get with default
|
|
44
|
-
const appEnv = Env.get('APP_ENV', 'production');
|
|
45
|
-
console.log(`✓ APP_ENV: "${appEnv}" (using default)`);
|
|
46
|
-
|
|
47
|
-
// Get non-existent key
|
|
48
|
-
const apiKey = Env.get('API_KEY');
|
|
49
|
-
console.log(`✓ API_KEY: ${apiKey} (undefined)`);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Example 2: Type-safe getters
|
|
54
|
-
*/
|
|
55
|
-
function typeSafeGetters() {
|
|
56
|
-
console.log('\n=== Example 2: Type-Safe Getters ===\n');
|
|
57
|
-
|
|
58
|
-
// Get string
|
|
59
|
-
const appName = Env.getString('APP_NAME', 'Default App');
|
|
60
|
-
console.log(`✓ getString('APP_NAME'): "${appName}"`);
|
|
61
|
-
|
|
62
|
-
// Get number
|
|
63
|
-
const port = Env.getNumber('APP_PORT', 3000);
|
|
64
|
-
console.log(`✓ getNumber('APP_PORT'): ${port} (type: ${typeof port})`);
|
|
65
|
-
|
|
66
|
-
// Get boolean
|
|
67
|
-
const debug = Env.getBool('APP_DEBUG', false);
|
|
68
|
-
console.log(`✓ getBool('APP_DEBUG'): ${debug} (type: ${typeof debug})`);
|
|
69
|
-
|
|
70
|
-
// Get array
|
|
71
|
-
const origins = Env.getArray('ALLOWED_ORIGINS', []);
|
|
72
|
-
console.log(`✓ getArray('ALLOWED_ORIGINS'):`, origins);
|
|
73
|
-
|
|
74
|
-
// Get JSON
|
|
75
|
-
const features = Env.getJson('FEATURE_FLAGS', {});
|
|
76
|
-
console.log(`✓ getJson('FEATURE_FLAGS'):`, features);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Example 3: Required values
|
|
81
|
-
*/
|
|
82
|
-
function requiredValues() {
|
|
83
|
-
console.log('\n=== Example 3: Required Values ===\n');
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
// Get required value that exists
|
|
87
|
-
const appName = Env.getOrThrow('APP_NAME');
|
|
88
|
-
console.log(`✓ getOrThrow('APP_NAME'): "${appName}"`);
|
|
89
|
-
|
|
90
|
-
// Try to get required value that doesn't exist
|
|
91
|
-
const apiKey = Env.getOrThrow('API_KEY');
|
|
92
|
-
console.log(`✓ getOrThrow('API_KEY'): "${apiKey}"`);
|
|
93
|
-
} catch (error) {
|
|
94
|
-
console.log(`✗ Error: ${(error as Error).message}`);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Example 4: Check existence
|
|
100
|
-
*/
|
|
101
|
-
function checkExistence() {
|
|
102
|
-
console.log('\n=== Example 4: Check Existence ===\n');
|
|
103
|
-
|
|
104
|
-
const hasAppName = Env.has('APP_NAME');
|
|
105
|
-
console.log(`✓ has('APP_NAME'): ${hasAppName}`);
|
|
106
|
-
|
|
107
|
-
const hasApiKey = Env.has('API_KEY');
|
|
108
|
-
console.log(`✓ has('API_KEY'): ${hasApiKey}`);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Example 5: Get all environment variables
|
|
113
|
-
*/
|
|
114
|
-
function getAllEnv() {
|
|
115
|
-
console.log('\n=== Example 5: Get All Environment Variables ===\n');
|
|
116
|
-
|
|
117
|
-
const allEnv = Env.all();
|
|
118
|
-
const appVars = Object.keys(allEnv).filter(k => k.startsWith('APP_'));
|
|
119
|
-
|
|
120
|
-
console.log('✓ All APP_* environment variables:');
|
|
121
|
-
appVars.forEach(key => {
|
|
122
|
-
console.log(` - ${key}: ${allEnv[key]}`);
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Example 6: Practical use case - Configuration class
|
|
128
|
-
*/
|
|
129
|
-
class AppConfig {
|
|
130
|
-
static get name(): string {
|
|
131
|
-
return Env.getString('APP_NAME', 'My App');
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
static get port(): number {
|
|
135
|
-
return Env.getNumber('APP_PORT', 3000);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
static get debug(): boolean {
|
|
139
|
-
return Env.getBool('APP_DEBUG', false);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
static get environment(): string {
|
|
143
|
-
return Env.getString('NODE_ENV', 'development');
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
static get isProduction(): boolean {
|
|
147
|
-
return this.environment === 'production';
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
static get isDevelopment(): boolean {
|
|
151
|
-
return this.environment === 'development';
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
static get apiTimeout(): number {
|
|
155
|
-
return Env.getNumber('API_TIMEOUT', 5000);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
static get allowedOrigins(): string[] {
|
|
159
|
-
return Env.getArray('ALLOWED_ORIGINS', ['http://localhost:3000']);
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
static toJSON() {
|
|
163
|
-
return {
|
|
164
|
-
name: this.name,
|
|
165
|
-
port: this.port,
|
|
166
|
-
debug: this.debug,
|
|
167
|
-
environment: this.environment,
|
|
168
|
-
isProduction: this.isProduction,
|
|
169
|
-
isDevelopment: this.isDevelopment,
|
|
170
|
-
apiTimeout: this.apiTimeout,
|
|
171
|
-
allowedOrigins: this.allowedOrigins,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function configurationClass() {
|
|
177
|
-
console.log('\n=== Example 6: Configuration Class ===\n');
|
|
178
|
-
|
|
179
|
-
console.log('✓ Application configuration:');
|
|
180
|
-
console.log(JSON.stringify(AppConfig.toJSON(), null, 2));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Example 7: Practical use case - Database configuration
|
|
185
|
-
*/
|
|
186
|
-
function databaseConfig() {
|
|
187
|
-
console.log('\n=== Example 7: Database Configuration ===\n');
|
|
188
|
-
|
|
189
|
-
// Set database environment variables
|
|
190
|
-
process.env.DB_HOST = 'localhost';
|
|
191
|
-
process.env.DB_PORT = '5432';
|
|
192
|
-
process.env.DB_NAME = 'myapp';
|
|
193
|
-
process.env.DB_USER = 'admin';
|
|
194
|
-
process.env.DB_PASSWORD = 'secret123';
|
|
195
|
-
process.env.DB_SSL = 'true';
|
|
196
|
-
|
|
197
|
-
// Build database configuration using Env helper
|
|
198
|
-
const dbConfig = {
|
|
199
|
-
host: Env.getString('DB_HOST', 'localhost'),
|
|
200
|
-
port: Env.getNumber('DB_PORT', 5432),
|
|
201
|
-
database: Env.getString('DB_NAME', 'myapp'),
|
|
202
|
-
username: Env.getStringOrThrow('DB_USER'),
|
|
203
|
-
password: Env.getStringOrThrow('DB_PASSWORD'),
|
|
204
|
-
ssl: Env.getBool('DB_SSL', false),
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
console.log('✓ Database configuration:');
|
|
208
|
-
console.log(JSON.stringify(dbConfig, null, 2));
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
/**
|
|
212
|
-
* Example 8: Practical use case - Feature flags
|
|
213
|
-
*/
|
|
214
|
-
function featureFlags() {
|
|
215
|
-
console.log('\n=== Example 8: Feature Flags ===\n');
|
|
216
|
-
|
|
217
|
-
// Set feature flags
|
|
218
|
-
process.env.FEATURE_CACHE = 'true';
|
|
219
|
-
process.env.FEATURE_LOGGING = '1';
|
|
220
|
-
process.env.FEATURE_METRICS = 'false';
|
|
221
|
-
|
|
222
|
-
// Read feature flags using Env helper
|
|
223
|
-
const features = {
|
|
224
|
-
cache: Env.getBool('FEATURE_CACHE', false),
|
|
225
|
-
logging: Env.getBool('FEATURE_LOGGING', false),
|
|
226
|
-
metrics: Env.getBool('FEATURE_METRICS', false),
|
|
227
|
-
};
|
|
228
|
-
|
|
229
|
-
console.log('✓ Feature flags:');
|
|
230
|
-
Object.entries(features).forEach(([name, enabled]) => {
|
|
231
|
-
console.log(` - ${name}: ${enabled ? '✅ enabled' : '❌ disabled'}`);
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* Example 9: Comparison with ConfigService
|
|
237
|
-
*/
|
|
238
|
-
function comparisonWithConfigService() {
|
|
239
|
-
console.log('\n=== Example 9: Env vs ConfigService ===\n');
|
|
240
|
-
|
|
241
|
-
console.log('Env Helper:');
|
|
242
|
-
console.log(' ✅ No dependency injection needed');
|
|
243
|
-
console.log(' ✅ Static methods, easy to use anywhere');
|
|
244
|
-
console.log(' ✅ Perfect for simple scripts and utilities');
|
|
245
|
-
console.log(' ✅ Direct access to process.env');
|
|
246
|
-
console.log(' ❌ No driver support (env only)');
|
|
247
|
-
console.log(' ❌ No caching or advanced features');
|
|
248
|
-
|
|
249
|
-
console.log('\nConfigService:');
|
|
250
|
-
console.log(' ✅ Multiple driver support (env, file, etc.)');
|
|
251
|
-
console.log(' ✅ Dependency injection integration');
|
|
252
|
-
console.log(' ✅ Advanced features (caching, validation)');
|
|
253
|
-
console.log(' ✅ Better for large applications');
|
|
254
|
-
console.log(' ❌ Requires module setup');
|
|
255
|
-
console.log(' ❌ Needs dependency injection');
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Run all examples
|
|
260
|
-
*/
|
|
261
|
-
function main() {
|
|
262
|
-
console.log('╔════════════════════════════════════════╗');
|
|
263
|
-
console.log('║ Env Helper Examples ║');
|
|
264
|
-
console.log('╚════════════════════════════════════════╝');
|
|
265
|
-
|
|
266
|
-
setupEnv();
|
|
267
|
-
|
|
268
|
-
basicUsage();
|
|
269
|
-
typeSafeGetters();
|
|
270
|
-
requiredValues();
|
|
271
|
-
checkExistence();
|
|
272
|
-
getAllEnv();
|
|
273
|
-
configurationClass();
|
|
274
|
-
databaseConfig();
|
|
275
|
-
featureFlags();
|
|
276
|
-
comparisonWithConfigService();
|
|
277
|
-
|
|
278
|
-
console.log('\n✅ All examples completed successfully!\n');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
// Run the examples
|
|
282
|
-
main();
|
package/.examples/README.md
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
# Config Examples
|
|
2
|
-
|
|
3
|
-
This folder contains examples demonstrating how to use `@abdokouta/config` in various scenarios.
|
|
4
|
-
|
|
5
|
-
## Examples Overview
|
|
6
|
-
|
|
7
|
-
### 1. Basic Usage (`01-basic-usage.ts`)
|
|
8
|
-
|
|
9
|
-
Learn the fundamental configuration operations:
|
|
10
|
-
- ✅ Environment variable access
|
|
11
|
-
- ✅ Type-safe getters (getString, getNumber, getBool)
|
|
12
|
-
- ✅ Default values
|
|
13
|
-
- ✅ Nested configuration
|
|
14
|
-
- ✅ JSON configuration
|
|
15
|
-
- ✅ Array values
|
|
16
|
-
|
|
17
|
-
**Run:**
|
|
18
|
-
```bash
|
|
19
|
-
ts-node examples/01-basic-usage.ts
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
### 2. Multiple Drivers (`02-multiple-drivers.ts`)
|
|
23
|
-
|
|
24
|
-
Work with different configuration drivers:
|
|
25
|
-
- ✅ Environment driver (dotenv)
|
|
26
|
-
- ✅ File driver (TypeScript/JSON)
|
|
27
|
-
- ✅ Switching between drivers
|
|
28
|
-
- ✅ Driver-specific features
|
|
29
|
-
- ✅ Configuration merging
|
|
30
|
-
|
|
31
|
-
**Run:**
|
|
32
|
-
```bash
|
|
33
|
-
ts-node examples/02-multiple-drivers.ts
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
### 3. Env Helper (`03-env-helper.ts`)
|
|
37
|
-
|
|
38
|
-
Use the standalone Env utility:
|
|
39
|
-
- ✅ Direct environment access
|
|
40
|
-
- ✅ Type conversions
|
|
41
|
-
- ✅ No service injection needed
|
|
42
|
-
- ✅ Static helper methods
|
|
43
|
-
- ✅ Quick access patterns
|
|
44
|
-
|
|
45
|
-
**Run:**
|
|
46
|
-
```bash
|
|
47
|
-
ts-node examples/03-env-helper.ts
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Quick Start
|
|
51
|
-
|
|
52
|
-
### Installation
|
|
53
|
-
|
|
54
|
-
```bash
|
|
55
|
-
npm install @abdokouta/config @abdokouta/container
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
### Basic Setup
|
|
59
|
-
|
|
60
|
-
```typescript
|
|
61
|
-
import { ConfigModule, ConfigService } from '@abdokouta/config';
|
|
62
|
-
import { Inversiland } from '@abdokouta/container';
|
|
63
|
-
|
|
64
|
-
// Initialize config
|
|
65
|
-
const app = await Inversiland.run({
|
|
66
|
-
module: class AppModule {},
|
|
67
|
-
imports: [
|
|
68
|
-
ConfigModule.forRoot({
|
|
69
|
-
driver: 'env',
|
|
70
|
-
envFilePath: '.env',
|
|
71
|
-
isGlobal: true,
|
|
72
|
-
}),
|
|
73
|
-
],
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
// Get config service
|
|
77
|
-
const config = app.get(ConfigService);
|
|
78
|
-
|
|
79
|
-
// Use config
|
|
80
|
-
const dbHost = config.getString('DB_HOST', 'localhost');
|
|
81
|
-
const dbPort = config.getNumber('DB_PORT', 5432);
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
## Common Patterns
|
|
85
|
-
|
|
86
|
-
### 1. Database Configuration
|
|
87
|
-
|
|
88
|
-
```typescript
|
|
89
|
-
const dbConfig = {
|
|
90
|
-
host: config.getString('DB_HOST', 'localhost'),
|
|
91
|
-
port: config.getNumber('DB_PORT', 5432),
|
|
92
|
-
database: config.getString('DB_NAME', 'myapp'),
|
|
93
|
-
username: config.getStringOrThrow('DB_USER'),
|
|
94
|
-
password: config.getStringOrThrow('DB_PASSWORD'),
|
|
95
|
-
ssl: config.getBool('DB_SSL', false),
|
|
96
|
-
};
|
|
97
|
-
```
|
|
98
|
-
|
|
99
|
-
### 2. API Configuration
|
|
100
|
-
|
|
101
|
-
```typescript
|
|
102
|
-
const apiConfig = {
|
|
103
|
-
url: config.getString('API_URL', 'http://localhost:3000'),
|
|
104
|
-
timeout: config.getNumber('API_TIMEOUT', 5000),
|
|
105
|
-
retries: config.getNumber('API_RETRIES', 3),
|
|
106
|
-
apiKey: config.getStringOrThrow('API_KEY'),
|
|
107
|
-
};
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### 3. Feature Flags
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
const features = {
|
|
114
|
-
enableCache: config.getBool('FEATURE_CACHE', true),
|
|
115
|
-
enableLogging: config.getBool('FEATURE_LOGGING', true),
|
|
116
|
-
enableMetrics: config.getBool('FEATURE_METRICS', false),
|
|
117
|
-
};
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
### 4. Array Configuration
|
|
121
|
-
|
|
122
|
-
```typescript
|
|
123
|
-
const allowedOrigins = config.getArray('CORS_ORIGINS', ['http://localhost:3000']);
|
|
124
|
-
const trustedProxies = config.getArray('TRUSTED_PROXIES', []);
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### 5. JSON Configuration
|
|
128
|
-
|
|
129
|
-
```typescript
|
|
130
|
-
const complexConfig = config.getJson('APP_CONFIG', {
|
|
131
|
-
theme: 'light',
|
|
132
|
-
locale: 'en',
|
|
133
|
-
features: [],
|
|
134
|
-
});
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
## Best Practices
|
|
138
|
-
|
|
139
|
-
### 1. Use Type-Safe Getters
|
|
140
|
-
|
|
141
|
-
```typescript
|
|
142
|
-
// Good: Type-safe with defaults
|
|
143
|
-
const port = config.getNumber('PORT', 3000);
|
|
144
|
-
const debug = config.getBool('DEBUG', false);
|
|
145
|
-
|
|
146
|
-
// Bad: Generic get without type safety
|
|
147
|
-
const port = config.get('PORT') || 3000;
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
### 2. Require Critical Values
|
|
151
|
-
|
|
152
|
-
```typescript
|
|
153
|
-
// Use OrThrow for required configuration
|
|
154
|
-
const apiKey = config.getStringOrThrow('API_KEY');
|
|
155
|
-
const dbPassword = config.getStringOrThrow('DB_PASSWORD');
|
|
156
|
-
```
|
|
157
|
-
|
|
158
|
-
### 3. Group Related Configuration
|
|
159
|
-
|
|
160
|
-
```typescript
|
|
161
|
-
class DatabaseConfig {
|
|
162
|
-
constructor(private config: ConfigService) {}
|
|
163
|
-
|
|
164
|
-
get host() {
|
|
165
|
-
return this.config.getString('DB_HOST', 'localhost');
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
get port() {
|
|
169
|
-
return this.config.getNumber('DB_PORT', 5432);
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
get credentials() {
|
|
173
|
-
return {
|
|
174
|
-
username: this.config.getStringOrThrow('DB_USER'),
|
|
175
|
-
password: this.config.getStringOrThrow('DB_PASSWORD'),
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
### 4. Use Environment-Specific Defaults
|
|
182
|
-
|
|
183
|
-
```typescript
|
|
184
|
-
const env = config.getString('NODE_ENV', 'development');
|
|
185
|
-
|
|
186
|
-
const logLevel = config.getString(
|
|
187
|
-
'LOG_LEVEL',
|
|
188
|
-
env === 'production' ? 'error' : 'debug'
|
|
189
|
-
);
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
### 5. Validate Configuration on Startup
|
|
193
|
-
|
|
194
|
-
```typescript
|
|
195
|
-
function validateConfig(config: ConfigService) {
|
|
196
|
-
const required = ['DB_HOST', 'DB_USER', 'DB_PASSWORD', 'API_KEY'];
|
|
197
|
-
|
|
198
|
-
for (const key of required) {
|
|
199
|
-
if (!config.has(key)) {
|
|
200
|
-
throw new Error(`Missing required configuration: ${key}`);
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
```
|
|
205
|
-
|
|
206
|
-
## Configuration Examples
|
|
207
|
-
|
|
208
|
-
### Environment Driver
|
|
209
|
-
|
|
210
|
-
```typescript
|
|
211
|
-
ConfigModule.forRoot({
|
|
212
|
-
driver: 'env',
|
|
213
|
-
envFilePath: '.env',
|
|
214
|
-
ignoreEnvFile: false,
|
|
215
|
-
expandVariables: true,
|
|
216
|
-
isGlobal: true,
|
|
217
|
-
})
|
|
218
|
-
```
|
|
219
|
-
|
|
220
|
-
### File Driver
|
|
221
|
-
|
|
222
|
-
```typescript
|
|
223
|
-
ConfigModule.forRoot({
|
|
224
|
-
driver: 'file',
|
|
225
|
-
load: {
|
|
226
|
-
database: {
|
|
227
|
-
host: 'localhost',
|
|
228
|
-
port: 5432,
|
|
229
|
-
},
|
|
230
|
-
api: {
|
|
231
|
-
url: 'http://localhost:3000',
|
|
232
|
-
},
|
|
233
|
-
},
|
|
234
|
-
isGlobal: true,
|
|
235
|
-
})
|
|
236
|
-
```
|
|
237
|
-
|
|
238
|
-
### Custom Configuration
|
|
239
|
-
|
|
240
|
-
```typescript
|
|
241
|
-
ConfigModule.forRoot({
|
|
242
|
-
driver: 'env',
|
|
243
|
-
load: {
|
|
244
|
-
// Merge custom config with env vars
|
|
245
|
-
app: {
|
|
246
|
-
name: 'My App',
|
|
247
|
-
version: '1.0.0',
|
|
248
|
-
},
|
|
249
|
-
},
|
|
250
|
-
isGlobal: true,
|
|
251
|
-
})
|
|
252
|
-
```
|
|
253
|
-
|
|
254
|
-
## Troubleshooting
|
|
255
|
-
|
|
256
|
-
### Configuration Not Loading
|
|
257
|
-
|
|
258
|
-
1. Check if ConfigModule is imported
|
|
259
|
-
2. Verify .env file exists and is readable
|
|
260
|
-
3. Check environment variable names
|
|
261
|
-
4. Verify driver configuration
|
|
262
|
-
|
|
263
|
-
### Type Conversion Issues
|
|
264
|
-
|
|
265
|
-
1. Use appropriate getter methods
|
|
266
|
-
2. Check default values
|
|
267
|
-
3. Validate input format
|
|
268
|
-
4. Use getOrThrow for debugging
|
|
269
|
-
|
|
270
|
-
### Missing Values
|
|
271
|
-
|
|
272
|
-
1. Use has() to check existence
|
|
273
|
-
2. Provide sensible defaults
|
|
274
|
-
3. Use getOrThrow for required values
|
|
275
|
-
4. Check environment variable names
|
|
276
|
-
|
|
277
|
-
## Additional Resources
|
|
278
|
-
|
|
279
|
-
- [Main README](../README.md) - Package documentation
|
|
280
|
-
- [NestJS Config Documentation](https://docs.nestjs.com/techniques/configuration) - Inspiration
|
|
281
|
-
- [dotenv Documentation](https://github.com/motdotla/dotenv) - Environment variables
|
|
282
|
-
|
|
283
|
-
## Contributing
|
|
284
|
-
|
|
285
|
-
Found an issue or have a suggestion? Please open an issue or submit a pull request!
|
package/.prettierrc.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export default '@nesvel/prettier-config';
|