@geekmidas/envkit 0.0.1 → 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.
@@ -39,48 +39,65 @@ var EnvironmentParser = class {
39
39
  constructor(config) {
40
40
  this.config = config;
41
41
  }
42
- getZodGetter = (name) => {
43
- return new Proxy({ ...z }, { get: (target, prop) => {
44
- const func = target[prop];
45
- if (typeof func === "function") return (...args) => {
46
- const schema = func(...args);
47
- const originalParse = schema.parse;
48
- const originalSafeParse = schema.safeParse;
49
- schema.parse = () => {
50
- const value = get(this.config, name);
51
- try {
52
- return originalParse.call(schema, value);
53
- } catch (error) {
54
- if (error instanceof z.ZodError) {
55
- const modifiedIssues = error.issues.map((issue) => ({
56
- ...issue,
57
- message: `Environment variable "${name}": ${issue.message}`,
58
- path: [name, ...issue.path]
59
- }));
60
- throw new z.ZodError(modifiedIssues);
61
- }
62
- throw error;
63
- }
64
- };
65
- schema.safeParse = () => {
66
- const value = get(this.config, name);
67
- const result = originalSafeParse.call(schema, value);
68
- if (!result.success) {
69
- const modifiedIssues = result.error.issues.map((issue) => ({
42
+ wrapSchema = (schema, name) => {
43
+ return new Proxy(schema, { get: (target, prop) => {
44
+ if (prop === "parse") return () => {
45
+ const value = get(this.config, name);
46
+ try {
47
+ return target.parse(value);
48
+ } catch (error) {
49
+ if (error instanceof z.ZodError) {
50
+ const modifiedIssues = error.issues.map((issue) => ({
70
51
  ...issue,
71
52
  message: `Environment variable "${name}": ${issue.message}`,
72
53
  path: [name, ...issue.path]
73
54
  }));
74
- return {
75
- success: false,
76
- error: new z.ZodError(modifiedIssues)
77
- };
55
+ throw new z.ZodError(modifiedIssues);
78
56
  }
79
- return result;
80
- };
81
- return schema;
57
+ throw error;
58
+ }
59
+ };
60
+ if (prop === "safeParse") return () => {
61
+ const value = get(this.config, name);
62
+ const result = target.safeParse(value);
63
+ if (!result.success) {
64
+ const modifiedIssues = result.error.issues.map((issue) => ({
65
+ ...issue,
66
+ message: `Environment variable "${name}": ${issue.message}`,
67
+ path: [name, ...issue.path]
68
+ }));
69
+ return {
70
+ success: false,
71
+ error: new z.ZodError(modifiedIssues)
72
+ };
73
+ }
74
+ return result;
82
75
  };
83
- return func;
76
+ const originalProp = target[prop];
77
+ if (typeof originalProp === "function") return (...args) => {
78
+ const result = originalProp.apply(target, args);
79
+ if (result && typeof result === "object" && "parse" in result) return this.wrapSchema(result, name);
80
+ return result;
81
+ };
82
+ return originalProp;
83
+ } });
84
+ };
85
+ getZodGetter = (name) => {
86
+ return new Proxy({ ...z }, { get: (target, prop) => {
87
+ const value = target[prop];
88
+ if (typeof value === "function") return (...args) => {
89
+ const schema = value(...args);
90
+ return this.wrapSchema(schema, name);
91
+ };
92
+ if (value && typeof value === "object") return new Proxy(value, { get: (nestedTarget, nestedProp) => {
93
+ const nestedValue = nestedTarget[nestedProp];
94
+ if (typeof nestedValue === "function") return (...args) => {
95
+ const schema = nestedValue(...args);
96
+ return this.wrapSchema(schema, name);
97
+ };
98
+ return nestedValue;
99
+ } });
100
+ return value;
84
101
  } });
85
102
  };
86
103
  /**
@@ -5,6 +5,9 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __commonJS = (cb, mod) => function() {
9
+ return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
10
+ };
8
11
  var __copyProps = (to, from, except, desc) => {
9
12
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
13
  key = keys[i];
@@ -62,48 +65,65 @@ var EnvironmentParser = class {
62
65
  constructor(config) {
63
66
  this.config = config;
64
67
  }
65
- getZodGetter = (name) => {
66
- return new Proxy({ ...zod_v4.z }, { get: (target, prop) => {
67
- const func = target[prop];
68
- if (typeof func === "function") return (...args) => {
69
- const schema = func(...args);
70
- const originalParse = schema.parse;
71
- const originalSafeParse = schema.safeParse;
72
- schema.parse = () => {
73
- const value = (0, lodash_get.default)(this.config, name);
74
- try {
75
- return originalParse.call(schema, value);
76
- } catch (error) {
77
- if (error instanceof zod_v4.z.ZodError) {
78
- const modifiedIssues = error.issues.map((issue) => ({
79
- ...issue,
80
- message: `Environment variable "${name}": ${issue.message}`,
81
- path: [name, ...issue.path]
82
- }));
83
- throw new zod_v4.z.ZodError(modifiedIssues);
84
- }
85
- throw error;
86
- }
87
- };
88
- schema.safeParse = () => {
89
- const value = (0, lodash_get.default)(this.config, name);
90
- const result = originalSafeParse.call(schema, value);
91
- if (!result.success) {
92
- const modifiedIssues = result.error.issues.map((issue) => ({
68
+ wrapSchema = (schema, name) => {
69
+ return new Proxy(schema, { get: (target, prop) => {
70
+ if (prop === "parse") return () => {
71
+ const value = (0, lodash_get.default)(this.config, name);
72
+ try {
73
+ return target.parse(value);
74
+ } catch (error) {
75
+ if (error instanceof zod_v4.z.ZodError) {
76
+ const modifiedIssues = error.issues.map((issue) => ({
93
77
  ...issue,
94
78
  message: `Environment variable "${name}": ${issue.message}`,
95
79
  path: [name, ...issue.path]
96
80
  }));
97
- return {
98
- success: false,
99
- error: new zod_v4.z.ZodError(modifiedIssues)
100
- };
81
+ throw new zod_v4.z.ZodError(modifiedIssues);
101
82
  }
102
- return result;
103
- };
104
- return schema;
83
+ throw error;
84
+ }
105
85
  };
106
- return func;
86
+ if (prop === "safeParse") return () => {
87
+ const value = (0, lodash_get.default)(this.config, name);
88
+ const result = target.safeParse(value);
89
+ if (!result.success) {
90
+ const modifiedIssues = result.error.issues.map((issue) => ({
91
+ ...issue,
92
+ message: `Environment variable "${name}": ${issue.message}`,
93
+ path: [name, ...issue.path]
94
+ }));
95
+ return {
96
+ success: false,
97
+ error: new zod_v4.z.ZodError(modifiedIssues)
98
+ };
99
+ }
100
+ return result;
101
+ };
102
+ const originalProp = target[prop];
103
+ if (typeof originalProp === "function") return (...args) => {
104
+ const result = originalProp.apply(target, args);
105
+ if (result && typeof result === "object" && "parse" in result) return this.wrapSchema(result, name);
106
+ return result;
107
+ };
108
+ return originalProp;
109
+ } });
110
+ };
111
+ getZodGetter = (name) => {
112
+ return new Proxy({ ...zod_v4.z }, { get: (target, prop) => {
113
+ const value = target[prop];
114
+ if (typeof value === "function") return (...args) => {
115
+ const schema = value(...args);
116
+ return this.wrapSchema(schema, name);
117
+ };
118
+ if (value && typeof value === "object") return new Proxy(value, { get: (nestedTarget, nestedProp) => {
119
+ const nestedValue = nestedTarget[nestedProp];
120
+ if (typeof nestedValue === "function") return (...args) => {
121
+ const schema = nestedValue(...args);
122
+ return this.wrapSchema(schema, name);
123
+ };
124
+ return nestedValue;
125
+ } });
126
+ return value;
107
127
  } });
108
128
  };
109
129
  /**
@@ -130,4 +150,16 @@ Object.defineProperty(exports, 'EnvironmentParser', {
130
150
  get: function () {
131
151
  return EnvironmentParser;
132
152
  }
153
+ });
154
+ Object.defineProperty(exports, '__commonJS', {
155
+ enumerable: true,
156
+ get: function () {
157
+ return __commonJS;
158
+ }
159
+ });
160
+ Object.defineProperty(exports, '__toESM', {
161
+ enumerable: true,
162
+ get: function () {
163
+ return __toESM;
164
+ }
133
165
  });
@@ -1,4 +1,4 @@
1
- const require_EnvironmentParser = require('./EnvironmentParser-nZnZXM_Y.cjs');
1
+ const require_EnvironmentParser = require('./EnvironmentParser-Bz9IoLJs.cjs');
2
2
 
3
3
  exports.ConfigParser = require_EnvironmentParser.ConfigParser;
4
4
  exports.EnvironmentParser = require_EnvironmentParser.EnvironmentParser;
@@ -1,3 +1,3 @@
1
- import { ConfigParser, EnvironmentParser } from "./EnvironmentParser-Dd6TbwJC.mjs";
1
+ import { ConfigParser, EnvironmentParser } from "./EnvironmentParser-Boj5EFmL.mjs";
2
2
 
3
3
  export { ConfigParser, EnvironmentParser };
@@ -0,0 +1,322 @@
1
+ const require_EnvironmentParser = require('../EnvironmentParser-Bz9IoLJs.cjs');
2
+ const require_vi_bdSIJ99Y = require('../vi.bdSIJ99Y-CT9xbG7X.cjs');
3
+ const zod_v4 = require_EnvironmentParser.__toESM(require("zod/v4"));
4
+
5
+ //#region src/__tests__/ConfigParser.spec.ts
6
+ require_vi_bdSIJ99Y.describe("ConfigParser", () => {
7
+ require_vi_bdSIJ99Y.describe("Basic functionality", () => {
8
+ require_vi_bdSIJ99Y.it("should parse simple Zod schemas", () => {
9
+ const config = {
10
+ name: zod_v4.z.string().default("Test"),
11
+ age: zod_v4.z.number().default(25),
12
+ active: zod_v4.z.boolean().default(true)
13
+ };
14
+ const parser = new require_EnvironmentParser.ConfigParser(config);
15
+ const result = parser.parse();
16
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
17
+ name: "Test",
18
+ age: 25,
19
+ active: true
20
+ });
21
+ });
22
+ require_vi_bdSIJ99Y.it("should handle optional values", () => {
23
+ const config = {
24
+ required: zod_v4.z.string().default("value"),
25
+ optional: zod_v4.z.string().optional()
26
+ };
27
+ const parser = new require_EnvironmentParser.ConfigParser(config);
28
+ const result = parser.parse();
29
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
30
+ required: "value",
31
+ optional: void 0
32
+ });
33
+ });
34
+ require_vi_bdSIJ99Y.it("should validate and use provided default values", () => {
35
+ const config = {
36
+ port: zod_v4.z.number().default(3e3),
37
+ host: zod_v4.z.string().default("localhost"),
38
+ debug: zod_v4.z.boolean().default(false)
39
+ };
40
+ const parser = new require_EnvironmentParser.ConfigParser(config);
41
+ const result = parser.parse();
42
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
43
+ port: 3e3,
44
+ host: "localhost",
45
+ debug: false
46
+ });
47
+ });
48
+ });
49
+ require_vi_bdSIJ99Y.describe("Nested objects", () => {
50
+ require_vi_bdSIJ99Y.it("should parse nested configuration objects", () => {
51
+ const config = {
52
+ database: {
53
+ host: zod_v4.z.string().default("localhost"),
54
+ port: zod_v4.z.number().default(5432),
55
+ ssl: zod_v4.z.boolean().default(false)
56
+ },
57
+ api: {
58
+ key: zod_v4.z.string().default("default-key"),
59
+ timeout: zod_v4.z.number().default(5e3)
60
+ }
61
+ };
62
+ const parser = new require_EnvironmentParser.ConfigParser(config);
63
+ const result = parser.parse();
64
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
65
+ database: {
66
+ host: "localhost",
67
+ port: 5432,
68
+ ssl: false
69
+ },
70
+ api: {
71
+ key: "default-key",
72
+ timeout: 5e3
73
+ }
74
+ });
75
+ });
76
+ require_vi_bdSIJ99Y.it("should handle deeply nested objects", () => {
77
+ const config = { app: {
78
+ name: zod_v4.z.string().default("MyApp"),
79
+ version: zod_v4.z.string().default("1.0.0"),
80
+ features: {
81
+ auth: {
82
+ enabled: zod_v4.z.boolean().default(true),
83
+ provider: zod_v4.z.string().default("local")
84
+ },
85
+ cache: {
86
+ enabled: zod_v4.z.boolean().default(false),
87
+ ttl: zod_v4.z.number().default(3600)
88
+ }
89
+ }
90
+ } };
91
+ const parser = new require_EnvironmentParser.ConfigParser(config);
92
+ const result = parser.parse();
93
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({ app: {
94
+ name: "MyApp",
95
+ version: "1.0.0",
96
+ features: {
97
+ auth: {
98
+ enabled: true,
99
+ provider: "local"
100
+ },
101
+ cache: {
102
+ enabled: false,
103
+ ttl: 3600
104
+ }
105
+ }
106
+ } });
107
+ });
108
+ require_vi_bdSIJ99Y.it("should handle mixed nested and flat configuration", () => {
109
+ const config = {
110
+ appName: zod_v4.z.string().default("Test App"),
111
+ database: {
112
+ url: zod_v4.z.string().default("postgres://localhost/test"),
113
+ poolSize: zod_v4.z.number().default(10)
114
+ },
115
+ port: zod_v4.z.number().default(3e3),
116
+ features: { logging: {
117
+ level: zod_v4.z.string().default("info"),
118
+ pretty: zod_v4.z.boolean().default(true)
119
+ } }
120
+ };
121
+ const parser = new require_EnvironmentParser.ConfigParser(config);
122
+ const result = parser.parse();
123
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
124
+ appName: "Test App",
125
+ database: {
126
+ url: "postgres://localhost/test",
127
+ poolSize: 10
128
+ },
129
+ port: 3e3,
130
+ features: { logging: {
131
+ level: "info",
132
+ pretty: true
133
+ } }
134
+ });
135
+ });
136
+ });
137
+ require_vi_bdSIJ99Y.describe("Error handling", () => {
138
+ require_vi_bdSIJ99Y.it("should throw ZodError for schemas without defaults", () => {
139
+ const config = {
140
+ required: zod_v4.z.string(),
141
+ alsoRequired: zod_v4.z.number()
142
+ };
143
+ const parser = new require_EnvironmentParser.ConfigParser(config);
144
+ require_vi_bdSIJ99Y.globalExpect(() => parser.parse()).toThrow(zod_v4.z.ZodError);
145
+ });
146
+ require_vi_bdSIJ99Y.it("should collect multiple validation errors", () => {
147
+ const config = {
148
+ field1: zod_v4.z.string(),
149
+ field2: zod_v4.z.number(),
150
+ field3: zod_v4.z.boolean()
151
+ };
152
+ const parser = new require_EnvironmentParser.ConfigParser(config);
153
+ try {
154
+ parser.parse();
155
+ require_vi_bdSIJ99Y.globalExpect(true).toBe(false);
156
+ } catch (error) {
157
+ require_vi_bdSIJ99Y.globalExpect(error).toBeInstanceOf(zod_v4.z.ZodError);
158
+ const zodError = error;
159
+ require_vi_bdSIJ99Y.globalExpect(zodError.issues).toHaveLength(3);
160
+ }
161
+ });
162
+ require_vi_bdSIJ99Y.it("should include correct paths in nested validation errors", () => {
163
+ const config = {
164
+ database: {
165
+ host: zod_v4.z.string(),
166
+ port: zod_v4.z.number()
167
+ },
168
+ api: { key: zod_v4.z.string() }
169
+ };
170
+ const parser = new require_EnvironmentParser.ConfigParser(config);
171
+ try {
172
+ parser.parse();
173
+ require_vi_bdSIJ99Y.globalExpect(true).toBe(false);
174
+ } catch (error) {
175
+ require_vi_bdSIJ99Y.globalExpect(error).toBeInstanceOf(zod_v4.z.ZodError);
176
+ const zodError = error;
177
+ const paths = zodError.issues.map((err) => err.path.join("."));
178
+ require_vi_bdSIJ99Y.globalExpect(paths).toContain("database.host");
179
+ require_vi_bdSIJ99Y.globalExpect(paths).toContain("database.port");
180
+ require_vi_bdSIJ99Y.globalExpect(paths).toContain("api.key");
181
+ }
182
+ });
183
+ require_vi_bdSIJ99Y.it("should use default values that pass validation", () => {
184
+ const config = {
185
+ port: zod_v4.z.number().min(1e3).max(65535).default(3e3),
186
+ email: zod_v4.z.string().email().default("admin@example.com")
187
+ };
188
+ const parser = new require_EnvironmentParser.ConfigParser(config);
189
+ const result = parser.parse();
190
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
191
+ port: 3e3,
192
+ email: "admin@example.com"
193
+ });
194
+ });
195
+ });
196
+ require_vi_bdSIJ99Y.describe("Type safety", () => {
197
+ require_vi_bdSIJ99Y.it("should infer correct types for simple configuration", () => {
198
+ const config = {
199
+ name: zod_v4.z.string().default("test"),
200
+ count: zod_v4.z.number().default(42),
201
+ enabled: zod_v4.z.boolean().default(true)
202
+ };
203
+ const parser = new require_EnvironmentParser.ConfigParser(config);
204
+ const result = parser.parse();
205
+ const _typeCheck = true;
206
+ const _typeCheck2 = true;
207
+ require_vi_bdSIJ99Y.globalExpect(_typeCheck).toBe(true);
208
+ require_vi_bdSIJ99Y.globalExpect(_typeCheck2).toBe(true);
209
+ });
210
+ require_vi_bdSIJ99Y.it("should infer correct types for nested configuration", () => {
211
+ const config = {
212
+ database: {
213
+ host: zod_v4.z.string().default("localhost"),
214
+ port: zod_v4.z.number().default(5432)
215
+ },
216
+ features: { auth: zod_v4.z.boolean().default(true) }
217
+ };
218
+ const parser = new require_EnvironmentParser.ConfigParser(config);
219
+ const result = parser.parse();
220
+ const _typeCheck = true;
221
+ const _typeCheck2 = true;
222
+ require_vi_bdSIJ99Y.globalExpect(_typeCheck).toBe(true);
223
+ require_vi_bdSIJ99Y.globalExpect(_typeCheck2).toBe(true);
224
+ });
225
+ require_vi_bdSIJ99Y.it("should handle optional types correctly", () => {
226
+ const config = {
227
+ required: zod_v4.z.string().default("value"),
228
+ optional: zod_v4.z.string().optional(),
229
+ nullable: zod_v4.z.string().nullable().default(null)
230
+ };
231
+ const parser = new require_EnvironmentParser.ConfigParser(config);
232
+ const result = parser.parse();
233
+ });
234
+ });
235
+ require_vi_bdSIJ99Y.describe("Complex schemas", () => {
236
+ require_vi_bdSIJ99Y.it("should handle enum schemas", () => {
237
+ const config = {
238
+ environment: zod_v4.z.enum([
239
+ "development",
240
+ "staging",
241
+ "production"
242
+ ]).default("development"),
243
+ logLevel: zod_v4.z.enum([
244
+ "debug",
245
+ "info",
246
+ "warn",
247
+ "error"
248
+ ]).default("info")
249
+ };
250
+ const parser = new require_EnvironmentParser.ConfigParser(config);
251
+ const result = parser.parse();
252
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
253
+ environment: "development",
254
+ logLevel: "info"
255
+ });
256
+ });
257
+ require_vi_bdSIJ99Y.it("should handle union schemas", () => {
258
+ const config = {
259
+ port: zod_v4.z.union([zod_v4.z.string(), zod_v4.z.number()]).default(3e3),
260
+ timeout: zod_v4.z.union([zod_v4.z.number(), zod_v4.z.null()]).default(null)
261
+ };
262
+ const parser = new require_EnvironmentParser.ConfigParser(config);
263
+ const result = parser.parse();
264
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
265
+ port: 3e3,
266
+ timeout: null
267
+ });
268
+ });
269
+ require_vi_bdSIJ99Y.it("should handle array schemas", () => {
270
+ const config = {
271
+ tags: zod_v4.z.array(zod_v4.z.string()).default(["tag1", "tag2"]),
272
+ ports: zod_v4.z.array(zod_v4.z.number()).default([3e3, 3001])
273
+ };
274
+ const parser = new require_EnvironmentParser.ConfigParser(config);
275
+ const result = parser.parse();
276
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
277
+ tags: ["tag1", "tag2"],
278
+ ports: [3e3, 3001]
279
+ });
280
+ });
281
+ require_vi_bdSIJ99Y.it("should handle record schemas", () => {
282
+ const config = {
283
+ metadata: zod_v4.z.record(zod_v4.z.string(), zod_v4.z.string()).default({
284
+ key1: "value1",
285
+ key2: "value2"
286
+ }),
287
+ counters: zod_v4.z.record(zod_v4.z.string(), zod_v4.z.number()).default({
288
+ count1: 1,
289
+ count2: 2
290
+ })
291
+ };
292
+ const parser = new require_EnvironmentParser.ConfigParser(config);
293
+ const result = parser.parse();
294
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
295
+ metadata: {
296
+ key1: "value1",
297
+ key2: "value2"
298
+ },
299
+ counters: {
300
+ count1: 1,
301
+ count2: 2
302
+ }
303
+ });
304
+ });
305
+ require_vi_bdSIJ99Y.it("should handle transformed schemas", () => {
306
+ const config = {
307
+ portString: zod_v4.z.string().transform(Number).default(8080),
308
+ booleanString: zod_v4.z.string().transform((v) => v === "true").default(false),
309
+ jsonString: zod_v4.z.string().transform((v) => JSON.parse(v)).default({ key: "value" })
310
+ };
311
+ const parser = new require_EnvironmentParser.ConfigParser(config);
312
+ const result = parser.parse();
313
+ require_vi_bdSIJ99Y.globalExpect(result).toEqual({
314
+ portString: 8080,
315
+ booleanString: false,
316
+ jsonString: { key: "value" }
317
+ });
318
+ });
319
+ });
320
+ });
321
+
322
+ //#endregion