@geekmidas/envkit 0.0.6 → 0.0.8

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 (47) hide show
  1. package/dist/{EnvironmentParser-C-arQEHQ.d.mts → EnvironmentParser-B8--woiB.d.cts} +40 -2
  2. package/dist/{EnvironmentParser-X4h2Vp4r.d.cts → EnvironmentParser-C_9v2BDw.d.mts} +40 -2
  3. package/dist/{EnvironmentParser-CQUOGqc0.mjs → EnvironmentParser-STvN_RCc.mjs} +46 -3
  4. package/dist/EnvironmentParser-STvN_RCc.mjs.map +1 -0
  5. package/dist/{EnvironmentParser-BDPDLv6i.cjs → EnvironmentParser-cnxuy7lw.cjs} +46 -3
  6. package/dist/EnvironmentParser-cnxuy7lw.cjs.map +1 -0
  7. package/dist/EnvironmentParser.cjs +1 -1
  8. package/dist/EnvironmentParser.d.cts +1 -1
  9. package/dist/EnvironmentParser.d.mts +1 -1
  10. package/dist/EnvironmentParser.mjs +1 -1
  11. package/dist/SnifferEnvironmentParser.cjs +140 -0
  12. package/dist/SnifferEnvironmentParser.cjs.map +1 -0
  13. package/dist/SnifferEnvironmentParser.d.cts +50 -0
  14. package/dist/SnifferEnvironmentParser.d.mts +50 -0
  15. package/dist/SnifferEnvironmentParser.mjs +139 -0
  16. package/dist/SnifferEnvironmentParser.mjs.map +1 -0
  17. package/dist/index.cjs +2 -1
  18. package/dist/index.d.cts +2 -2
  19. package/dist/index.d.mts +2 -2
  20. package/dist/index.mjs +2 -2
  21. package/dist/sst.cjs +131 -4
  22. package/dist/sst.cjs.map +1 -0
  23. package/dist/sst.d.cts +2 -1
  24. package/dist/sst.d.mts +2 -1
  25. package/dist/sst.mjs +128 -2
  26. package/dist/sst.mjs.map +1 -0
  27. package/package.json +9 -2
  28. package/src/EnvironmentParser.ts +51 -2
  29. package/src/SnifferEnvironmentParser.ts +207 -0
  30. package/src/__tests__/EnvironmentParser.spec.ts +147 -0
  31. package/src/__tests__/SnifferEnvironmentParser.spec.ts +332 -0
  32. package/src/__tests__/sst.spec.ts +9 -6
  33. package/src/index.ts +1 -1
  34. package/dist/__tests__/ConfigParser.spec.cjs +0 -323
  35. package/dist/__tests__/ConfigParser.spec.d.cts +0 -1
  36. package/dist/__tests__/ConfigParser.spec.d.mts +0 -1
  37. package/dist/__tests__/ConfigParser.spec.mjs +0 -322
  38. package/dist/__tests__/EnvironmentParser.spec.cjs +0 -422
  39. package/dist/__tests__/EnvironmentParser.spec.d.cts +0 -1
  40. package/dist/__tests__/EnvironmentParser.spec.d.mts +0 -1
  41. package/dist/__tests__/EnvironmentParser.spec.mjs +0 -421
  42. package/dist/__tests__/sst.spec.cjs +0 -305
  43. package/dist/__tests__/sst.spec.d.cts +0 -1
  44. package/dist/__tests__/sst.spec.d.mts +0 -1
  45. package/dist/__tests__/sst.spec.mjs +0 -304
  46. package/dist/sst-BSxwaAdz.cjs +0 -146
  47. package/dist/sst-CQhO0S6y.mjs +0 -128
@@ -1,421 +0,0 @@
1
- import { EnvironmentParser } from "../EnvironmentParser-CQUOGqc0.mjs";
2
- import { z } from "zod/v4";
3
- import { describe, expect, it } from "vitest";
4
-
5
- //#region src/__tests__/EnvironmentParser.spec.ts
6
- describe("EnvironmentParser", () => {
7
- describe("Basic parsing functionality", () => {
8
- it("should parse simple string values", () => {
9
- const env = { APP_NAME: "Test App" };
10
- const parser = new EnvironmentParser(env);
11
- const config = parser.create((get) => ({ appName: get("APP_NAME").string() })).parse();
12
- expect(config).toEqual({ appName: "Test App" });
13
- });
14
- it("should parse with default values when env var is missing", () => {
15
- const env = {};
16
- const parser = new EnvironmentParser(env);
17
- const config = parser.create((get) => ({
18
- appName: get("APP_NAME").string().default("Default App"),
19
- port: get("PORT").string().transform(Number).default(3e3)
20
- })).parse();
21
- expect(config).toEqual({
22
- appName: "Default App",
23
- port: 3e3
24
- });
25
- });
26
- it("should transform string to number", () => {
27
- const env = { PORT: "8080" };
28
- const parser = new EnvironmentParser(env);
29
- const config = parser.create((get) => ({ port: get("PORT").string().transform(Number) })).parse();
30
- expect(config).toEqual({ port: 8080 });
31
- });
32
- it("should transform string to boolean", () => {
33
- const env = {
34
- FEATURE_ENABLED: "true",
35
- FEATURE_DISABLED: "false",
36
- FEATURE_TRUTHY: "yes"
37
- };
38
- const parser = new EnvironmentParser(env);
39
- const config = parser.create((get) => ({
40
- enabled: get("FEATURE_ENABLED").string().transform((v) => v === "true"),
41
- disabled: get("FEATURE_DISABLED").string().transform((v) => v === "true"),
42
- truthy: get("FEATURE_TRUTHY").string().transform((v) => v === "true")
43
- })).parse();
44
- expect(config).toEqual({
45
- enabled: true,
46
- disabled: false,
47
- truthy: false
48
- });
49
- });
50
- it("should handle optional values", () => {
51
- const env = { REQUIRED: "value" };
52
- const parser = new EnvironmentParser(env);
53
- const config = parser.create((get) => ({
54
- required: get("REQUIRED").string(),
55
- optional: get("OPTIONAL").string().optional()
56
- })).parse();
57
- expect(config).toEqual({
58
- required: "value",
59
- optional: void 0
60
- });
61
- });
62
- it("should validate URLs", () => {
63
- const env = {
64
- VALID_URL: "https://example.com",
65
- DATABASE_URL: "postgresql://user:pass@localhost:5432/db"
66
- };
67
- const parser = new EnvironmentParser(env);
68
- const config = parser.create((get) => ({
69
- apiUrl: get("VALID_URL").string().url(),
70
- dbUrl: get("DATABASE_URL").string().url()
71
- })).parse();
72
- expect(config).toEqual({
73
- apiUrl: "https://example.com",
74
- dbUrl: "postgresql://user:pass@localhost:5432/db"
75
- });
76
- });
77
- it("should validate email addresses", () => {
78
- const env = { ADMIN_EMAIL: "admin@example.com" };
79
- const parser = new EnvironmentParser(env);
80
- const config = parser.create((get) => ({ adminEmail: get("ADMIN_EMAIL").string().email() })).parse();
81
- expect(config).toEqual({ adminEmail: "admin@example.com" });
82
- });
83
- it("should validate enums", () => {
84
- const env = { NODE_ENV: "production" };
85
- const parser = new EnvironmentParser(env);
86
- const config = parser.create((get) => ({ env: get("NODE_ENV").enum([
87
- "development",
88
- "staging",
89
- "production"
90
- ]) })).parse();
91
- expect(config).toEqual({ env: "production" });
92
- });
93
- });
94
- describe("Nested configuration", () => {
95
- it("should handle nested objects", () => {
96
- const env = {
97
- DB_HOST: "localhost",
98
- DB_PORT: "5432",
99
- DB_NAME: "myapp",
100
- API_KEY: "secret123",
101
- API_URL: "https://api.example.com"
102
- };
103
- const parser = new EnvironmentParser(env);
104
- const config = parser.create((get) => ({
105
- database: {
106
- host: get("DB_HOST").string(),
107
- port: get("DB_PORT").string().transform(Number),
108
- name: get("DB_NAME").string()
109
- },
110
- api: {
111
- key: get("API_KEY").string(),
112
- url: get("API_URL").string().url()
113
- }
114
- })).parse();
115
- expect(config).toEqual({
116
- database: {
117
- host: "localhost",
118
- port: 5432,
119
- name: "myapp"
120
- },
121
- api: {
122
- key: "secret123",
123
- url: "https://api.example.com"
124
- }
125
- });
126
- });
127
- it("should handle deeply nested objects", () => {
128
- const env = {
129
- FEATURE_AUTH_ENABLED: "true",
130
- FEATURE_AUTH_PROVIDER: "oauth",
131
- FEATURE_CACHE_ENABLED: "false",
132
- FEATURE_CACHE_TTL: "3600"
133
- };
134
- const parser = new EnvironmentParser(env);
135
- const config = parser.create((get) => ({ features: {
136
- authentication: {
137
- enabled: get("FEATURE_AUTH_ENABLED").string().transform((v) => v === "true"),
138
- provider: get("FEATURE_AUTH_PROVIDER").string()
139
- },
140
- cache: {
141
- enabled: get("FEATURE_CACHE_ENABLED").string().transform((v) => v === "true"),
142
- ttl: get("FEATURE_CACHE_TTL").string().transform(Number)
143
- }
144
- } })).parse();
145
- expect(config).toEqual({ features: {
146
- authentication: {
147
- enabled: true,
148
- provider: "oauth"
149
- },
150
- cache: {
151
- enabled: false,
152
- ttl: 3600
153
- }
154
- } });
155
- });
156
- it("should handle mixed nested objects with defaults", () => {
157
- const env = {
158
- DB_HOST: "custom-host",
159
- REDIS_TTL: "7200"
160
- };
161
- const parser = new EnvironmentParser(env);
162
- const config = parser.create((get) => ({
163
- database: {
164
- host: get("DB_HOST").string(),
165
- port: get("DB_PORT").string().transform(Number).default(5432),
166
- ssl: get("DB_SSL").string().transform((v) => v === "true").default(false)
167
- },
168
- cache: { redis: {
169
- host: get("REDIS_HOST").string().default("localhost"),
170
- port: get("REDIS_PORT").string().transform(Number).default(6379),
171
- ttl: get("REDIS_TTL").string().transform(Number)
172
- } }
173
- })).parse();
174
- expect(config).toEqual({
175
- database: {
176
- host: "custom-host",
177
- port: 5432,
178
- ssl: false
179
- },
180
- cache: { redis: {
181
- host: "localhost",
182
- port: 6379,
183
- ttl: 7200
184
- } }
185
- });
186
- });
187
- });
188
- describe("Error handling and validation", () => {
189
- it("should throw ZodError for missing required values", () => {
190
- const env = {};
191
- const parser = new EnvironmentParser(env);
192
- expect(() => {
193
- parser.create((get) => ({ required: get("REQUIRED_VAR").string() })).parse();
194
- }).toThrow(z.ZodError);
195
- });
196
- it("should throw ZodError with descriptive error messages", () => {
197
- const env = {};
198
- const parser = new EnvironmentParser(env);
199
- try {
200
- parser.create((get) => ({
201
- apiKey: get("API_KEY").string().min(32),
202
- dbUrl: get("DATABASE_URL").string().url()
203
- })).parse();
204
- } catch (error) {
205
- expect(error).toBeInstanceOf(z.ZodError);
206
- const zodError = error;
207
- expect(zodError.issues).toHaveLength(2);
208
- expect(zodError.issues[0].message).toContain("Environment variable \"API_KEY\"");
209
- expect(zodError.issues[1].message).toContain("Environment variable \"DATABASE_URL\"");
210
- }
211
- });
212
- it("should validate minimum string length", () => {
213
- const env = { API_KEY: "short" };
214
- const parser = new EnvironmentParser(env);
215
- expect(() => {
216
- parser.create((get) => ({ apiKey: get("API_KEY").string().min(32) })).parse();
217
- }).toThrow(z.ZodError);
218
- });
219
- it("should validate maximum string length", () => {
220
- const env = { SHORT_TEXT: "a".repeat(100) };
221
- const parser = new EnvironmentParser(env);
222
- expect(() => {
223
- parser.create((get) => ({ shortText: get("SHORT_TEXT").string().max(50) })).parse();
224
- }).toThrow(z.ZodError);
225
- });
226
- it("should validate exact string length", () => {
227
- const env = {
228
- VALID_KEY: "a".repeat(32),
229
- INVALID_KEY: "short"
230
- };
231
- const parser = new EnvironmentParser(env);
232
- const validConfig = parser.create((get) => ({ key: get("VALID_KEY").string().length(32) })).parse();
233
- expect(validConfig).toEqual({ key: "a".repeat(32) });
234
- expect(() => {
235
- parser.create((get) => ({ key: get("INVALID_KEY").string().length(32) })).parse();
236
- }).toThrow(z.ZodError);
237
- });
238
- it("should validate invalid URLs", () => {
239
- const env = { INVALID_URL: "not-a-url" };
240
- const parser = new EnvironmentParser(env);
241
- expect(() => {
242
- parser.create((get) => ({ url: get("INVALID_URL").string().url() })).parse();
243
- }).toThrow(z.ZodError);
244
- });
245
- it("should validate invalid email addresses", () => {
246
- const env = { INVALID_EMAIL: "not-an-email" };
247
- const parser = new EnvironmentParser(env);
248
- expect(() => {
249
- parser.create((get) => ({ email: get("INVALID_EMAIL").string().email() })).parse();
250
- }).toThrow(z.ZodError);
251
- });
252
- it("should validate invalid enum values", () => {
253
- const env = { NODE_ENV: "invalid" };
254
- const parser = new EnvironmentParser(env);
255
- expect(() => {
256
- parser.create((get) => ({ env: get("NODE_ENV").enum([
257
- "development",
258
- "staging",
259
- "production"
260
- ]) })).parse();
261
- }).toThrow(z.ZodError);
262
- });
263
- it("should validate number ranges", () => {
264
- const env = {
265
- VALID_PORT: "8080",
266
- INVALID_PORT_LOW: "0",
267
- INVALID_PORT_HIGH: "70000"
268
- };
269
- const parser = new EnvironmentParser(env);
270
- const validConfig = parser.create((get) => ({ port: get("VALID_PORT").coerce.number().min(1).max(65535) })).parse();
271
- expect(validConfig).toEqual({ port: 8080 });
272
- expect(() => {
273
- parser.create((get) => ({ port: get("INVALID_PORT_LOW").coerce.number().min(1).max(65535) })).parse();
274
- }).toThrow(z.ZodError);
275
- expect(() => {
276
- parser.create((get) => ({ port: get("INVALID_PORT_HIGH").coerce.number().min(1).max(65535) })).parse();
277
- }).toThrow(z.ZodError);
278
- });
279
- it("should handle transformation errors", () => {
280
- const env = { INVALID_NUMBER: "not-a-number" };
281
- const parser = new EnvironmentParser(env);
282
- expect(() => {
283
- parser.create((get) => ({ number: get("INVALID_NUMBER").string().transform((v) => {
284
- const num = Number(v);
285
- if (isNaN(num)) throw new Error("Invalid number");
286
- return num;
287
- }) })).parse();
288
- }).toThrow();
289
- });
290
- });
291
- describe("Complex scenarios", () => {
292
- it("should handle array transformations", () => {
293
- const env = {
294
- ALLOWED_ORIGINS: "http://localhost:3000,https://example.com,https://app.example.com",
295
- FEATURE_FLAGS: "auth,cache,notifications"
296
- };
297
- const parser = new EnvironmentParser(env);
298
- const config = parser.create((get) => ({
299
- cors: { origins: get("ALLOWED_ORIGINS").string().transform((origins) => origins.split(",").map((o) => o.trim())) },
300
- features: get("FEATURE_FLAGS").string().transform((flags) => flags.split(",").map((f) => f.trim()))
301
- })).parse();
302
- expect(config).toEqual({
303
- cors: { origins: [
304
- "http://localhost:3000",
305
- "https://example.com",
306
- "https://app.example.com"
307
- ] },
308
- features: [
309
- "auth",
310
- "cache",
311
- "notifications"
312
- ]
313
- });
314
- });
315
- it("should handle JSON parsing", () => {
316
- const env = {
317
- FEATURE_CONFIG: "{\"auth\":true,\"cache\":false,\"debug\":true}",
318
- RATE_LIMITS: "{\"requests\":100,\"window\":60000}"
319
- };
320
- const parser = new EnvironmentParser(env);
321
- const config = parser.create((get) => ({
322
- features: get("FEATURE_CONFIG").string().transform((str) => JSON.parse(str)).pipe(z.record(z.string(), z.boolean())),
323
- rateLimits: get("RATE_LIMITS").string().transform((str) => JSON.parse(str)).pipe(z.object({
324
- requests: z.number(),
325
- window: z.number()
326
- }))
327
- })).parse();
328
- expect(config).toEqual({
329
- features: {
330
- auth: true,
331
- cache: false,
332
- debug: true
333
- },
334
- rateLimits: {
335
- requests: 100,
336
- window: 6e4
337
- }
338
- });
339
- });
340
- it("should handle custom refinements", () => {
341
- const env = {
342
- CORS_ORIGINS: "https://example.com,https://app.example.com",
343
- API_KEYS: "key1234567890123456789012345678901,key2345678901234567890123456789012"
344
- };
345
- const parser = new EnvironmentParser(env);
346
- const config = parser.create((get) => ({
347
- cors: { origins: get("CORS_ORIGINS").string().transform((origins) => origins.split(",").map((o) => o.trim())).refine((origins) => origins.every((o) => o.startsWith("https://")), { message: "All CORS origins must use HTTPS" }) },
348
- apiKeys: get("API_KEYS").string().transform((keys) => keys.split(",").map((k) => k.trim())).pipe(z.array(z.string().min(32)))
349
- })).parse();
350
- expect(config).toEqual({
351
- cors: { origins: ["https://example.com", "https://app.example.com"] },
352
- apiKeys: ["key1234567890123456789012345678901", "key2345678901234567890123456789012"]
353
- });
354
- });
355
- it("should fail refinements with descriptive errors", () => {
356
- const env = { CORS_ORIGINS: "http://example.com,https://app.example.com" };
357
- const parser = new EnvironmentParser(env);
358
- expect(() => {
359
- parser.create((get) => ({ cors: { origins: get("CORS_ORIGINS").string().transform((origins) => origins.split(",").map((o) => o.trim())).refine((origins) => origins.every((o) => o.startsWith("https://")), { message: "All CORS origins must use HTTPS" }) } })).parse();
360
- }).toThrow(z.ZodError);
361
- });
362
- });
363
- describe("Type inference", () => {
364
- it("should correctly infer string types", () => {
365
- const env = { APP_NAME: "Test App" };
366
- const parser = new EnvironmentParser(env);
367
- const config = parser.create((get) => ({ appName: get("APP_NAME").string() })).parse();
368
- const _typeCheck = true;
369
- const _typeCheck2 = true;
370
- expect(_typeCheck).toBe(true);
371
- expect(_typeCheck2).toBe(true);
372
- });
373
- it("should correctly infer number types after transformation", () => {
374
- const env = { PORT: "3000" };
375
- const parser = new EnvironmentParser(env);
376
- const config = parser.create((get) => ({ port: get("PORT").string().transform(Number) })).parse();
377
- const _typeCheck = true;
378
- const _typeCheck2 = true;
379
- expect(_typeCheck).toBe(true);
380
- expect(_typeCheck2).toBe(true);
381
- });
382
- it("should correctly infer boolean types after transformation", () => {
383
- const env = { FEATURE_ENABLED: "true" };
384
- const parser = new EnvironmentParser(env);
385
- const config = parser.create((get) => ({ enabled: get("FEATURE_ENABLED").string().transform((v) => v === "true") })).parse();
386
- const _typeCheck = true;
387
- const _typeCheck2 = true;
388
- expect(_typeCheck).toBe(true);
389
- expect(_typeCheck2).toBe(true);
390
- });
391
- it("should correctly infer optional types", () => {
392
- const env = { REQUIRED: "value" };
393
- const parser = new EnvironmentParser(env);
394
- const config = parser.create((get) => ({
395
- required: get("REQUIRED").string(),
396
- optional: get("OPTIONAL").string().optional()
397
- })).parse();
398
- });
399
- it("should correctly infer nested object types", () => {
400
- const env = {
401
- DB_HOST: "localhost",
402
- DB_PORT: "5432",
403
- API_KEY: "secret"
404
- };
405
- const parser = new EnvironmentParser(env);
406
- const config = parser.create((get) => ({
407
- database: {
408
- host: get("DB_HOST").string(),
409
- port: get("DB_PORT").string().transform(Number)
410
- },
411
- api: { key: get("API_KEY").string() }
412
- })).parse();
413
- const _typeCheck = true;
414
- const _typeCheck2 = true;
415
- expect(_typeCheck).toBe(true);
416
- expect(_typeCheck2).toBe(true);
417
- });
418
- });
419
- });
420
-
421
- //#endregion