@geekmidas/envkit 0.2.0 → 0.4.0

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 (73) hide show
  1. package/dist/{EnvironmentBuilder-DfmYRBm-.mjs → EnvironmentBuilder-BSuHZm0y.mjs} +2 -4
  2. package/dist/EnvironmentBuilder-BSuHZm0y.mjs.map +1 -0
  3. package/dist/EnvironmentBuilder-DHfDXJUm.d.mts.map +1 -0
  4. package/dist/{EnvironmentBuilder-W2wku49g.cjs → EnvironmentBuilder-Djr1VsWM.cjs} +2 -4
  5. package/dist/EnvironmentBuilder-Djr1VsWM.cjs.map +1 -0
  6. package/dist/EnvironmentBuilder-Xuf2Dd9u.d.cts.map +1 -0
  7. package/dist/EnvironmentBuilder.cjs +1 -1
  8. package/dist/EnvironmentBuilder.mjs +1 -1
  9. package/dist/EnvironmentParser-Bt246UeP.cjs.map +1 -1
  10. package/dist/{EnvironmentParser-CVWU1ooT.d.mts → EnvironmentParser-CY8TosTN.d.mts} +2 -1
  11. package/dist/EnvironmentParser-CY8TosTN.d.mts.map +1 -0
  12. package/dist/{EnvironmentParser-tV-JjCg7.d.cts → EnvironmentParser-DtOL86NU.d.cts} +2 -1
  13. package/dist/EnvironmentParser-DtOL86NU.d.cts.map +1 -0
  14. package/dist/EnvironmentParser-c06agx31.mjs.map +1 -1
  15. package/dist/EnvironmentParser.d.cts +1 -1
  16. package/dist/EnvironmentParser.d.mts +1 -1
  17. package/dist/SnifferEnvironmentParser.cjs.map +1 -1
  18. package/dist/SnifferEnvironmentParser.d.cts +3 -2
  19. package/dist/SnifferEnvironmentParser.d.cts.map +1 -0
  20. package/dist/SnifferEnvironmentParser.d.mts +3 -2
  21. package/dist/SnifferEnvironmentParser.d.mts.map +1 -0
  22. package/dist/SnifferEnvironmentParser.mjs.map +1 -1
  23. package/dist/{SstEnvironmentBuilder-DEa3lTUB.mjs → SstEnvironmentBuilder-BEBFSUYr.mjs} +2 -2
  24. package/dist/SstEnvironmentBuilder-BEBFSUYr.mjs.map +1 -0
  25. package/dist/SstEnvironmentBuilder-CjURMGjW.d.mts.map +1 -0
  26. package/dist/SstEnvironmentBuilder-D4oSo_KX.d.cts.map +1 -0
  27. package/dist/{SstEnvironmentBuilder-BuFw1hCe.cjs → SstEnvironmentBuilder-wFnN2M5O.cjs} +2 -2
  28. package/dist/SstEnvironmentBuilder-wFnN2M5O.cjs.map +1 -0
  29. package/dist/SstEnvironmentBuilder.cjs +2 -2
  30. package/dist/SstEnvironmentBuilder.mjs +2 -2
  31. package/dist/credentials.cjs +66 -0
  32. package/dist/credentials.cjs.map +1 -0
  33. package/dist/credentials.d.cts +31 -0
  34. package/dist/credentials.d.cts.map +1 -0
  35. package/dist/credentials.d.mts +31 -0
  36. package/dist/credentials.d.mts.map +1 -0
  37. package/dist/credentials.mjs +62 -0
  38. package/dist/credentials.mjs.map +1 -0
  39. package/dist/index.cjs +1 -1
  40. package/dist/index.d.cts +1 -1
  41. package/dist/index.d.mts +1 -1
  42. package/dist/index.mjs +1 -1
  43. package/dist/sst.cjs +2 -2
  44. package/dist/sst.cjs.map +1 -1
  45. package/dist/sst.d.cts +1 -0
  46. package/dist/sst.d.cts.map +1 -0
  47. package/dist/sst.d.mts +1 -0
  48. package/dist/sst.d.mts.map +1 -0
  49. package/dist/sst.mjs +2 -2
  50. package/dist/sst.mjs.map +1 -1
  51. package/examples/basic-usage.ts +329 -333
  52. package/package.json +6 -1
  53. package/src/EnvironmentBuilder.ts +76 -80
  54. package/src/EnvironmentParser.ts +231 -231
  55. package/src/SnifferEnvironmentParser.ts +178 -178
  56. package/src/SstEnvironmentBuilder.ts +127 -127
  57. package/src/__tests__/ConfigParser.spec.ts +388 -388
  58. package/src/__tests__/EnvironmentBuilder.spec.ts +245 -265
  59. package/src/__tests__/EnvironmentParser.spec.ts +828 -828
  60. package/src/__tests__/SnifferEnvironmentParser.spec.ts +380 -326
  61. package/src/__tests__/SstEnvironmentBuilder.spec.ts +347 -367
  62. package/src/__tests__/credentials.integration.spec.ts +239 -0
  63. package/src/__tests__/credentials.spec.ts +136 -0
  64. package/src/__tests__/sst.spec.ts +390 -413
  65. package/src/credentials.ts +99 -0
  66. package/src/index.ts +11 -11
  67. package/src/sst.ts +24 -24
  68. package/sst-env.d.ts +0 -1
  69. package/tsconfig.json +9 -0
  70. package/dist/EnvironmentBuilder-DfmYRBm-.mjs.map +0 -1
  71. package/dist/EnvironmentBuilder-W2wku49g.cjs.map +0 -1
  72. package/dist/SstEnvironmentBuilder-BuFw1hCe.cjs.map +0 -1
  73. package/dist/SstEnvironmentBuilder-DEa3lTUB.mjs.map +0 -1
@@ -3,837 +3,837 @@ import { z } from 'zod/v4';
3
3
  import { EnvironmentParser } from '../EnvironmentParser';
4
4
 
5
5
  describe('EnvironmentParser', () => {
6
- describe('Basic parsing functionality', () => {
7
- it('should parse simple string values', () => {
8
- const env = { APP_NAME: 'Test App' };
9
- const parser = new EnvironmentParser(env);
10
-
11
- const config = parser
12
- .create((get) => ({
13
- appName: get('APP_NAME').string(),
14
- }))
15
- .parse();
16
-
17
- expect(config).toEqual({ appName: 'Test App' });
18
- });
19
-
20
- it('should parse with default values when env var is missing', () => {
21
- const env = {};
22
- const parser = new EnvironmentParser(env);
23
-
24
- const config = parser
25
- .create((get) => ({
26
- appName: get('APP_NAME').string().default('Default App'),
27
- port: get('PORT').string().transform(Number).default(3000),
28
- }))
29
- .parse();
30
-
31
- expect(config).toEqual({
32
- appName: 'Default App',
33
- port: 3000,
34
- });
35
- });
36
-
37
- it('should transform string to number', () => {
38
- const env = { PORT: '8080' };
39
- const parser = new EnvironmentParser(env);
40
-
41
- const config = parser
42
- .create((get) => ({
43
- port: get('PORT').string().transform(Number),
44
- }))
45
- .parse();
46
-
47
- expect(config).toEqual({ port: 8080 });
48
- });
49
-
50
- it('should transform string to boolean', () => {
51
- const env = {
52
- FEATURE_ENABLED: 'true',
53
- FEATURE_DISABLED: 'false',
54
- FEATURE_TRUTHY: 'yes',
55
- };
56
- const parser = new EnvironmentParser(env);
57
-
58
- const config = parser
59
- .create((get) => ({
60
- enabled: get('FEATURE_ENABLED')
61
- .string()
62
- .transform((v) => v === 'true'),
63
- disabled: get('FEATURE_DISABLED')
64
- .string()
65
- .transform((v) => v === 'true'),
66
- truthy: get('FEATURE_TRUTHY')
67
- .string()
68
- .transform((v) => v === 'true'),
69
- }))
70
- .parse();
71
-
72
- expect(config).toEqual({
73
- enabled: true,
74
- disabled: false,
75
- truthy: false,
76
- });
77
- });
78
-
79
- it('should handle optional values', () => {
80
- const env = { REQUIRED: 'value' };
81
- const parser = new EnvironmentParser(env);
82
-
83
- const config = parser
84
- .create((get) => ({
85
- required: get('REQUIRED').string(),
86
- optional: get('OPTIONAL').string().optional(),
87
- }))
88
- .parse();
89
-
90
- expect(config).toEqual({
91
- required: 'value',
92
- optional: undefined,
93
- });
94
- });
95
-
96
- it('should validate URLs', () => {
97
- const env = {
98
- VALID_URL: 'https://example.com',
99
- DATABASE_URL: 'postgresql://user:pass@localhost:5432/db',
100
- };
101
- const parser = new EnvironmentParser(env);
102
-
103
- const config = parser
104
- .create((get) => ({
105
- apiUrl: get('VALID_URL').string().url(),
106
- dbUrl: get('DATABASE_URL').string().url(),
107
- }))
108
- .parse();
109
-
110
- expect(config).toEqual({
111
- apiUrl: 'https://example.com',
112
- dbUrl: 'postgresql://user:pass@localhost:5432/db',
113
- });
114
- });
115
-
116
- it('should validate email addresses', () => {
117
- const env = { ADMIN_EMAIL: 'admin@example.com' };
118
- const parser = new EnvironmentParser(env);
119
-
120
- const config = parser
121
- .create((get) => ({
122
- adminEmail: get('ADMIN_EMAIL').string().email(),
123
- }))
124
- .parse();
125
-
126
- expect(config).toEqual({ adminEmail: 'admin@example.com' });
127
- });
128
-
129
- it('should validate enums', () => {
130
- const env = { NODE_ENV: 'production' };
131
- const parser = new EnvironmentParser(env);
132
-
133
- const config = parser
134
- .create((get) => ({
135
- env: get('NODE_ENV').enum(['development', 'staging', 'production']),
136
- }))
137
- .parse();
138
-
139
- expect(config).toEqual({ env: 'production' });
140
- });
141
- });
142
-
143
- describe('Nested configuration', () => {
144
- it('should handle nested objects', () => {
145
- const env = {
146
- DB_HOST: 'localhost',
147
- DB_PORT: '5432',
148
- DB_NAME: 'myapp',
149
- API_KEY: 'secret123',
150
- API_URL: 'https://api.example.com',
151
- };
152
- const parser = new EnvironmentParser(env);
153
-
154
- const config = parser
155
- .create((get) => ({
156
- database: {
157
- host: get('DB_HOST').string(),
158
- port: get('DB_PORT').string().transform(Number),
159
- name: get('DB_NAME').string(),
160
- },
161
- api: {
162
- key: get('API_KEY').string(),
163
- url: get('API_URL').string().url(),
164
- },
165
- }))
166
- .parse();
167
-
168
- expect(config).toEqual({
169
- database: {
170
- host: 'localhost',
171
- port: 5432,
172
- name: 'myapp',
173
- },
174
- api: {
175
- key: 'secret123',
176
- url: 'https://api.example.com',
177
- },
178
- });
179
- });
180
-
181
- it('should handle deeply nested objects', () => {
182
- const env = {
183
- FEATURE_AUTH_ENABLED: 'true',
184
- FEATURE_AUTH_PROVIDER: 'oauth',
185
- FEATURE_CACHE_ENABLED: 'false',
186
- FEATURE_CACHE_TTL: '3600',
187
- };
188
- const parser = new EnvironmentParser(env);
189
-
190
- const config = parser
191
- .create((get) => ({
192
- features: {
193
- authentication: {
194
- enabled: get('FEATURE_AUTH_ENABLED')
195
- .string()
196
- .transform((v) => v === 'true'),
197
- provider: get('FEATURE_AUTH_PROVIDER').string(),
198
- },
199
- cache: {
200
- enabled: get('FEATURE_CACHE_ENABLED')
201
- .string()
202
- .transform((v) => v === 'true'),
203
- ttl: get('FEATURE_CACHE_TTL').string().transform(Number),
204
- },
205
- },
206
- }))
207
- .parse();
208
-
209
- expect(config).toEqual({
210
- features: {
211
- authentication: {
212
- enabled: true,
213
- provider: 'oauth',
214
- },
215
- cache: {
216
- enabled: false,
217
- ttl: 3600,
218
- },
219
- },
220
- });
221
- });
222
-
223
- it('should handle mixed nested objects with defaults', () => {
224
- const env = {
225
- DB_HOST: 'custom-host',
226
- REDIS_TTL: '7200',
227
- };
228
- const parser = new EnvironmentParser(env);
229
-
230
- const config = parser
231
- .create((get) => ({
232
- database: {
233
- host: get('DB_HOST').string(),
234
- port: get('DB_PORT').string().transform(Number).default(5432),
235
- ssl: get('DB_SSL')
236
- .string()
237
- .transform((v) => v === 'true')
238
- .default(false),
239
- },
240
- cache: {
241
- redis: {
242
- host: get('REDIS_HOST').string().default('localhost'),
243
- port: get('REDIS_PORT').string().transform(Number).default(6379),
244
- ttl: get('REDIS_TTL').string().transform(Number),
245
- },
246
- },
247
- }))
248
- .parse();
249
-
250
- expect(config).toEqual({
251
- database: {
252
- host: 'custom-host',
253
- port: 5432,
254
- ssl: false,
255
- },
256
- cache: {
257
- redis: {
258
- host: 'localhost',
259
- port: 6379,
260
- ttl: 7200,
261
- },
262
- },
263
- });
264
- });
265
- });
266
-
267
- describe('Error handling and validation', () => {
268
- it('should throw ZodError for missing required values', () => {
269
- const env = {};
270
- const parser = new EnvironmentParser(env);
271
-
272
- expect(() => {
273
- parser
274
- .create((get) => ({
275
- required: get('REQUIRED_VAR').string(),
276
- }))
277
- .parse();
278
- }).toThrow(z.ZodError);
279
- });
280
-
281
- it('should throw ZodError with descriptive error messages', () => {
282
- const env = {};
283
- const parser = new EnvironmentParser(env);
284
-
285
- try {
286
- parser
287
- .create((get) => ({
288
- apiKey: get('API_KEY').string().min(32),
289
- dbUrl: get('DATABASE_URL').string().url(),
290
- }))
291
- .parse();
292
- } catch (error) {
293
- expect(error).toBeInstanceOf(z.ZodError);
294
- const zodError = error as z.ZodError;
295
-
296
- expect(zodError.issues).toHaveLength(2);
297
- expect(zodError.issues[0].message).toContain(
298
- 'Environment variable "API_KEY"',
299
- );
300
- expect(zodError.issues[1].message).toContain(
301
- 'Environment variable "DATABASE_URL"',
302
- );
303
- }
304
- });
305
-
306
- it('should validate minimum string length', () => {
307
- const env = { API_KEY: 'short' };
308
- const parser = new EnvironmentParser(env);
309
-
310
- expect(() => {
311
- parser
312
- .create((get) => ({
313
- apiKey: get('API_KEY').string().min(32),
314
- }))
315
- .parse();
316
- }).toThrow(z.ZodError);
317
- });
318
-
319
- it('should validate maximum string length', () => {
320
- const env = { SHORT_TEXT: 'a'.repeat(100) };
321
- const parser = new EnvironmentParser(env);
322
-
323
- expect(() => {
324
- parser
325
- .create((get) => ({
326
- shortText: get('SHORT_TEXT').string().max(50),
327
- }))
328
- .parse();
329
- }).toThrow(z.ZodError);
330
- });
331
-
332
- it('should validate exact string length', () => {
333
- const env = {
334
- VALID_KEY: 'a'.repeat(32),
335
- INVALID_KEY: 'short',
336
- };
337
- const parser = new EnvironmentParser(env);
338
-
339
- const validConfig = parser
340
- .create((get) => ({
341
- key: get('VALID_KEY').string().length(32),
342
- }))
343
- .parse();
344
-
345
- expect(validConfig).toEqual({ key: 'a'.repeat(32) });
346
-
347
- expect(() => {
348
- parser
349
- .create((get) => ({
350
- key: get('INVALID_KEY').string().length(32),
351
- }))
352
- .parse();
353
- }).toThrow(z.ZodError);
354
- });
355
-
356
- it('should validate invalid URLs', () => {
357
- const env = { INVALID_URL: 'not-a-url' };
358
- const parser = new EnvironmentParser(env);
359
-
360
- expect(() => {
361
- parser
362
- .create((get) => ({
363
- url: get('INVALID_URL').string().url(),
364
- }))
365
- .parse();
366
- }).toThrow(z.ZodError);
367
- });
368
-
369
- it('should validate invalid email addresses', () => {
370
- const env = { INVALID_EMAIL: 'not-an-email' };
371
- const parser = new EnvironmentParser(env);
372
-
373
- expect(() => {
374
- parser
375
- .create((get) => ({
376
- email: get('INVALID_EMAIL').string().email(),
377
- }))
378
- .parse();
379
- }).toThrow(z.ZodError);
380
- });
381
-
382
- it('should validate invalid enum values', () => {
383
- const env = { NODE_ENV: 'invalid' };
384
- const parser = new EnvironmentParser(env);
385
-
386
- expect(() => {
387
- parser
388
- .create((get) => ({
389
- env: get('NODE_ENV').enum(['development', 'staging', 'production']),
390
- }))
391
- .parse();
392
- }).toThrow(z.ZodError);
393
- });
394
-
395
- it('should validate number ranges', () => {
396
- const env = {
397
- VALID_PORT: '8080',
398
- INVALID_PORT_LOW: '0',
399
- INVALID_PORT_HIGH: '70000',
400
- };
401
- const parser = new EnvironmentParser(env);
402
-
403
- // Test valid port number
404
- const validConfig = parser
405
- .create((get) => ({
406
- port: get('VALID_PORT').coerce.number().min(1).max(65535),
407
- }))
408
- .parse();
409
-
410
- expect(validConfig).toEqual({ port: 8080 });
411
-
412
- // Test port number too low (0 < 1)
413
- expect(() => {
414
- parser
415
- .create((get) => ({
416
- port: get('INVALID_PORT_LOW').coerce.number().min(1).max(65535),
417
- }))
418
- .parse();
419
- }).toThrow(z.ZodError);
420
-
421
- // Test port number too high (70000 > 65535)
422
- expect(() => {
423
- parser
424
- .create((get) => ({
425
- port: get('INVALID_PORT_HIGH').coerce.number().min(1).max(65535),
426
- }))
427
- .parse();
428
- }).toThrow(z.ZodError);
429
- });
430
-
431
- it('should handle transformation errors', () => {
432
- const env = { INVALID_NUMBER: 'not-a-number' };
433
- const parser = new EnvironmentParser(env);
434
-
435
- expect(() => {
436
- parser
437
- .create((get) => ({
438
- number: get('INVALID_NUMBER')
439
- .string()
440
- .transform((v) => {
441
- const num = Number(v);
442
- if (isNaN(num)) throw new Error('Invalid number');
443
- return num;
444
- }),
445
- }))
446
- .parse();
447
- }).toThrow();
448
- });
449
- });
450
-
451
- describe('Complex scenarios', () => {
452
- it('should handle array transformations', () => {
453
- const env = {
454
- ALLOWED_ORIGINS:
455
- 'http://localhost:3000,https://example.com,https://app.example.com',
456
- FEATURE_FLAGS: 'auth,cache,notifications',
457
- };
458
- const parser = new EnvironmentParser(env);
459
-
460
- const config = parser
461
- .create((get) => ({
462
- cors: {
463
- origins: get('ALLOWED_ORIGINS')
464
- .string()
465
- .transform((origins) => origins.split(',').map((o) => o.trim())),
466
- },
467
- features: get('FEATURE_FLAGS')
468
- .string()
469
- .transform((flags) => flags.split(',').map((f) => f.trim())),
470
- }))
471
- .parse();
472
-
473
- expect(config).toEqual({
474
- cors: {
475
- origins: [
476
- 'http://localhost:3000',
477
- 'https://example.com',
478
- 'https://app.example.com',
479
- ],
480
- },
481
- features: ['auth', 'cache', 'notifications'],
482
- });
483
- });
484
-
485
- it('should handle JSON parsing', () => {
486
- const env = {
487
- FEATURE_CONFIG: '{"auth":true,"cache":false,"debug":true}',
488
- RATE_LIMITS: '{"requests":100,"window":60000}',
489
- };
490
- const parser = new EnvironmentParser(env);
491
-
492
- const config = parser
493
- .create((get) => ({
494
- features: get('FEATURE_CONFIG')
495
- .string()
496
- .transform((str) => JSON.parse(str))
497
- .pipe(z.record(z.string(), z.boolean())),
498
- rateLimits: get('RATE_LIMITS')
499
- .string()
500
- .transform((str) => JSON.parse(str))
501
- .pipe(z.object({ requests: z.number(), window: z.number() })),
502
- }))
503
- .parse();
504
-
505
- expect(config).toEqual({
506
- features: {
507
- auth: true,
508
- cache: false,
509
- debug: true,
510
- },
511
- rateLimits: {
512
- requests: 100,
513
- window: 60000,
514
- },
515
- });
516
- });
517
-
518
- it('should handle custom refinements', () => {
519
- const env = {
520
- CORS_ORIGINS: 'https://example.com,https://app.example.com',
521
- API_KEYS:
522
- 'key1234567890123456789012345678901,key2345678901234567890123456789012',
523
- };
524
- const parser = new EnvironmentParser(env);
525
-
526
- const config = parser
527
- .create((get) => ({
528
- cors: {
529
- origins: get('CORS_ORIGINS')
530
- .string()
531
- .transform((origins) => origins.split(',').map((o) => o.trim()))
532
- .refine(
533
- (origins) => origins.every((o) => o.startsWith('https://')),
534
- { message: 'All CORS origins must use HTTPS' },
535
- ),
536
- },
537
- apiKeys: get('API_KEYS')
538
- .string()
539
- .transform((keys) => keys.split(',').map((k) => k.trim()))
540
- .pipe(z.array(z.string().min(32))),
541
- }))
542
- .parse();
543
-
544
- expect(config).toEqual({
545
- cors: {
546
- origins: ['https://example.com', 'https://app.example.com'],
547
- },
548
- apiKeys: [
549
- 'key1234567890123456789012345678901',
550
- 'key2345678901234567890123456789012',
551
- ],
552
- });
553
- });
554
-
555
- it('should fail refinements with descriptive errors', () => {
556
- const env = {
557
- CORS_ORIGINS: 'http://example.com,https://app.example.com',
558
- };
559
- const parser = new EnvironmentParser(env);
560
-
561
- expect(() => {
562
- parser
563
- .create((get) => ({
564
- cors: {
565
- origins: get('CORS_ORIGINS')
566
- .string()
567
- .transform((origins) => origins.split(',').map((o) => o.trim()))
568
- .refine(
569
- (origins) => origins.every((o) => o.startsWith('https://')),
570
- { message: 'All CORS origins must use HTTPS' },
571
- ),
572
- },
573
- }))
574
- .parse();
575
- }).toThrow(z.ZodError);
576
- });
577
- });
578
-
579
- describe('Type inference', () => {
580
- it('should correctly infer string types', () => {
581
- const env = { APP_NAME: 'Test App' };
582
- const parser = new EnvironmentParser(env);
583
-
584
- const config = parser
585
- .create((get) => ({
586
- appName: get('APP_NAME').string(),
587
- }))
588
- .parse();
589
-
590
- // TypeScript should infer this as { appName: string }
591
- type ConfigType = typeof config;
592
- type ExpectedType = { appName: string };
593
-
594
- // This will compile if types match correctly
595
- const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
596
- const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
597
-
598
- expect(_typeCheck).toBe(true);
599
- expect(_typeCheck2).toBe(true);
600
- });
601
-
602
- it('should correctly infer number types after transformation', () => {
603
- const env = { PORT: '3000' };
604
- const parser = new EnvironmentParser(env);
605
-
606
- const config = parser
607
- .create((get) => ({
608
- port: get('PORT').string().transform(Number),
609
- }))
610
- .parse();
611
-
612
- // TypeScript should infer this as { port: number }
613
- type ConfigType = typeof config;
614
- type ExpectedType = { port: number };
615
-
616
- const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
617
- const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
618
-
619
- expect(_typeCheck).toBe(true);
620
- expect(_typeCheck2).toBe(true);
621
- });
622
-
623
- it('should correctly infer boolean types after transformation', () => {
624
- const env = { FEATURE_ENABLED: 'true' };
625
- const parser = new EnvironmentParser(env);
626
-
627
- const config = parser
628
- .create((get) => ({
629
- enabled: get('FEATURE_ENABLED')
630
- .string()
631
- .transform((v) => v === 'true'),
632
- }))
633
- .parse();
634
-
635
- // TypeScript should infer this as { enabled: boolean }
636
- type ConfigType = typeof config;
637
- type ExpectedType = { enabled: boolean };
638
-
639
- const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
640
- const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
641
-
642
- expect(_typeCheck).toBe(true);
643
- expect(_typeCheck2).toBe(true);
644
- });
645
-
646
- it('should correctly infer optional types', () => {
647
- const env = { REQUIRED: 'value' };
648
- const parser = new EnvironmentParser(env);
649
-
650
- const config = parser
651
- .create((get) => ({
652
- required: get('REQUIRED').string(),
653
- optional: get('OPTIONAL').string().optional(),
654
- }))
655
- .parse();
656
- });
657
-
658
- it('should correctly infer nested object types', () => {
659
- const env = {
660
- DB_HOST: 'localhost',
661
- DB_PORT: '5432',
662
- API_KEY: 'secret',
663
- };
664
- const parser = new EnvironmentParser(env);
665
-
666
- const config = parser
667
- .create((get) => ({
668
- database: {
669
- host: get('DB_HOST').string(),
670
- port: get('DB_PORT').string().transform(Number),
671
- },
672
- api: {
673
- key: get('API_KEY').string(),
674
- },
675
- }))
676
- .parse();
677
-
678
- // TypeScript should infer the correct nested structure
679
- type ConfigType = typeof config;
680
- type ExpectedType = {
681
- database: { host: string; port: number };
682
- api: { key: string };
683
- };
684
-
685
- const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
686
- const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
687
-
688
- expect(_typeCheck).toBe(true);
689
- expect(_typeCheck2).toBe(true);
690
- });
691
- });
692
-
693
- describe('Environment variable tracking', () => {
694
- it('should track accessed environment variables', () => {
695
- const env = { APP_NAME: 'Test App', PORT: '3000' };
696
- const parser = new EnvironmentParser(env);
697
-
698
- const config = parser.create((get) => ({
699
- appName: get('APP_NAME').string(),
700
- port: get('PORT').string().transform(Number),
701
- }));
702
-
703
- const envVars = config.getEnvironmentVariables();
704
-
705
- expect(envVars).toEqual(['APP_NAME', 'PORT']);
706
- });
707
-
708
- it('should track variables even when not parsed', () => {
709
- const env = {};
710
- const parser = new EnvironmentParser(env);
711
-
712
- const config = parser.create((get) => ({
713
- database: get('DATABASE_URL').string().optional(),
714
- redis: get('REDIS_URL').string().optional(),
715
- }));
716
-
717
- // Should track even without calling parse()
718
- const envVars = config.getEnvironmentVariables();
719
-
720
- expect(envVars).toEqual(['DATABASE_URL', 'REDIS_URL']);
721
- });
722
-
723
- it('should track variables in nested configurations', () => {
724
- const env = {
725
- DB_HOST: 'localhost',
726
- DB_PORT: '5432',
727
- API_KEY: 'secret',
728
- };
729
- const parser = new EnvironmentParser(env);
730
-
731
- const config = parser.create((get) => ({
732
- database: {
733
- host: get('DB_HOST').string(),
734
- port: get('DB_PORT').string().transform(Number),
735
- },
736
- api: {
737
- key: get('API_KEY').string(),
738
- },
739
- }));
740
-
741
- const envVars = config.getEnvironmentVariables();
742
-
743
- expect(envVars).toEqual(['API_KEY', 'DB_HOST', 'DB_PORT']);
744
- });
745
-
746
- it('should return sorted environment variable names', () => {
747
- const env = {};
748
- const parser = new EnvironmentParser(env);
749
-
750
- const config = parser.create((get) => ({
751
- zValue: get('Z_VALUE').string().optional(),
752
- aValue: get('A_VALUE').string().optional(),
753
- mValue: get('M_VALUE').string().optional(),
754
- }));
755
-
756
- const envVars = config.getEnvironmentVariables();
757
-
758
- // Should be sorted alphabetically
759
- expect(envVars).toEqual(['A_VALUE', 'M_VALUE', 'Z_VALUE']);
760
- });
761
-
762
- it('should deduplicate environment variable names', () => {
763
- const env = { SHARED_VAR: 'value' };
764
- const parser = new EnvironmentParser(env);
765
-
766
- const config = parser.create((get) => ({
767
- value1: get('SHARED_VAR').string(),
768
- value2: get('SHARED_VAR').string(),
769
- value3: get('SHARED_VAR').string(),
770
- }));
771
-
772
- const envVars = config.getEnvironmentVariables();
773
-
774
- // Should only appear once despite being accessed 3 times
775
- expect(envVars).toEqual(['SHARED_VAR']);
776
- });
777
-
778
- it('should track variables with default values', () => {
779
- const env = {};
780
- const parser = new EnvironmentParser(env);
781
-
782
- const config = parser.create((get) => ({
783
- port: get('PORT').string().default('3000'),
784
- host: get('HOST').string().default('localhost'),
785
- }));
786
-
787
- const envVars = config.getEnvironmentVariables();
788
-
789
- // Should track even when defaults are used
790
- expect(envVars).toEqual(['HOST', 'PORT']);
791
- });
6
+ describe('Basic parsing functionality', () => {
7
+ it('should parse simple string values', () => {
8
+ const env = { APP_NAME: 'Test App' };
9
+ const parser = new EnvironmentParser(env);
10
+
11
+ const config = parser
12
+ .create((get) => ({
13
+ appName: get('APP_NAME').string(),
14
+ }))
15
+ .parse();
16
+
17
+ expect(config).toEqual({ appName: 'Test App' });
18
+ });
19
+
20
+ it('should parse with default values when env var is missing', () => {
21
+ const env = {};
22
+ const parser = new EnvironmentParser(env);
23
+
24
+ const config = parser
25
+ .create((get) => ({
26
+ appName: get('APP_NAME').string().default('Default App'),
27
+ port: get('PORT').string().transform(Number).default(3000),
28
+ }))
29
+ .parse();
30
+
31
+ expect(config).toEqual({
32
+ appName: 'Default App',
33
+ port: 3000,
34
+ });
35
+ });
36
+
37
+ it('should transform string to number', () => {
38
+ const env = { PORT: '8080' };
39
+ const parser = new EnvironmentParser(env);
40
+
41
+ const config = parser
42
+ .create((get) => ({
43
+ port: get('PORT').string().transform(Number),
44
+ }))
45
+ .parse();
46
+
47
+ expect(config).toEqual({ port: 8080 });
48
+ });
49
+
50
+ it('should transform string to boolean', () => {
51
+ const env = {
52
+ FEATURE_ENABLED: 'true',
53
+ FEATURE_DISABLED: 'false',
54
+ FEATURE_TRUTHY: 'yes',
55
+ };
56
+ const parser = new EnvironmentParser(env);
57
+
58
+ const config = parser
59
+ .create((get) => ({
60
+ enabled: get('FEATURE_ENABLED')
61
+ .string()
62
+ .transform((v) => v === 'true'),
63
+ disabled: get('FEATURE_DISABLED')
64
+ .string()
65
+ .transform((v) => v === 'true'),
66
+ truthy: get('FEATURE_TRUTHY')
67
+ .string()
68
+ .transform((v) => v === 'true'),
69
+ }))
70
+ .parse();
71
+
72
+ expect(config).toEqual({
73
+ enabled: true,
74
+ disabled: false,
75
+ truthy: false,
76
+ });
77
+ });
78
+
79
+ it('should handle optional values', () => {
80
+ const env = { REQUIRED: 'value' };
81
+ const parser = new EnvironmentParser(env);
82
+
83
+ const config = parser
84
+ .create((get) => ({
85
+ required: get('REQUIRED').string(),
86
+ optional: get('OPTIONAL').string().optional(),
87
+ }))
88
+ .parse();
89
+
90
+ expect(config).toEqual({
91
+ required: 'value',
92
+ optional: undefined,
93
+ });
94
+ });
95
+
96
+ it('should validate URLs', () => {
97
+ const env = {
98
+ VALID_URL: 'https://example.com',
99
+ DATABASE_URL: 'postgresql://user:pass@localhost:5432/db',
100
+ };
101
+ const parser = new EnvironmentParser(env);
102
+
103
+ const config = parser
104
+ .create((get) => ({
105
+ apiUrl: get('VALID_URL').string().url(),
106
+ dbUrl: get('DATABASE_URL').string().url(),
107
+ }))
108
+ .parse();
109
+
110
+ expect(config).toEqual({
111
+ apiUrl: 'https://example.com',
112
+ dbUrl: 'postgresql://user:pass@localhost:5432/db',
113
+ });
114
+ });
115
+
116
+ it('should validate email addresses', () => {
117
+ const env = { ADMIN_EMAIL: 'admin@example.com' };
118
+ const parser = new EnvironmentParser(env);
119
+
120
+ const config = parser
121
+ .create((get) => ({
122
+ adminEmail: get('ADMIN_EMAIL').string().email(),
123
+ }))
124
+ .parse();
125
+
126
+ expect(config).toEqual({ adminEmail: 'admin@example.com' });
127
+ });
128
+
129
+ it('should validate enums', () => {
130
+ const env = { NODE_ENV: 'production' };
131
+ const parser = new EnvironmentParser(env);
132
+
133
+ const config = parser
134
+ .create((get) => ({
135
+ env: get('NODE_ENV').enum(['development', 'staging', 'production']),
136
+ }))
137
+ .parse();
138
+
139
+ expect(config).toEqual({ env: 'production' });
140
+ });
141
+ });
142
+
143
+ describe('Nested configuration', () => {
144
+ it('should handle nested objects', () => {
145
+ const env = {
146
+ DB_HOST: 'localhost',
147
+ DB_PORT: '5432',
148
+ DB_NAME: 'myapp',
149
+ API_KEY: 'secret123',
150
+ API_URL: 'https://api.example.com',
151
+ };
152
+ const parser = new EnvironmentParser(env);
153
+
154
+ const config = parser
155
+ .create((get) => ({
156
+ database: {
157
+ host: get('DB_HOST').string(),
158
+ port: get('DB_PORT').string().transform(Number),
159
+ name: get('DB_NAME').string(),
160
+ },
161
+ api: {
162
+ key: get('API_KEY').string(),
163
+ url: get('API_URL').string().url(),
164
+ },
165
+ }))
166
+ .parse();
167
+
168
+ expect(config).toEqual({
169
+ database: {
170
+ host: 'localhost',
171
+ port: 5432,
172
+ name: 'myapp',
173
+ },
174
+ api: {
175
+ key: 'secret123',
176
+ url: 'https://api.example.com',
177
+ },
178
+ });
179
+ });
180
+
181
+ it('should handle deeply nested objects', () => {
182
+ const env = {
183
+ FEATURE_AUTH_ENABLED: 'true',
184
+ FEATURE_AUTH_PROVIDER: 'oauth',
185
+ FEATURE_CACHE_ENABLED: 'false',
186
+ FEATURE_CACHE_TTL: '3600',
187
+ };
188
+ const parser = new EnvironmentParser(env);
189
+
190
+ const config = parser
191
+ .create((get) => ({
192
+ features: {
193
+ authentication: {
194
+ enabled: get('FEATURE_AUTH_ENABLED')
195
+ .string()
196
+ .transform((v) => v === 'true'),
197
+ provider: get('FEATURE_AUTH_PROVIDER').string(),
198
+ },
199
+ cache: {
200
+ enabled: get('FEATURE_CACHE_ENABLED')
201
+ .string()
202
+ .transform((v) => v === 'true'),
203
+ ttl: get('FEATURE_CACHE_TTL').string().transform(Number),
204
+ },
205
+ },
206
+ }))
207
+ .parse();
208
+
209
+ expect(config).toEqual({
210
+ features: {
211
+ authentication: {
212
+ enabled: true,
213
+ provider: 'oauth',
214
+ },
215
+ cache: {
216
+ enabled: false,
217
+ ttl: 3600,
218
+ },
219
+ },
220
+ });
221
+ });
222
+
223
+ it('should handle mixed nested objects with defaults', () => {
224
+ const env = {
225
+ DB_HOST: 'custom-host',
226
+ REDIS_TTL: '7200',
227
+ };
228
+ const parser = new EnvironmentParser(env);
229
+
230
+ const config = parser
231
+ .create((get) => ({
232
+ database: {
233
+ host: get('DB_HOST').string(),
234
+ port: get('DB_PORT').string().transform(Number).default(5432),
235
+ ssl: get('DB_SSL')
236
+ .string()
237
+ .transform((v) => v === 'true')
238
+ .default(false),
239
+ },
240
+ cache: {
241
+ redis: {
242
+ host: get('REDIS_HOST').string().default('localhost'),
243
+ port: get('REDIS_PORT').string().transform(Number).default(6379),
244
+ ttl: get('REDIS_TTL').string().transform(Number),
245
+ },
246
+ },
247
+ }))
248
+ .parse();
249
+
250
+ expect(config).toEqual({
251
+ database: {
252
+ host: 'custom-host',
253
+ port: 5432,
254
+ ssl: false,
255
+ },
256
+ cache: {
257
+ redis: {
258
+ host: 'localhost',
259
+ port: 6379,
260
+ ttl: 7200,
261
+ },
262
+ },
263
+ });
264
+ });
265
+ });
266
+
267
+ describe('Error handling and validation', () => {
268
+ it('should throw ZodError for missing required values', () => {
269
+ const env = {};
270
+ const parser = new EnvironmentParser(env);
271
+
272
+ expect(() => {
273
+ parser
274
+ .create((get) => ({
275
+ required: get('REQUIRED_VAR').string(),
276
+ }))
277
+ .parse();
278
+ }).toThrow(z.ZodError);
279
+ });
280
+
281
+ it('should throw ZodError with descriptive error messages', () => {
282
+ const env = {};
283
+ const parser = new EnvironmentParser(env);
284
+
285
+ try {
286
+ parser
287
+ .create((get) => ({
288
+ apiKey: get('API_KEY').string().min(32),
289
+ dbUrl: get('DATABASE_URL').string().url(),
290
+ }))
291
+ .parse();
292
+ } catch (error) {
293
+ expect(error).toBeInstanceOf(z.ZodError);
294
+ const zodError = error as z.ZodError;
295
+
296
+ expect(zodError.issues).toHaveLength(2);
297
+ expect(zodError.issues[0].message).toContain(
298
+ 'Environment variable "API_KEY"',
299
+ );
300
+ expect(zodError.issues[1].message).toContain(
301
+ 'Environment variable "DATABASE_URL"',
302
+ );
303
+ }
304
+ });
305
+
306
+ it('should validate minimum string length', () => {
307
+ const env = { API_KEY: 'short' };
308
+ const parser = new EnvironmentParser(env);
309
+
310
+ expect(() => {
311
+ parser
312
+ .create((get) => ({
313
+ apiKey: get('API_KEY').string().min(32),
314
+ }))
315
+ .parse();
316
+ }).toThrow(z.ZodError);
317
+ });
318
+
319
+ it('should validate maximum string length', () => {
320
+ const env = { SHORT_TEXT: 'a'.repeat(100) };
321
+ const parser = new EnvironmentParser(env);
322
+
323
+ expect(() => {
324
+ parser
325
+ .create((get) => ({
326
+ shortText: get('SHORT_TEXT').string().max(50),
327
+ }))
328
+ .parse();
329
+ }).toThrow(z.ZodError);
330
+ });
331
+
332
+ it('should validate exact string length', () => {
333
+ const env = {
334
+ VALID_KEY: 'a'.repeat(32),
335
+ INVALID_KEY: 'short',
336
+ };
337
+ const parser = new EnvironmentParser(env);
338
+
339
+ const validConfig = parser
340
+ .create((get) => ({
341
+ key: get('VALID_KEY').string().length(32),
342
+ }))
343
+ .parse();
344
+
345
+ expect(validConfig).toEqual({ key: 'a'.repeat(32) });
346
+
347
+ expect(() => {
348
+ parser
349
+ .create((get) => ({
350
+ key: get('INVALID_KEY').string().length(32),
351
+ }))
352
+ .parse();
353
+ }).toThrow(z.ZodError);
354
+ });
355
+
356
+ it('should validate invalid URLs', () => {
357
+ const env = { INVALID_URL: 'not-a-url' };
358
+ const parser = new EnvironmentParser(env);
359
+
360
+ expect(() => {
361
+ parser
362
+ .create((get) => ({
363
+ url: get('INVALID_URL').string().url(),
364
+ }))
365
+ .parse();
366
+ }).toThrow(z.ZodError);
367
+ });
368
+
369
+ it('should validate invalid email addresses', () => {
370
+ const env = { INVALID_EMAIL: 'not-an-email' };
371
+ const parser = new EnvironmentParser(env);
372
+
373
+ expect(() => {
374
+ parser
375
+ .create((get) => ({
376
+ email: get('INVALID_EMAIL').string().email(),
377
+ }))
378
+ .parse();
379
+ }).toThrow(z.ZodError);
380
+ });
381
+
382
+ it('should validate invalid enum values', () => {
383
+ const env = { NODE_ENV: 'invalid' };
384
+ const parser = new EnvironmentParser(env);
385
+
386
+ expect(() => {
387
+ parser
388
+ .create((get) => ({
389
+ env: get('NODE_ENV').enum(['development', 'staging', 'production']),
390
+ }))
391
+ .parse();
392
+ }).toThrow(z.ZodError);
393
+ });
394
+
395
+ it('should validate number ranges', () => {
396
+ const env = {
397
+ VALID_PORT: '8080',
398
+ INVALID_PORT_LOW: '0',
399
+ INVALID_PORT_HIGH: '70000',
400
+ };
401
+ const parser = new EnvironmentParser(env);
402
+
403
+ // Test valid port number
404
+ const validConfig = parser
405
+ .create((get) => ({
406
+ port: get('VALID_PORT').coerce.number().min(1).max(65535),
407
+ }))
408
+ .parse();
409
+
410
+ expect(validConfig).toEqual({ port: 8080 });
411
+
412
+ // Test port number too low (0 < 1)
413
+ expect(() => {
414
+ parser
415
+ .create((get) => ({
416
+ port: get('INVALID_PORT_LOW').coerce.number().min(1).max(65535),
417
+ }))
418
+ .parse();
419
+ }).toThrow(z.ZodError);
420
+
421
+ // Test port number too high (70000 > 65535)
422
+ expect(() => {
423
+ parser
424
+ .create((get) => ({
425
+ port: get('INVALID_PORT_HIGH').coerce.number().min(1).max(65535),
426
+ }))
427
+ .parse();
428
+ }).toThrow(z.ZodError);
429
+ });
430
+
431
+ it('should handle transformation errors', () => {
432
+ const env = { INVALID_NUMBER: 'not-a-number' };
433
+ const parser = new EnvironmentParser(env);
434
+
435
+ expect(() => {
436
+ parser
437
+ .create((get) => ({
438
+ number: get('INVALID_NUMBER')
439
+ .string()
440
+ .transform((v) => {
441
+ const num = Number(v);
442
+ if (Number.isNaN(num)) throw new Error('Invalid number');
443
+ return num;
444
+ }),
445
+ }))
446
+ .parse();
447
+ }).toThrow();
448
+ });
449
+ });
450
+
451
+ describe('Complex scenarios', () => {
452
+ it('should handle array transformations', () => {
453
+ const env = {
454
+ ALLOWED_ORIGINS:
455
+ 'http://localhost:3000,https://example.com,https://app.example.com',
456
+ FEATURE_FLAGS: 'auth,cache,notifications',
457
+ };
458
+ const parser = new EnvironmentParser(env);
459
+
460
+ const config = parser
461
+ .create((get) => ({
462
+ cors: {
463
+ origins: get('ALLOWED_ORIGINS')
464
+ .string()
465
+ .transform((origins) => origins.split(',').map((o) => o.trim())),
466
+ },
467
+ features: get('FEATURE_FLAGS')
468
+ .string()
469
+ .transform((flags) => flags.split(',').map((f) => f.trim())),
470
+ }))
471
+ .parse();
472
+
473
+ expect(config).toEqual({
474
+ cors: {
475
+ origins: [
476
+ 'http://localhost:3000',
477
+ 'https://example.com',
478
+ 'https://app.example.com',
479
+ ],
480
+ },
481
+ features: ['auth', 'cache', 'notifications'],
482
+ });
483
+ });
484
+
485
+ it('should handle JSON parsing', () => {
486
+ const env = {
487
+ FEATURE_CONFIG: '{"auth":true,"cache":false,"debug":true}',
488
+ RATE_LIMITS: '{"requests":100,"window":60000}',
489
+ };
490
+ const parser = new EnvironmentParser(env);
491
+
492
+ const config = parser
493
+ .create((get) => ({
494
+ features: get('FEATURE_CONFIG')
495
+ .string()
496
+ .transform((str) => JSON.parse(str))
497
+ .pipe(z.record(z.string(), z.boolean())),
498
+ rateLimits: get('RATE_LIMITS')
499
+ .string()
500
+ .transform((str) => JSON.parse(str))
501
+ .pipe(z.object({ requests: z.number(), window: z.number() })),
502
+ }))
503
+ .parse();
504
+
505
+ expect(config).toEqual({
506
+ features: {
507
+ auth: true,
508
+ cache: false,
509
+ debug: true,
510
+ },
511
+ rateLimits: {
512
+ requests: 100,
513
+ window: 60000,
514
+ },
515
+ });
516
+ });
517
+
518
+ it('should handle custom refinements', () => {
519
+ const env = {
520
+ CORS_ORIGINS: 'https://example.com,https://app.example.com',
521
+ API_KEYS:
522
+ 'key1234567890123456789012345678901,key2345678901234567890123456789012',
523
+ };
524
+ const parser = new EnvironmentParser(env);
525
+
526
+ const config = parser
527
+ .create((get) => ({
528
+ cors: {
529
+ origins: get('CORS_ORIGINS')
530
+ .string()
531
+ .transform((origins) => origins.split(',').map((o) => o.trim()))
532
+ .refine(
533
+ (origins) => origins.every((o) => o.startsWith('https://')),
534
+ { message: 'All CORS origins must use HTTPS' },
535
+ ),
536
+ },
537
+ apiKeys: get('API_KEYS')
538
+ .string()
539
+ .transform((keys) => keys.split(',').map((k) => k.trim()))
540
+ .pipe(z.array(z.string().min(32))),
541
+ }))
542
+ .parse();
543
+
544
+ expect(config).toEqual({
545
+ cors: {
546
+ origins: ['https://example.com', 'https://app.example.com'],
547
+ },
548
+ apiKeys: [
549
+ 'key1234567890123456789012345678901',
550
+ 'key2345678901234567890123456789012',
551
+ ],
552
+ });
553
+ });
554
+
555
+ it('should fail refinements with descriptive errors', () => {
556
+ const env = {
557
+ CORS_ORIGINS: 'http://example.com,https://app.example.com',
558
+ };
559
+ const parser = new EnvironmentParser(env);
560
+
561
+ expect(() => {
562
+ parser
563
+ .create((get) => ({
564
+ cors: {
565
+ origins: get('CORS_ORIGINS')
566
+ .string()
567
+ .transform((origins) => origins.split(',').map((o) => o.trim()))
568
+ .refine(
569
+ (origins) => origins.every((o) => o.startsWith('https://')),
570
+ { message: 'All CORS origins must use HTTPS' },
571
+ ),
572
+ },
573
+ }))
574
+ .parse();
575
+ }).toThrow(z.ZodError);
576
+ });
577
+ });
578
+
579
+ describe('Type inference', () => {
580
+ it('should correctly infer string types', () => {
581
+ const env = { APP_NAME: 'Test App' };
582
+ const parser = new EnvironmentParser(env);
583
+
584
+ const config = parser
585
+ .create((get) => ({
586
+ appName: get('APP_NAME').string(),
587
+ }))
588
+ .parse();
589
+
590
+ // TypeScript should infer this as { appName: string }
591
+ type ConfigType = typeof config;
592
+ type ExpectedType = { appName: string };
593
+
594
+ // This will compile if types match correctly
595
+ const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
596
+ const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
597
+
598
+ expect(_typeCheck).toBe(true);
599
+ expect(_typeCheck2).toBe(true);
600
+ });
601
+
602
+ it('should correctly infer number types after transformation', () => {
603
+ const env = { PORT: '3000' };
604
+ const parser = new EnvironmentParser(env);
605
+
606
+ const config = parser
607
+ .create((get) => ({
608
+ port: get('PORT').string().transform(Number),
609
+ }))
610
+ .parse();
611
+
612
+ // TypeScript should infer this as { port: number }
613
+ type ConfigType = typeof config;
614
+ type ExpectedType = { port: number };
615
+
616
+ const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
617
+ const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
618
+
619
+ expect(_typeCheck).toBe(true);
620
+ expect(_typeCheck2).toBe(true);
621
+ });
622
+
623
+ it('should correctly infer boolean types after transformation', () => {
624
+ const env = { FEATURE_ENABLED: 'true' };
625
+ const parser = new EnvironmentParser(env);
626
+
627
+ const config = parser
628
+ .create((get) => ({
629
+ enabled: get('FEATURE_ENABLED')
630
+ .string()
631
+ .transform((v) => v === 'true'),
632
+ }))
633
+ .parse();
634
+
635
+ // TypeScript should infer this as { enabled: boolean }
636
+ type ConfigType = typeof config;
637
+ type ExpectedType = { enabled: boolean };
638
+
639
+ const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
640
+ const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
641
+
642
+ expect(_typeCheck).toBe(true);
643
+ expect(_typeCheck2).toBe(true);
644
+ });
645
+
646
+ it('should correctly infer optional types', () => {
647
+ const env = { REQUIRED: 'value' };
648
+ const parser = new EnvironmentParser(env);
649
+
650
+ const _config = parser
651
+ .create((get) => ({
652
+ required: get('REQUIRED').string(),
653
+ optional: get('OPTIONAL').string().optional(),
654
+ }))
655
+ .parse();
656
+ });
657
+
658
+ it('should correctly infer nested object types', () => {
659
+ const env = {
660
+ DB_HOST: 'localhost',
661
+ DB_PORT: '5432',
662
+ API_KEY: 'secret',
663
+ };
664
+ const parser = new EnvironmentParser(env);
665
+
666
+ const config = parser
667
+ .create((get) => ({
668
+ database: {
669
+ host: get('DB_HOST').string(),
670
+ port: get('DB_PORT').string().transform(Number),
671
+ },
672
+ api: {
673
+ key: get('API_KEY').string(),
674
+ },
675
+ }))
676
+ .parse();
677
+
678
+ // TypeScript should infer the correct nested structure
679
+ type ConfigType = typeof config;
680
+ type ExpectedType = {
681
+ database: { host: string; port: number };
682
+ api: { key: string };
683
+ };
684
+
685
+ const _typeCheck: ConfigType extends ExpectedType ? true : false = true;
686
+ const _typeCheck2: ExpectedType extends ConfigType ? true : false = true;
687
+
688
+ expect(_typeCheck).toBe(true);
689
+ expect(_typeCheck2).toBe(true);
690
+ });
691
+ });
692
+
693
+ describe('Environment variable tracking', () => {
694
+ it('should track accessed environment variables', () => {
695
+ const env = { APP_NAME: 'Test App', PORT: '3000' };
696
+ const parser = new EnvironmentParser(env);
697
+
698
+ const config = parser.create((get) => ({
699
+ appName: get('APP_NAME').string(),
700
+ port: get('PORT').string().transform(Number),
701
+ }));
702
+
703
+ const envVars = config.getEnvironmentVariables();
704
+
705
+ expect(envVars).toEqual(['APP_NAME', 'PORT']);
706
+ });
707
+
708
+ it('should track variables even when not parsed', () => {
709
+ const env = {};
710
+ const parser = new EnvironmentParser(env);
711
+
712
+ const config = parser.create((get) => ({
713
+ database: get('DATABASE_URL').string().optional(),
714
+ redis: get('REDIS_URL').string().optional(),
715
+ }));
716
+
717
+ // Should track even without calling parse()
718
+ const envVars = config.getEnvironmentVariables();
719
+
720
+ expect(envVars).toEqual(['DATABASE_URL', 'REDIS_URL']);
721
+ });
722
+
723
+ it('should track variables in nested configurations', () => {
724
+ const env = {
725
+ DB_HOST: 'localhost',
726
+ DB_PORT: '5432',
727
+ API_KEY: 'secret',
728
+ };
729
+ const parser = new EnvironmentParser(env);
730
+
731
+ const config = parser.create((get) => ({
732
+ database: {
733
+ host: get('DB_HOST').string(),
734
+ port: get('DB_PORT').string().transform(Number),
735
+ },
736
+ api: {
737
+ key: get('API_KEY').string(),
738
+ },
739
+ }));
740
+
741
+ const envVars = config.getEnvironmentVariables();
742
+
743
+ expect(envVars).toEqual(['API_KEY', 'DB_HOST', 'DB_PORT']);
744
+ });
745
+
746
+ it('should return sorted environment variable names', () => {
747
+ const env = {};
748
+ const parser = new EnvironmentParser(env);
749
+
750
+ const config = parser.create((get) => ({
751
+ zValue: get('Z_VALUE').string().optional(),
752
+ aValue: get('A_VALUE').string().optional(),
753
+ mValue: get('M_VALUE').string().optional(),
754
+ }));
755
+
756
+ const envVars = config.getEnvironmentVariables();
757
+
758
+ // Should be sorted alphabetically
759
+ expect(envVars).toEqual(['A_VALUE', 'M_VALUE', 'Z_VALUE']);
760
+ });
761
+
762
+ it('should deduplicate environment variable names', () => {
763
+ const env = { SHARED_VAR: 'value' };
764
+ const parser = new EnvironmentParser(env);
765
+
766
+ const config = parser.create((get) => ({
767
+ value1: get('SHARED_VAR').string(),
768
+ value2: get('SHARED_VAR').string(),
769
+ value3: get('SHARED_VAR').string(),
770
+ }));
771
+
772
+ const envVars = config.getEnvironmentVariables();
773
+
774
+ // Should only appear once despite being accessed 3 times
775
+ expect(envVars).toEqual(['SHARED_VAR']);
776
+ });
777
+
778
+ it('should track variables with default values', () => {
779
+ const env = {};
780
+ const parser = new EnvironmentParser(env);
781
+
782
+ const config = parser.create((get) => ({
783
+ port: get('PORT').string().default('3000'),
784
+ host: get('HOST').string().default('localhost'),
785
+ }));
786
+
787
+ const envVars = config.getEnvironmentVariables();
788
+
789
+ // Should track even when defaults are used
790
+ expect(envVars).toEqual(['HOST', 'PORT']);
791
+ });
792
792
 
793
- it('should work with empty configuration', () => {
794
- const env = {};
795
- const parser = new EnvironmentParser(env);
793
+ it('should work with empty configuration', () => {
794
+ const env = {};
795
+ const parser = new EnvironmentParser(env);
796
796
 
797
- const config = parser.create(() => ({}));
798
-
799
- const envVars = config.getEnvironmentVariables();
797
+ const config = parser.create(() => ({}));
798
+
799
+ const envVars = config.getEnvironmentVariables();
800
800
 
801
- expect(envVars).toEqual([]);
802
- });
803
-
804
- it('should track variables accessed through coerce', () => {
805
- const env = { NUM_WORKERS: '4', TIMEOUT: '30000' };
806
- const parser = new EnvironmentParser(env);
807
-
808
- const config = parser.create((get) => ({
809
- workers: get('NUM_WORKERS').coerce.number(),
810
- timeout: get('TIMEOUT').coerce.number(),
811
- }));
812
-
813
- const envVars = config.getEnvironmentVariables();
814
-
815
- expect(envVars).toEqual(['NUM_WORKERS', 'TIMEOUT']);
816
- });
817
-
818
- it('should track variables with complex transformations', () => {
819
- const env = {
820
- ALLOWED_ORIGINS: 'http://localhost,https://example.com',
821
- FEATURE_FLAGS: 'auth,cache',
822
- };
823
- const parser = new EnvironmentParser(env);
824
-
825
- const config = parser.create((get) => ({
826
- origins: get('ALLOWED_ORIGINS')
827
- .string()
828
- .transform((v) => v.split(',')),
829
- features: get('FEATURE_FLAGS')
830
- .string()
831
- .transform((v) => v.split(',')),
832
- }));
801
+ expect(envVars).toEqual([]);
802
+ });
803
+
804
+ it('should track variables accessed through coerce', () => {
805
+ const env = { NUM_WORKERS: '4', TIMEOUT: '30000' };
806
+ const parser = new EnvironmentParser(env);
807
+
808
+ const config = parser.create((get) => ({
809
+ workers: get('NUM_WORKERS').coerce.number(),
810
+ timeout: get('TIMEOUT').coerce.number(),
811
+ }));
812
+
813
+ const envVars = config.getEnvironmentVariables();
814
+
815
+ expect(envVars).toEqual(['NUM_WORKERS', 'TIMEOUT']);
816
+ });
817
+
818
+ it('should track variables with complex transformations', () => {
819
+ const env = {
820
+ ALLOWED_ORIGINS: 'http://localhost,https://example.com',
821
+ FEATURE_FLAGS: 'auth,cache',
822
+ };
823
+ const parser = new EnvironmentParser(env);
824
+
825
+ const config = parser.create((get) => ({
826
+ origins: get('ALLOWED_ORIGINS')
827
+ .string()
828
+ .transform((v) => v.split(',')),
829
+ features: get('FEATURE_FLAGS')
830
+ .string()
831
+ .transform((v) => v.split(',')),
832
+ }));
833
833
 
834
- const envVars = config.getEnvironmentVariables();
834
+ const envVars = config.getEnvironmentVariables();
835
835
 
836
- expect(envVars).toEqual(['ALLOWED_ORIGINS', 'FEATURE_FLAGS']);
837
- });
838
- });
836
+ expect(envVars).toEqual(['ALLOWED_ORIGINS', 'FEATURE_FLAGS']);
837
+ });
838
+ });
839
839
  });