@abdokouta/react-config 1.0.0 → 1.0.1
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/.examples/01-basic-usage.ts +7 -5
- package/.examples/02-env-helper.ts +5 -5
- package/.examples/README.md +21 -9
- package/LICENSE +21 -0
- package/README.md +2 -1
- package/__tests__/config.module.test.ts +47 -47
- package/__tests__/drivers/env.driver.test.ts +59 -59
- package/__tests__/services/config.service.test.ts +101 -94
- package/__tests__/setup.d.ts +3 -3
- package/__tests__/vitest.setup.ts +7 -7
- package/config/config.config.ts +9 -9
- package/dist/index.d.mts +33 -1
- package/dist/index.d.ts +33 -1
- 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 +14 -13
- package/src/config.module.ts +12 -14
- package/src/constants/tokens.constant.ts +4 -4
- package/src/drivers/env.driver.ts +24 -10
- package/src/drivers/file.driver.ts +7 -6
- package/src/index.ts +1 -0
- package/src/interfaces/config-driver.interface.ts +1 -1
- package/src/interfaces/config-service.interface.ts +1 -1
- package/src/plugins/vite.plugin.ts +11 -8
- package/src/services/config.service.ts +9 -9
- package/src/utils/define-config.util.ts +35 -0
- package/src/utils/get-nested-value.util.ts +4 -7
- package/src/utils/index.ts +3 -0
- package/src/utils/load-config-file.util.ts +2 -7
- package/src/utils/scan-config-files.util.ts +2 -6
- package/tsup.config.ts +7 -7
- package/vitest.config.ts +15 -15
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Basic Config Usage Example
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* This example demonstrates the fundamental configuration operations:
|
|
5
5
|
* - Environment variable access
|
|
6
6
|
* - Type-safe getters
|
|
7
7
|
* - Default values
|
|
8
8
|
* - Nested configuration
|
|
9
|
-
*
|
|
9
|
+
*
|
|
10
10
|
* @example
|
|
11
11
|
* Run this example:
|
|
12
12
|
* ```bash
|
|
@@ -193,10 +193,12 @@ function getAllConfig(config: ConfigService) {
|
|
|
193
193
|
console.log('\n=== Example 8: Get All Configuration ===\n');
|
|
194
194
|
|
|
195
195
|
const allConfig = config.all();
|
|
196
|
-
const keys = Object.keys(allConfig).filter(
|
|
197
|
-
|
|
196
|
+
const keys = Object.keys(allConfig).filter(
|
|
197
|
+
(k) => k.startsWith('APP_') || k.startsWith('ALLOWED_')
|
|
198
|
+
);
|
|
199
|
+
|
|
198
200
|
console.log('✓ All APP_* and ALLOWED_* configuration:');
|
|
199
|
-
keys.forEach(key => {
|
|
201
|
+
keys.forEach((key) => {
|
|
200
202
|
console.log(` - ${key}: ${allConfig[key]}`);
|
|
201
203
|
});
|
|
202
204
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Env Helper Example
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* This example demonstrates the standalone Env utility for quick
|
|
5
5
|
* environment variable access without dependency injection.
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
7
|
* @example
|
|
8
8
|
* Run this example:
|
|
9
9
|
* ```bash
|
|
@@ -115,10 +115,10 @@ function getAllEnv() {
|
|
|
115
115
|
console.log('\n=== Example 5: Get All Environment Variables ===\n');
|
|
116
116
|
|
|
117
117
|
const allEnv = Env.all();
|
|
118
|
-
const appVars = Object.keys(allEnv).filter(k => k.startsWith('APP_'));
|
|
119
|
-
|
|
118
|
+
const appVars = Object.keys(allEnv).filter((k) => k.startsWith('APP_'));
|
|
119
|
+
|
|
120
120
|
console.log('✓ All APP_* environment variables:');
|
|
121
|
-
appVars.forEach(key => {
|
|
121
|
+
appVars.forEach((key) => {
|
|
122
122
|
console.log(` - ${key}: ${allEnv[key]}`);
|
|
123
123
|
});
|
|
124
124
|
}
|
package/.examples/README.md
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# Config Examples
|
|
2
2
|
|
|
3
|
-
This folder contains examples demonstrating how to use `@abdokouta/config` in
|
|
3
|
+
This folder contains examples demonstrating how to use `@abdokouta/config` in
|
|
4
|
+
various scenarios.
|
|
4
5
|
|
|
5
6
|
## Examples Overview
|
|
6
7
|
|
|
7
8
|
### 1. Basic Usage (`01-basic-usage.ts`)
|
|
8
9
|
|
|
9
10
|
Learn the fundamental configuration operations:
|
|
11
|
+
|
|
10
12
|
- ✅ Environment variable access
|
|
11
13
|
- ✅ Type-safe getters (getString, getNumber, getBool)
|
|
12
14
|
- ✅ Default values
|
|
@@ -15,6 +17,7 @@ Learn the fundamental configuration operations:
|
|
|
15
17
|
- ✅ Array values
|
|
16
18
|
|
|
17
19
|
**Run:**
|
|
20
|
+
|
|
18
21
|
```bash
|
|
19
22
|
ts-node examples/01-basic-usage.ts
|
|
20
23
|
```
|
|
@@ -22,6 +25,7 @@ ts-node examples/01-basic-usage.ts
|
|
|
22
25
|
### 2. Multiple Drivers (`02-multiple-drivers.ts`)
|
|
23
26
|
|
|
24
27
|
Work with different configuration drivers:
|
|
28
|
+
|
|
25
29
|
- ✅ Environment driver (dotenv)
|
|
26
30
|
- ✅ File driver (TypeScript/JSON)
|
|
27
31
|
- ✅ Switching between drivers
|
|
@@ -29,6 +33,7 @@ Work with different configuration drivers:
|
|
|
29
33
|
- ✅ Configuration merging
|
|
30
34
|
|
|
31
35
|
**Run:**
|
|
36
|
+
|
|
32
37
|
```bash
|
|
33
38
|
ts-node examples/02-multiple-drivers.ts
|
|
34
39
|
```
|
|
@@ -36,6 +41,7 @@ ts-node examples/02-multiple-drivers.ts
|
|
|
36
41
|
### 3. Env Helper (`03-env-helper.ts`)
|
|
37
42
|
|
|
38
43
|
Use the standalone Env utility:
|
|
44
|
+
|
|
39
45
|
- ✅ Direct environment access
|
|
40
46
|
- ✅ Type conversions
|
|
41
47
|
- ✅ No service injection needed
|
|
@@ -43,6 +49,7 @@ Use the standalone Env utility:
|
|
|
43
49
|
- ✅ Quick access patterns
|
|
44
50
|
|
|
45
51
|
**Run:**
|
|
52
|
+
|
|
46
53
|
```bash
|
|
47
54
|
ts-node examples/03-env-helper.ts
|
|
48
55
|
```
|
|
@@ -120,7 +127,9 @@ const features = {
|
|
|
120
127
|
### 4. Array Configuration
|
|
121
128
|
|
|
122
129
|
```typescript
|
|
123
|
-
const allowedOrigins = config.getArray('CORS_ORIGINS', [
|
|
130
|
+
const allowedOrigins = config.getArray('CORS_ORIGINS', [
|
|
131
|
+
'http://localhost:3000',
|
|
132
|
+
]);
|
|
124
133
|
const trustedProxies = config.getArray('TRUSTED_PROXIES', []);
|
|
125
134
|
```
|
|
126
135
|
|
|
@@ -194,7 +203,7 @@ const logLevel = config.getString(
|
|
|
194
203
|
```typescript
|
|
195
204
|
function validateConfig(config: ConfigService) {
|
|
196
205
|
const required = ['DB_HOST', 'DB_USER', 'DB_PASSWORD', 'API_KEY'];
|
|
197
|
-
|
|
206
|
+
|
|
198
207
|
for (const key of required) {
|
|
199
208
|
if (!config.has(key)) {
|
|
200
209
|
throw new Error(`Missing required configuration: ${key}`);
|
|
@@ -214,7 +223,7 @@ ConfigModule.forRoot({
|
|
|
214
223
|
ignoreEnvFile: false,
|
|
215
224
|
expandVariables: true,
|
|
216
225
|
isGlobal: true,
|
|
217
|
-
})
|
|
226
|
+
});
|
|
218
227
|
```
|
|
219
228
|
|
|
220
229
|
### File Driver
|
|
@@ -232,7 +241,7 @@ ConfigModule.forRoot({
|
|
|
232
241
|
},
|
|
233
242
|
},
|
|
234
243
|
isGlobal: true,
|
|
235
|
-
})
|
|
244
|
+
});
|
|
236
245
|
```
|
|
237
246
|
|
|
238
247
|
### Custom Configuration
|
|
@@ -248,7 +257,7 @@ ConfigModule.forRoot({
|
|
|
248
257
|
},
|
|
249
258
|
},
|
|
250
259
|
isGlobal: true,
|
|
251
|
-
})
|
|
260
|
+
});
|
|
252
261
|
```
|
|
253
262
|
|
|
254
263
|
## Troubleshooting
|
|
@@ -277,9 +286,12 @@ ConfigModule.forRoot({
|
|
|
277
286
|
## Additional Resources
|
|
278
287
|
|
|
279
288
|
- [Main README](../README.md) - Package documentation
|
|
280
|
-
- [NestJS Config Documentation](https://docs.nestjs.com/techniques/configuration) -
|
|
281
|
-
|
|
289
|
+
- [NestJS Config Documentation](https://docs.nestjs.com/techniques/configuration) -
|
|
290
|
+
Inspiration
|
|
291
|
+
- [dotenv Documentation](https://github.com/motdotla/dotenv) - Environment
|
|
292
|
+
variables
|
|
282
293
|
|
|
283
294
|
## Contributing
|
|
284
295
|
|
|
285
|
-
Found an issue or have a suggestion? Please open an issue or submit a pull
|
|
296
|
+
Found an issue or have a suggestion? Please open an issue or submit a pull
|
|
297
|
+
request!
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Refine
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @fileoverview Tests for ConfigModule
|
|
3
|
-
*
|
|
3
|
+
*
|
|
4
4
|
* This test suite verifies the ConfigModule functionality including:
|
|
5
5
|
* - Module registration (forRoot, forRootAsync)
|
|
6
6
|
* - Driver creation and configuration
|
|
7
7
|
* - Custom config merging
|
|
8
8
|
* - Provider setup
|
|
9
|
-
*
|
|
9
|
+
*
|
|
10
10
|
* @module @abdokouta/config
|
|
11
11
|
* @category Tests
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
-
import { describe, it, expect, beforeEach } from
|
|
15
|
-
import { ConfigModule } from
|
|
16
|
-
import { EnvDriver } from
|
|
17
|
-
import { FileDriver } from
|
|
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
18
|
|
|
19
|
-
describe(
|
|
20
|
-
describe(
|
|
21
|
-
it(
|
|
19
|
+
describe('ConfigModule', () => {
|
|
20
|
+
describe('forRoot', () => {
|
|
21
|
+
it('should create a dynamic module with default configuration', () => {
|
|
22
22
|
// Act: Create module with default config
|
|
23
23
|
const module = ConfigModule.forRoot();
|
|
24
24
|
|
|
@@ -29,11 +29,11 @@ describe("ConfigModule", () => {
|
|
|
29
29
|
expect(module.exports).toBeDefined();
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
-
it(
|
|
32
|
+
it('should create module with env driver', () => {
|
|
33
33
|
// Arrange: Configure env driver
|
|
34
34
|
const options = {
|
|
35
|
-
driver:
|
|
36
|
-
envFilePath:
|
|
35
|
+
driver: 'env' as const,
|
|
36
|
+
envFilePath: '.env.test',
|
|
37
37
|
};
|
|
38
38
|
|
|
39
39
|
// Act: Create module
|
|
@@ -44,11 +44,11 @@ describe("ConfigModule", () => {
|
|
|
44
44
|
expect(module.providers).toBeDefined();
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
it(
|
|
47
|
+
it('should create module with file driver', () => {
|
|
48
48
|
// Arrange: Configure file driver
|
|
49
49
|
const options = {
|
|
50
|
-
driver:
|
|
51
|
-
configPath:
|
|
50
|
+
driver: 'file' as const,
|
|
51
|
+
configPath: './config',
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
// Act: Create module
|
|
@@ -59,17 +59,17 @@ describe("ConfigModule", () => {
|
|
|
59
59
|
expect(module.providers).toBeDefined();
|
|
60
60
|
});
|
|
61
61
|
|
|
62
|
-
it(
|
|
62
|
+
it('should merge custom configuration', () => {
|
|
63
63
|
// Arrange: Custom config
|
|
64
64
|
const customConfig = {
|
|
65
65
|
app: {
|
|
66
|
-
name:
|
|
67
|
-
version:
|
|
66
|
+
name: 'Test App',
|
|
67
|
+
version: '1.0.0',
|
|
68
68
|
},
|
|
69
69
|
};
|
|
70
70
|
|
|
71
71
|
const options = {
|
|
72
|
-
driver:
|
|
72
|
+
driver: 'env' as const,
|
|
73
73
|
config: customConfig,
|
|
74
74
|
};
|
|
75
75
|
|
|
@@ -80,10 +80,10 @@ describe("ConfigModule", () => {
|
|
|
80
80
|
expect(module).toBeDefined();
|
|
81
81
|
});
|
|
82
82
|
|
|
83
|
-
it(
|
|
83
|
+
it('should set global flag when specified', () => {
|
|
84
84
|
// Arrange: Global module config
|
|
85
85
|
const options = {
|
|
86
|
-
driver:
|
|
86
|
+
driver: 'env' as const,
|
|
87
87
|
isGlobal: true,
|
|
88
88
|
};
|
|
89
89
|
|
|
@@ -95,12 +95,12 @@ describe("ConfigModule", () => {
|
|
|
95
95
|
});
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
describe(
|
|
99
|
-
it(
|
|
98
|
+
describe('forRootAsync', () => {
|
|
99
|
+
it('should create async dynamic module', async () => {
|
|
100
100
|
// Arrange: Async factory
|
|
101
101
|
const useFactory = async () => ({
|
|
102
|
-
driver:
|
|
103
|
-
envFilePath:
|
|
102
|
+
driver: 'env' as const,
|
|
103
|
+
envFilePath: '.env',
|
|
104
104
|
});
|
|
105
105
|
|
|
106
106
|
// Act: Create async module
|
|
@@ -113,16 +113,16 @@ describe("ConfigModule", () => {
|
|
|
113
113
|
expect(module.module).toBe(ConfigModule);
|
|
114
114
|
});
|
|
115
115
|
|
|
116
|
-
it(
|
|
116
|
+
it('should handle async factory with dependencies', async () => {
|
|
117
117
|
// Arrange: Factory with inject
|
|
118
118
|
const useFactory = async (dep: any) => ({
|
|
119
|
-
driver:
|
|
119
|
+
driver: 'env' as const,
|
|
120
120
|
});
|
|
121
121
|
|
|
122
122
|
// Act: Create async module
|
|
123
123
|
const module = await ConfigModule.forRootAsync({
|
|
124
124
|
useFactory,
|
|
125
|
-
inject: [
|
|
125
|
+
inject: ['SOME_DEPENDENCY'],
|
|
126
126
|
});
|
|
127
127
|
|
|
128
128
|
// Assert: Module is created
|
|
@@ -130,8 +130,8 @@ describe("ConfigModule", () => {
|
|
|
130
130
|
});
|
|
131
131
|
});
|
|
132
132
|
|
|
133
|
-
describe(
|
|
134
|
-
it(
|
|
133
|
+
describe('Driver Creation', () => {
|
|
134
|
+
it('should create EnvDriver by default', () => {
|
|
135
135
|
// Act: Create module with no driver specified
|
|
136
136
|
const module = ConfigModule.forRoot({});
|
|
137
137
|
|
|
@@ -139,11 +139,11 @@ describe("ConfigModule", () => {
|
|
|
139
139
|
expect(module).toBeDefined();
|
|
140
140
|
});
|
|
141
141
|
|
|
142
|
-
it(
|
|
142
|
+
it('should create FileDriver when specified', () => {
|
|
143
143
|
// Arrange: File driver config
|
|
144
144
|
const options = {
|
|
145
|
-
driver:
|
|
146
|
-
configPath:
|
|
145
|
+
driver: 'file' as const,
|
|
146
|
+
configPath: './config',
|
|
147
147
|
};
|
|
148
148
|
|
|
149
149
|
// Act: Create module
|
|
@@ -153,11 +153,11 @@ describe("ConfigModule", () => {
|
|
|
153
153
|
expect(module).toBeDefined();
|
|
154
154
|
});
|
|
155
155
|
|
|
156
|
-
it(
|
|
156
|
+
it('should pass driver options correctly', () => {
|
|
157
157
|
// Arrange: Driver with options
|
|
158
158
|
const options = {
|
|
159
|
-
driver:
|
|
160
|
-
envFilePath:
|
|
159
|
+
driver: 'env' as const,
|
|
160
|
+
envFilePath: '.env.custom',
|
|
161
161
|
expandVariables: true,
|
|
162
162
|
};
|
|
163
163
|
|
|
@@ -169,21 +169,21 @@ describe("ConfigModule", () => {
|
|
|
169
169
|
});
|
|
170
170
|
});
|
|
171
171
|
|
|
172
|
-
describe(
|
|
173
|
-
it(
|
|
172
|
+
describe('Configuration Merging', () => {
|
|
173
|
+
it('should merge multiple config sources', () => {
|
|
174
174
|
// Arrange: Multiple configs
|
|
175
175
|
const customConfig = {
|
|
176
176
|
database: {
|
|
177
|
-
host:
|
|
177
|
+
host: 'localhost',
|
|
178
178
|
port: 5432,
|
|
179
179
|
},
|
|
180
180
|
cache: {
|
|
181
|
-
driver:
|
|
181
|
+
driver: 'redis',
|
|
182
182
|
},
|
|
183
183
|
};
|
|
184
184
|
|
|
185
185
|
const options = {
|
|
186
|
-
driver:
|
|
186
|
+
driver: 'env' as const,
|
|
187
187
|
config: customConfig,
|
|
188
188
|
};
|
|
189
189
|
|
|
@@ -194,15 +194,15 @@ describe("ConfigModule", () => {
|
|
|
194
194
|
expect(module).toBeDefined();
|
|
195
195
|
});
|
|
196
196
|
|
|
197
|
-
it(
|
|
197
|
+
it('should handle nested configuration objects', () => {
|
|
198
198
|
// Arrange: Nested config
|
|
199
199
|
const customConfig = {
|
|
200
200
|
app: {
|
|
201
|
-
name:
|
|
201
|
+
name: 'Test',
|
|
202
202
|
features: {
|
|
203
203
|
auth: true,
|
|
204
204
|
api: {
|
|
205
|
-
version:
|
|
205
|
+
version: 'v1',
|
|
206
206
|
timeout: 5000,
|
|
207
207
|
},
|
|
208
208
|
},
|
|
@@ -221,8 +221,8 @@ describe("ConfigModule", () => {
|
|
|
221
221
|
});
|
|
222
222
|
});
|
|
223
223
|
|
|
224
|
-
describe(
|
|
225
|
-
it(
|
|
224
|
+
describe('Module Exports', () => {
|
|
225
|
+
it('should export ConfigService', () => {
|
|
226
226
|
// Act: Create module
|
|
227
227
|
const module = ConfigModule.forRoot();
|
|
228
228
|
|
|
@@ -231,7 +231,7 @@ describe("ConfigModule", () => {
|
|
|
231
231
|
expect(Array.isArray(module.exports)).toBe(true);
|
|
232
232
|
});
|
|
233
233
|
|
|
234
|
-
it(
|
|
234
|
+
it('should provide all necessary providers', () => {
|
|
235
235
|
// Act: Create module
|
|
236
236
|
const module = ConfigModule.forRoot();
|
|
237
237
|
|