@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
@@ -13,8 +13,12 @@ export class ConfigParser<TResponse extends EmptyObject> {
13
13
  * Creates a new ConfigParser instance.
14
14
  *
15
15
  * @param config - The configuration object to parse
16
+ * @param envVars - Set of environment variable names that were accessed
16
17
  */
17
- constructor(private readonly config: TResponse) {}
18
+ constructor(
19
+ private readonly config: TResponse,
20
+ private readonly envVars: Set<string> = new Set(),
21
+ ) {}
18
22
  /**
19
23
  * Parses the config object and validates it against the Zod schemas
20
24
  * @returns The parsed config object
@@ -65,6 +69,26 @@ export class ConfigParser<TResponse extends EmptyObject> {
65
69
 
66
70
  return parsedConfig;
67
71
  }
72
+
73
+ /**
74
+ * Returns an array of environment variable names that were accessed during config creation.
75
+ * This is useful for deployment and configuration management to know which env vars are required.
76
+ *
77
+ * @returns Array of environment variable names, sorted alphabetically
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * const config = envParser.create((get) => ({
82
+ * dbUrl: get('DATABASE_URL').string(),
83
+ * port: get('PORT').number()
84
+ * }));
85
+ *
86
+ * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
87
+ * ```
88
+ */
89
+ getEnvironmentVariables(): string[] {
90
+ return Array.from(this.envVars).sort();
91
+ }
68
92
  }
69
93
 
70
94
  /**
@@ -87,6 +111,11 @@ export class ConfigParser<TResponse extends EmptyObject> {
87
111
  * ```
88
112
  */
89
113
  export class EnvironmentParser<T extends EmptyObject> {
114
+ /**
115
+ * Set to track which environment variable names have been accessed
116
+ */
117
+ private readonly accessedVars: Set<string> = new Set();
118
+
90
119
  /**
91
120
  * Creates a new EnvironmentParser instance.
92
121
  *
@@ -177,6 +206,9 @@ export class EnvironmentParser<T extends EmptyObject> {
177
206
  * @returns A proxied Zod object with wrapped schema creators
178
207
  */
179
208
  private getZodGetter = (name: string) => {
209
+ // Track that this environment variable was accessed
210
+ this.accessedVars.add(name);
211
+
180
212
  // Return an object that has all Zod schemas but with our wrapper
181
213
  return new Proxy(
182
214
  { ...z },
@@ -227,7 +259,24 @@ export class EnvironmentParser<T extends EmptyObject> {
227
259
  builder: (get: EnvFetcher) => TReturn,
228
260
  ): ConfigParser<TReturn> {
229
261
  const config = builder(this.getZodGetter);
230
- return new ConfigParser(config);
262
+ return new ConfigParser(config, this.accessedVars);
263
+ }
264
+
265
+ /**
266
+ * Returns an array of environment variable names that were accessed via the getter.
267
+ * This is useful for build-time analysis to determine which env vars a service needs.
268
+ *
269
+ * @returns Array of environment variable names, sorted alphabetically
270
+ *
271
+ * @example
272
+ * ```typescript
273
+ * const sniffer = new EnvironmentParser({});
274
+ * service.register(sniffer);
275
+ * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
276
+ * ```
277
+ */
278
+ getEnvironmentVariables(): string[] {
279
+ return Array.from(this.accessedVars).sort();
231
280
  }
232
281
  }
233
282
 
@@ -0,0 +1,207 @@
1
+ import { z } from 'zod/v4';
2
+ import {
3
+ ConfigParser,
4
+ type EmptyObject,
5
+ type EnvFetcher,
6
+ } from './EnvironmentParser';
7
+
8
+ /**
9
+ * A specialized EnvironmentParser for build-time analysis that tracks
10
+ * which environment variables are accessed without requiring actual values.
11
+ *
12
+ * Unlike the regular EnvironmentParser, the sniffer:
13
+ * - Always returns mock values from .parse() and .safeParse()
14
+ * - Never throws validation errors
15
+ * - Tracks all accessed environment variable names
16
+ *
17
+ * This allows service registration to succeed during build-time analysis
18
+ * even when environment variables are not set.
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * const sniffer = new SnifferEnvironmentParser();
23
+ * await service.register(sniffer); // Always succeeds
24
+ * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']
25
+ * ```
26
+ */
27
+ export class SnifferEnvironmentParser<
28
+ T extends EmptyObject = EmptyObject,
29
+ > {
30
+ private readonly accessedVars: Set<string> = new Set();
31
+
32
+ /**
33
+ * Wraps a Zod schema to always return mock values.
34
+ * This ensures .parse() and .safeParse() never fail.
35
+ */
36
+ private wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {
37
+ return new Proxy(schema, {
38
+ get: (target, prop) => {
39
+ if (prop === 'parse') {
40
+ return () => this.getMockValue(target);
41
+ }
42
+
43
+ if (prop === 'safeParse') {
44
+ return () => ({
45
+ success: true as const,
46
+ data: this.getMockValue(target),
47
+ });
48
+ }
49
+
50
+ const originalProp = target[prop as keyof typeof target];
51
+ if (typeof originalProp === 'function') {
52
+ return (...args: any[]) => {
53
+ const result = originalProp.apply(target, args);
54
+ if (result && typeof result === 'object' && 'parse' in result) {
55
+ return this.wrapSchema(result, name);
56
+ }
57
+ return result;
58
+ };
59
+ }
60
+
61
+ return originalProp;
62
+ },
63
+ });
64
+ };
65
+
66
+ /**
67
+ * Returns a mock value based on the Zod schema type.
68
+ */
69
+ private getMockValue(schema: z.ZodType): unknown {
70
+ // Return type-appropriate mock values
71
+ if (schema instanceof z.ZodString) return '';
72
+ if (schema instanceof z.ZodNumber) return 0;
73
+ if (schema instanceof z.ZodBoolean) return false;
74
+ if (schema instanceof z.ZodArray) return [];
75
+ if (schema instanceof z.ZodOptional) return undefined;
76
+ if (schema instanceof z.ZodNullable) return null;
77
+
78
+ // For object schemas, build mock object from shape
79
+ if (schema instanceof z.ZodObject && schema.shape) {
80
+ const result: Record<string, unknown> = {};
81
+ for (const [key, value] of Object.entries(schema.shape)) {
82
+ if (value instanceof z.ZodType) {
83
+ result[key] = this.getMockValue(value);
84
+ }
85
+ }
86
+ return result;
87
+ }
88
+
89
+ return '';
90
+ }
91
+
92
+ /**
93
+ * Creates a proxied Zod getter that tracks environment variable access.
94
+ */
95
+ private getZodGetter = (name: string) => {
96
+ this.accessedVars.add(name);
97
+
98
+ return new Proxy(
99
+ { ...z },
100
+ {
101
+ get: (target, prop) => {
102
+ // @ts-ignore
103
+ const value = target[prop];
104
+
105
+ if (typeof value === 'function') {
106
+ return (...args: any[]) => {
107
+ const schema = value(...args);
108
+ return this.wrapSchema(schema, name);
109
+ };
110
+ }
111
+
112
+ if (value && typeof value === 'object') {
113
+ return new Proxy(value, {
114
+ get: (nestedTarget, nestedProp) => {
115
+ const nestedValue =
116
+ nestedTarget[nestedProp as keyof typeof nestedTarget];
117
+ if (typeof nestedValue === 'function') {
118
+ return (...args: any[]) => {
119
+ const schema = nestedValue(...args);
120
+ return this.wrapSchema(schema, name);
121
+ };
122
+ }
123
+ return nestedValue;
124
+ },
125
+ });
126
+ }
127
+
128
+ return value;
129
+ },
130
+ },
131
+ );
132
+ };
133
+
134
+ /**
135
+ * Creates a ConfigParser that will return mock values when parsed.
136
+ */
137
+ create<TReturn extends EmptyObject>(
138
+ builder: (get: EnvFetcher) => TReturn,
139
+ ): ConfigParser<TReturn> {
140
+ const config = builder(this.getZodGetter);
141
+ return new SnifferConfigParser(config, this.accessedVars);
142
+ }
143
+
144
+ /**
145
+ * Returns all environment variable names that were accessed.
146
+ */
147
+ getEnvironmentVariables(): string[] {
148
+ return Array.from(this.accessedVars).sort();
149
+ }
150
+ }
151
+
152
+ /**
153
+ * A ConfigParser that always succeeds with mock values.
154
+ */
155
+ class SnifferConfigParser<TResponse extends EmptyObject> extends ConfigParser<TResponse> {
156
+ parse(): any {
157
+ return this.parseWithMocks(this.getConfig());
158
+ }
159
+
160
+ private getConfig(): TResponse {
161
+ // Access the private config via any cast
162
+ return (this as any).config;
163
+ }
164
+
165
+ private parseWithMocks<T>(config: T): any {
166
+ const result: EmptyObject = {};
167
+
168
+ if (config && typeof config !== 'object') {
169
+ return config;
170
+ }
171
+
172
+ for (const key in config) {
173
+ const schema = config[key];
174
+
175
+ if (schema instanceof z.ZodType) {
176
+ // Use safeParse which will return mock values from our wrapped schema
177
+ const parsed = schema.safeParse(undefined);
178
+ result[key] = parsed.success ? parsed.data : this.getDefaultForSchema(schema);
179
+ } else if (schema && typeof schema === 'object') {
180
+ result[key] = this.parseWithMocks(schema as EmptyObject);
181
+ }
182
+ }
183
+
184
+ return result;
185
+ }
186
+
187
+ private getDefaultForSchema(schema: z.ZodType): unknown {
188
+ if (schema instanceof z.ZodString) return '';
189
+ if (schema instanceof z.ZodNumber) return 0;
190
+ if (schema instanceof z.ZodBoolean) return false;
191
+ if (schema instanceof z.ZodArray) return [];
192
+ if (schema instanceof z.ZodOptional) return undefined;
193
+ if (schema instanceof z.ZodNullable) return null;
194
+
195
+ if (schema instanceof z.ZodObject && schema.shape) {
196
+ const result: Record<string, unknown> = {};
197
+ for (const [key, value] of Object.entries(schema.shape)) {
198
+ if (value instanceof z.ZodType) {
199
+ result[key] = this.getDefaultForSchema(value);
200
+ }
201
+ }
202
+ return result;
203
+ }
204
+
205
+ return '';
206
+ }
207
+ }
@@ -689,4 +689,151 @@ describe('EnvironmentParser', () => {
689
689
  expect(_typeCheck2).toBe(true);
690
690
  });
691
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
+
793
+ it('should work with empty configuration', () => {
794
+ const env = {};
795
+ const parser = new EnvironmentParser(env);
796
+
797
+ const config = parser.create(() => ({}));
798
+
799
+ const envVars = config.getEnvironmentVariables();
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
+ }));
833
+
834
+ const envVars = config.getEnvironmentVariables();
835
+
836
+ expect(envVars).toEqual(['ALLOWED_ORIGINS', 'FEATURE_FLAGS']);
837
+ });
838
+ });
692
839
  });