@abdokouta/react-config 1.0.1 → 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.
Files changed (39) hide show
  1. package/dist/{index.d.mts → index.d.cts} +1 -1
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs.map +1 -1
  5. package/package.json +46 -17
  6. package/.examples/01-basic-usage.ts +0 -291
  7. package/.examples/02-env-helper.ts +0 -282
  8. package/.examples/README.md +0 -297
  9. package/.prettierrc.js +0 -1
  10. package/__tests__/config.module.test.ts +0 -244
  11. package/__tests__/drivers/env.driver.test.ts +0 -259
  12. package/__tests__/services/config.service.test.ts +0 -335
  13. package/__tests__/setup.d.ts +0 -11
  14. package/__tests__/vitest.setup.ts +0 -30
  15. package/eslint.config.js +0 -13
  16. package/src/config.module.ts +0 -152
  17. package/src/constants/index.ts +0 -5
  18. package/src/constants/tokens.constant.ts +0 -38
  19. package/src/drivers/env.driver.ts +0 -208
  20. package/src/drivers/file.driver.ts +0 -82
  21. package/src/drivers/index.ts +0 -6
  22. package/src/index.ts +0 -93
  23. package/src/interfaces/config-driver.interface.ts +0 -30
  24. package/src/interfaces/config-module-options.interface.ts +0 -84
  25. package/src/interfaces/config-service.interface.ts +0 -71
  26. package/src/interfaces/index.ts +0 -8
  27. package/src/interfaces/vite-config-plugin-options.interface.ts +0 -56
  28. package/src/plugins/index.ts +0 -5
  29. package/src/plugins/vite.plugin.ts +0 -118
  30. package/src/services/config.service.ts +0 -172
  31. package/src/services/index.ts +0 -5
  32. package/src/utils/define-config.util.ts +0 -35
  33. package/src/utils/get-nested-value.util.ts +0 -53
  34. package/src/utils/index.ts +0 -9
  35. package/src/utils/load-config-file.util.ts +0 -32
  36. package/src/utils/scan-config-files.util.ts +0 -36
  37. package/tsconfig.json +0 -28
  38. package/tsup.config.ts +0 -105
  39. package/vitest.config.ts +0 -66
@@ -1,297 +0,0 @@
1
- # Config Examples
2
-
3
- This folder contains examples demonstrating how to use `@abdokouta/config` in
4
- various scenarios.
5
-
6
- ## Examples Overview
7
-
8
- ### 1. Basic Usage (`01-basic-usage.ts`)
9
-
10
- Learn the fundamental configuration operations:
11
-
12
- - ✅ Environment variable access
13
- - ✅ Type-safe getters (getString, getNumber, getBool)
14
- - ✅ Default values
15
- - ✅ Nested configuration
16
- - ✅ JSON configuration
17
- - ✅ Array values
18
-
19
- **Run:**
20
-
21
- ```bash
22
- ts-node examples/01-basic-usage.ts
23
- ```
24
-
25
- ### 2. Multiple Drivers (`02-multiple-drivers.ts`)
26
-
27
- Work with different configuration drivers:
28
-
29
- - ✅ Environment driver (dotenv)
30
- - ✅ File driver (TypeScript/JSON)
31
- - ✅ Switching between drivers
32
- - ✅ Driver-specific features
33
- - ✅ Configuration merging
34
-
35
- **Run:**
36
-
37
- ```bash
38
- ts-node examples/02-multiple-drivers.ts
39
- ```
40
-
41
- ### 3. Env Helper (`03-env-helper.ts`)
42
-
43
- Use the standalone Env utility:
44
-
45
- - ✅ Direct environment access
46
- - ✅ Type conversions
47
- - ✅ No service injection needed
48
- - ✅ Static helper methods
49
- - ✅ Quick access patterns
50
-
51
- **Run:**
52
-
53
- ```bash
54
- ts-node examples/03-env-helper.ts
55
- ```
56
-
57
- ## Quick Start
58
-
59
- ### Installation
60
-
61
- ```bash
62
- npm install @abdokouta/config @abdokouta/container
63
- ```
64
-
65
- ### Basic Setup
66
-
67
- ```typescript
68
- import { ConfigModule, ConfigService } from '@abdokouta/config';
69
- import { Inversiland } from '@abdokouta/container';
70
-
71
- // Initialize config
72
- const app = await Inversiland.run({
73
- module: class AppModule {},
74
- imports: [
75
- ConfigModule.forRoot({
76
- driver: 'env',
77
- envFilePath: '.env',
78
- isGlobal: true,
79
- }),
80
- ],
81
- });
82
-
83
- // Get config service
84
- const config = app.get(ConfigService);
85
-
86
- // Use config
87
- const dbHost = config.getString('DB_HOST', 'localhost');
88
- const dbPort = config.getNumber('DB_PORT', 5432);
89
- ```
90
-
91
- ## Common Patterns
92
-
93
- ### 1. Database Configuration
94
-
95
- ```typescript
96
- const dbConfig = {
97
- host: config.getString('DB_HOST', 'localhost'),
98
- port: config.getNumber('DB_PORT', 5432),
99
- database: config.getString('DB_NAME', 'myapp'),
100
- username: config.getStringOrThrow('DB_USER'),
101
- password: config.getStringOrThrow('DB_PASSWORD'),
102
- ssl: config.getBool('DB_SSL', false),
103
- };
104
- ```
105
-
106
- ### 2. API Configuration
107
-
108
- ```typescript
109
- const apiConfig = {
110
- url: config.getString('API_URL', 'http://localhost:3000'),
111
- timeout: config.getNumber('API_TIMEOUT', 5000),
112
- retries: config.getNumber('API_RETRIES', 3),
113
- apiKey: config.getStringOrThrow('API_KEY'),
114
- };
115
- ```
116
-
117
- ### 3. Feature Flags
118
-
119
- ```typescript
120
- const features = {
121
- enableCache: config.getBool('FEATURE_CACHE', true),
122
- enableLogging: config.getBool('FEATURE_LOGGING', true),
123
- enableMetrics: config.getBool('FEATURE_METRICS', false),
124
- };
125
- ```
126
-
127
- ### 4. Array Configuration
128
-
129
- ```typescript
130
- const allowedOrigins = config.getArray('CORS_ORIGINS', [
131
- 'http://localhost:3000',
132
- ]);
133
- const trustedProxies = config.getArray('TRUSTED_PROXIES', []);
134
- ```
135
-
136
- ### 5. JSON Configuration
137
-
138
- ```typescript
139
- const complexConfig = config.getJson('APP_CONFIG', {
140
- theme: 'light',
141
- locale: 'en',
142
- features: [],
143
- });
144
- ```
145
-
146
- ## Best Practices
147
-
148
- ### 1. Use Type-Safe Getters
149
-
150
- ```typescript
151
- // Good: Type-safe with defaults
152
- const port = config.getNumber('PORT', 3000);
153
- const debug = config.getBool('DEBUG', false);
154
-
155
- // Bad: Generic get without type safety
156
- const port = config.get('PORT') || 3000;
157
- ```
158
-
159
- ### 2. Require Critical Values
160
-
161
- ```typescript
162
- // Use OrThrow for required configuration
163
- const apiKey = config.getStringOrThrow('API_KEY');
164
- const dbPassword = config.getStringOrThrow('DB_PASSWORD');
165
- ```
166
-
167
- ### 3. Group Related Configuration
168
-
169
- ```typescript
170
- class DatabaseConfig {
171
- constructor(private config: ConfigService) {}
172
-
173
- get host() {
174
- return this.config.getString('DB_HOST', 'localhost');
175
- }
176
-
177
- get port() {
178
- return this.config.getNumber('DB_PORT', 5432);
179
- }
180
-
181
- get credentials() {
182
- return {
183
- username: this.config.getStringOrThrow('DB_USER'),
184
- password: this.config.getStringOrThrow('DB_PASSWORD'),
185
- };
186
- }
187
- }
188
- ```
189
-
190
- ### 4. Use Environment-Specific Defaults
191
-
192
- ```typescript
193
- const env = config.getString('NODE_ENV', 'development');
194
-
195
- const logLevel = config.getString(
196
- 'LOG_LEVEL',
197
- env === 'production' ? 'error' : 'debug'
198
- );
199
- ```
200
-
201
- ### 5. Validate Configuration on Startup
202
-
203
- ```typescript
204
- function validateConfig(config: ConfigService) {
205
- const required = ['DB_HOST', 'DB_USER', 'DB_PASSWORD', 'API_KEY'];
206
-
207
- for (const key of required) {
208
- if (!config.has(key)) {
209
- throw new Error(`Missing required configuration: ${key}`);
210
- }
211
- }
212
- }
213
- ```
214
-
215
- ## Configuration Examples
216
-
217
- ### Environment Driver
218
-
219
- ```typescript
220
- ConfigModule.forRoot({
221
- driver: 'env',
222
- envFilePath: '.env',
223
- ignoreEnvFile: false,
224
- expandVariables: true,
225
- isGlobal: true,
226
- });
227
- ```
228
-
229
- ### File Driver
230
-
231
- ```typescript
232
- ConfigModule.forRoot({
233
- driver: 'file',
234
- load: {
235
- database: {
236
- host: 'localhost',
237
- port: 5432,
238
- },
239
- api: {
240
- url: 'http://localhost:3000',
241
- },
242
- },
243
- isGlobal: true,
244
- });
245
- ```
246
-
247
- ### Custom Configuration
248
-
249
- ```typescript
250
- ConfigModule.forRoot({
251
- driver: 'env',
252
- load: {
253
- // Merge custom config with env vars
254
- app: {
255
- name: 'My App',
256
- version: '1.0.0',
257
- },
258
- },
259
- isGlobal: true,
260
- });
261
- ```
262
-
263
- ## Troubleshooting
264
-
265
- ### Configuration Not Loading
266
-
267
- 1. Check if ConfigModule is imported
268
- 2. Verify .env file exists and is readable
269
- 3. Check environment variable names
270
- 4. Verify driver configuration
271
-
272
- ### Type Conversion Issues
273
-
274
- 1. Use appropriate getter methods
275
- 2. Check default values
276
- 3. Validate input format
277
- 4. Use getOrThrow for debugging
278
-
279
- ### Missing Values
280
-
281
- 1. Use has() to check existence
282
- 2. Provide sensible defaults
283
- 3. Use getOrThrow for required values
284
- 4. Check environment variable names
285
-
286
- ## Additional Resources
287
-
288
- - [Main README](../README.md) - Package documentation
289
- - [NestJS Config Documentation](https://docs.nestjs.com/techniques/configuration) -
290
- Inspiration
291
- - [dotenv Documentation](https://github.com/motdotla/dotenv) - Environment
292
- variables
293
-
294
- ## Contributing
295
-
296
- Found an issue or have a suggestion? Please open an issue or submit a pull
297
- request!
package/.prettierrc.js DELETED
@@ -1 +0,0 @@
1
- export default '@nesvel/prettier-config';
@@ -1,244 +0,0 @@
1
- /**
2
- * @fileoverview Tests for ConfigModule
3
- *
4
- * This test suite verifies the ConfigModule functionality including:
5
- * - Module registration (forRoot, forRootAsync)
6
- * - Driver creation and configuration
7
- * - Custom config merging
8
- * - Provider setup
9
- *
10
- * @module @abdokouta/config
11
- * @category Tests
12
- */
13
-
14
- import { describe, it, expect, beforeEach } from 'vitest';
15
- import { ConfigModule } from '@/config.module';
16
- import { EnvDriver } from '@/drivers/env.driver';
17
- import { FileDriver } from '@/drivers/file.driver';
18
-
19
- describe('ConfigModule', () => {
20
- describe('forRoot', () => {
21
- it('should create a dynamic module with default configuration', () => {
22
- // Act: Create module with default config
23
- const module = ConfigModule.forRoot();
24
-
25
- // Assert: Module structure is correct
26
- expect(module).toBeDefined();
27
- expect(module.module).toBe(ConfigModule);
28
- expect(module.providers).toBeDefined();
29
- expect(module.exports).toBeDefined();
30
- });
31
-
32
- it('should create module with env driver', () => {
33
- // Arrange: Configure env driver
34
- const options = {
35
- driver: 'env' as const,
36
- envFilePath: '.env.test',
37
- };
38
-
39
- // Act: Create module
40
- const module = ConfigModule.forRoot(options);
41
-
42
- // Assert: Module is configured correctly
43
- expect(module).toBeDefined();
44
- expect(module.providers).toBeDefined();
45
- });
46
-
47
- it('should create module with file driver', () => {
48
- // Arrange: Configure file driver
49
- const options = {
50
- driver: 'file' as const,
51
- configPath: './config',
52
- };
53
-
54
- // Act: Create module
55
- const module = ConfigModule.forRoot(options);
56
-
57
- // Assert: Module is configured correctly
58
- expect(module).toBeDefined();
59
- expect(module.providers).toBeDefined();
60
- });
61
-
62
- it('should merge custom configuration', () => {
63
- // Arrange: Custom config
64
- const customConfig = {
65
- app: {
66
- name: 'Test App',
67
- version: '1.0.0',
68
- },
69
- };
70
-
71
- const options = {
72
- driver: 'env' as const,
73
- config: customConfig,
74
- };
75
-
76
- // Act: Create module
77
- const module = ConfigModule.forRoot(options);
78
-
79
- // Assert: Module includes custom config
80
- expect(module).toBeDefined();
81
- });
82
-
83
- it('should set global flag when specified', () => {
84
- // Arrange: Global module config
85
- const options = {
86
- driver: 'env' as const,
87
- isGlobal: true,
88
- };
89
-
90
- // Act: Create module
91
- const module = ConfigModule.forRoot(options);
92
-
93
- // Assert: Module is global
94
- expect(module.global).toBe(true);
95
- });
96
- });
97
-
98
- describe('forRootAsync', () => {
99
- it('should create async dynamic module', async () => {
100
- // Arrange: Async factory
101
- const useFactory = async () => ({
102
- driver: 'env' as const,
103
- envFilePath: '.env',
104
- });
105
-
106
- // Act: Create async module
107
- const module = await ConfigModule.forRootAsync({
108
- useFactory,
109
- });
110
-
111
- // Assert: Module is created
112
- expect(module).toBeDefined();
113
- expect(module.module).toBe(ConfigModule);
114
- });
115
-
116
- it('should handle async factory with dependencies', async () => {
117
- // Arrange: Factory with inject
118
- const useFactory = async (dep: any) => ({
119
- driver: 'env' as const,
120
- });
121
-
122
- // Act: Create async module
123
- const module = await ConfigModule.forRootAsync({
124
- useFactory,
125
- inject: ['SOME_DEPENDENCY'],
126
- });
127
-
128
- // Assert: Module is created
129
- expect(module).toBeDefined();
130
- });
131
- });
132
-
133
- describe('Driver Creation', () => {
134
- it('should create EnvDriver by default', () => {
135
- // Act: Create module with no driver specified
136
- const module = ConfigModule.forRoot({});
137
-
138
- // Assert: Module uses env driver
139
- expect(module).toBeDefined();
140
- });
141
-
142
- it('should create FileDriver when specified', () => {
143
- // Arrange: File driver config
144
- const options = {
145
- driver: 'file' as const,
146
- configPath: './config',
147
- };
148
-
149
- // Act: Create module
150
- const module = ConfigModule.forRoot(options);
151
-
152
- // Assert: Module uses file driver
153
- expect(module).toBeDefined();
154
- });
155
-
156
- it('should pass driver options correctly', () => {
157
- // Arrange: Driver with options
158
- const options = {
159
- driver: 'env' as const,
160
- envFilePath: '.env.custom',
161
- expandVariables: true,
162
- };
163
-
164
- // Act: Create module
165
- const module = ConfigModule.forRoot(options);
166
-
167
- // Assert: Module is configured
168
- expect(module).toBeDefined();
169
- });
170
- });
171
-
172
- describe('Configuration Merging', () => {
173
- it('should merge multiple config sources', () => {
174
- // Arrange: Multiple configs
175
- const customConfig = {
176
- database: {
177
- host: 'localhost',
178
- port: 5432,
179
- },
180
- cache: {
181
- driver: 'redis',
182
- },
183
- };
184
-
185
- const options = {
186
- driver: 'env' as const,
187
- config: customConfig,
188
- };
189
-
190
- // Act: Create module
191
- const module = ConfigModule.forRoot(options);
192
-
193
- // Assert: Configs are merged
194
- expect(module).toBeDefined();
195
- });
196
-
197
- it('should handle nested configuration objects', () => {
198
- // Arrange: Nested config
199
- const customConfig = {
200
- app: {
201
- name: 'Test',
202
- features: {
203
- auth: true,
204
- api: {
205
- version: 'v1',
206
- timeout: 5000,
207
- },
208
- },
209
- },
210
- };
211
-
212
- const options = {
213
- config: customConfig,
214
- };
215
-
216
- // Act: Create module
217
- const module = ConfigModule.forRoot(options);
218
-
219
- // Assert: Nested config is handled
220
- expect(module).toBeDefined();
221
- });
222
- });
223
-
224
- describe('Module Exports', () => {
225
- it('should export ConfigService', () => {
226
- // Act: Create module
227
- const module = ConfigModule.forRoot();
228
-
229
- // Assert: ConfigService is exported
230
- expect(module.exports).toBeDefined();
231
- expect(Array.isArray(module.exports)).toBe(true);
232
- });
233
-
234
- it('should provide all necessary providers', () => {
235
- // Act: Create module
236
- const module = ConfigModule.forRoot();
237
-
238
- // Assert: Providers are defined
239
- expect(module.providers).toBeDefined();
240
- expect(Array.isArray(module.providers)).toBe(true);
241
- expect(module.providers!.length).toBeGreaterThan(0);
242
- });
243
- });
244
- });