@geekmidas/envkit 0.0.2 → 0.0.3
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/dist/{EnvironmentParser-Dd6TbwJC.mjs → EnvironmentParser-Boj5EFmL.mjs} +53 -36
- package/dist/{EnvironmentParser-nZnZXM_Y.cjs → EnvironmentParser-Bz9IoLJs.cjs} +68 -36
- package/dist/EnvironmentParser.cjs +1 -1
- package/dist/EnvironmentParser.mjs +1 -1
- package/dist/__tests__/ConfigParser.spec.cjs +322 -0
- package/dist/__tests__/ConfigParser.spec.mjs +322 -0
- package/dist/__tests__/EnvironmentParser.spec.cjs +421 -0
- package/dist/__tests__/EnvironmentParser.spec.mjs +421 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/magic-string.es-BrLHy2bw.cjs +1015 -0
- package/dist/magic-string.es-u4lFXMgd.mjs +1014 -0
- package/dist/vi.bdSIJ99Y-CT9xbG7X.cjs +14895 -0
- package/dist/vi.bdSIJ99Y-CkiV-M6Y.mjs +14903 -0
- package/package.json +1 -1
- package/src/EnvironmentParser.ts +87 -47
- package/src/__tests__/ConfigParser.spec.ts +394 -0
- package/src/__tests__/EnvironmentParser.spec.ts +692 -0
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
import { EnvironmentParser } from '../EnvironmentParser';
|
|
4
|
+
|
|
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
|
+
});
|