@geekmidas/envkit 0.0.7 → 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.
- package/dist/{EnvironmentParser-C-arQEHQ.d.mts → EnvironmentParser-B8--woiB.d.cts} +40 -2
- package/dist/{EnvironmentParser-X4h2Vp4r.d.cts → EnvironmentParser-C_9v2BDw.d.mts} +40 -2
- package/dist/{EnvironmentParser-CQUOGqc0.mjs → EnvironmentParser-STvN_RCc.mjs} +46 -3
- package/dist/EnvironmentParser-STvN_RCc.mjs.map +1 -0
- package/dist/{EnvironmentParser-BDPDLv6i.cjs → EnvironmentParser-cnxuy7lw.cjs} +46 -3
- package/dist/EnvironmentParser-cnxuy7lw.cjs.map +1 -0
- package/dist/EnvironmentParser.cjs +1 -1
- package/dist/EnvironmentParser.d.cts +1 -1
- package/dist/EnvironmentParser.d.mts +1 -1
- package/dist/EnvironmentParser.mjs +1 -1
- package/dist/SnifferEnvironmentParser.cjs +140 -0
- package/dist/SnifferEnvironmentParser.cjs.map +1 -0
- package/dist/SnifferEnvironmentParser.d.cts +50 -0
- package/dist/SnifferEnvironmentParser.d.mts +50 -0
- package/dist/SnifferEnvironmentParser.mjs +139 -0
- package/dist/SnifferEnvironmentParser.mjs.map +1 -0
- package/dist/index.cjs +2 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +2 -2
- package/dist/sst.cjs +131 -4
- package/dist/sst.cjs.map +1 -0
- package/dist/sst.d.cts +2 -1
- package/dist/sst.d.mts +2 -1
- package/dist/sst.mjs +128 -2
- package/dist/sst.mjs.map +1 -0
- package/package.json +6 -1
- package/src/EnvironmentParser.ts +51 -2
- package/src/SnifferEnvironmentParser.ts +207 -0
- package/src/__tests__/EnvironmentParser.spec.ts +147 -0
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +332 -0
- package/src/index.ts +1 -1
- package/dist/__tests__/ConfigParser.spec.cjs +0 -323
- package/dist/__tests__/ConfigParser.spec.d.cts +0 -1
- package/dist/__tests__/ConfigParser.spec.d.mts +0 -1
- package/dist/__tests__/ConfigParser.spec.mjs +0 -322
- package/dist/__tests__/EnvironmentParser.spec.cjs +0 -422
- package/dist/__tests__/EnvironmentParser.spec.d.cts +0 -1
- package/dist/__tests__/EnvironmentParser.spec.d.mts +0 -1
- package/dist/__tests__/EnvironmentParser.spec.mjs +0 -421
- package/dist/__tests__/sst.spec.cjs +0 -305
- package/dist/__tests__/sst.spec.d.cts +0 -1
- package/dist/__tests__/sst.spec.d.mts +0 -1
- package/dist/__tests__/sst.spec.mjs +0 -304
- package/dist/sst-BSxwaAdz.cjs +0 -146
- package/dist/sst-CQhO0S6y.mjs +0 -128
|
@@ -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
|
});
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
import { SnifferEnvironmentParser } from '../SnifferEnvironmentParser';
|
|
4
|
+
|
|
5
|
+
describe('SnifferEnvironmentParser', () => {
|
|
6
|
+
describe('Environment variable tracking', () => {
|
|
7
|
+
it('should track accessed environment variables', () => {
|
|
8
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
9
|
+
|
|
10
|
+
sniffer.create((get) => ({
|
|
11
|
+
appName: get('APP_NAME').string(),
|
|
12
|
+
port: get('PORT').string().transform(Number),
|
|
13
|
+
}));
|
|
14
|
+
|
|
15
|
+
const envVars = sniffer.getEnvironmentVariables();
|
|
16
|
+
expect(envVars).toEqual(['APP_NAME', 'PORT']);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should track variables in nested configurations', () => {
|
|
20
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
21
|
+
|
|
22
|
+
sniffer.create((get) => ({
|
|
23
|
+
database: {
|
|
24
|
+
host: get('DB_HOST').string(),
|
|
25
|
+
port: get('DB_PORT').string().transform(Number),
|
|
26
|
+
},
|
|
27
|
+
api: {
|
|
28
|
+
key: get('API_KEY').string(),
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
const envVars = sniffer.getEnvironmentVariables();
|
|
33
|
+
expect(envVars).toEqual(['API_KEY', 'DB_HOST', 'DB_PORT']);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should return sorted environment variable names', () => {
|
|
37
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
38
|
+
|
|
39
|
+
sniffer.create((get) => ({
|
|
40
|
+
zValue: get('Z_VALUE').string(),
|
|
41
|
+
aValue: get('A_VALUE').string(),
|
|
42
|
+
mValue: get('M_VALUE').string(),
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
const envVars = sniffer.getEnvironmentVariables();
|
|
46
|
+
expect(envVars).toEqual(['A_VALUE', 'M_VALUE', 'Z_VALUE']);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should deduplicate environment variable names', () => {
|
|
50
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
51
|
+
|
|
52
|
+
sniffer.create((get) => ({
|
|
53
|
+
value1: get('SHARED_VAR').string(),
|
|
54
|
+
value2: get('SHARED_VAR').string(),
|
|
55
|
+
value3: get('SHARED_VAR').string(),
|
|
56
|
+
}));
|
|
57
|
+
|
|
58
|
+
const envVars = sniffer.getEnvironmentVariables();
|
|
59
|
+
expect(envVars).toEqual(['SHARED_VAR']);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('should track variables accessed through coerce', () => {
|
|
63
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
64
|
+
|
|
65
|
+
sniffer.create((get) => ({
|
|
66
|
+
workers: get('NUM_WORKERS').coerce.number(),
|
|
67
|
+
timeout: get('TIMEOUT').coerce.number(),
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
const envVars = sniffer.getEnvironmentVariables();
|
|
71
|
+
expect(envVars).toEqual(['NUM_WORKERS', 'TIMEOUT']);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe('Mock value parsing', () => {
|
|
76
|
+
it('should never throw when parsing - returns mock values', () => {
|
|
77
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
78
|
+
|
|
79
|
+
const config = sniffer.create((get) => ({
|
|
80
|
+
required: get('REQUIRED_VAR').string(),
|
|
81
|
+
alsoRequired: get('ALSO_REQUIRED').string(),
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
// Should not throw even though env vars are not set
|
|
85
|
+
expect(() => config.parse()).not.toThrow();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should return empty string for string schemas', () => {
|
|
89
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
90
|
+
|
|
91
|
+
const config = sniffer
|
|
92
|
+
.create((get) => ({
|
|
93
|
+
value: get('STRING_VAR').string(),
|
|
94
|
+
}))
|
|
95
|
+
.parse();
|
|
96
|
+
|
|
97
|
+
expect(config.value).toBe('');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should return 0 for number schemas', () => {
|
|
101
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
102
|
+
|
|
103
|
+
const config = sniffer
|
|
104
|
+
.create((get) => ({
|
|
105
|
+
value: get('NUMBER_VAR').coerce.number(),
|
|
106
|
+
}))
|
|
107
|
+
.parse();
|
|
108
|
+
|
|
109
|
+
expect(config.value).toBe(0);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('should return false for boolean schemas', () => {
|
|
113
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
114
|
+
|
|
115
|
+
const config = sniffer
|
|
116
|
+
.create((get) => ({
|
|
117
|
+
value: get('BOOL_VAR').coerce.boolean(),
|
|
118
|
+
}))
|
|
119
|
+
.parse();
|
|
120
|
+
|
|
121
|
+
expect(config.value).toBe(false);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should return empty array for array schemas', () => {
|
|
125
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
126
|
+
|
|
127
|
+
const config = sniffer
|
|
128
|
+
.create((get) => ({
|
|
129
|
+
value: get('ARRAY_VAR').array(z.string()),
|
|
130
|
+
}))
|
|
131
|
+
.parse();
|
|
132
|
+
|
|
133
|
+
expect(config.value).toEqual([]);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should return undefined for optional schemas', () => {
|
|
137
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
138
|
+
|
|
139
|
+
const config = sniffer
|
|
140
|
+
.create((get) => ({
|
|
141
|
+
value: get('OPTIONAL_VAR').string().optional(),
|
|
142
|
+
}))
|
|
143
|
+
.parse();
|
|
144
|
+
|
|
145
|
+
expect(config.value).toBeUndefined();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should handle nested configurations', () => {
|
|
149
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
150
|
+
|
|
151
|
+
const config = sniffer
|
|
152
|
+
.create((get) => ({
|
|
153
|
+
database: {
|
|
154
|
+
host: get('DB_HOST').string(),
|
|
155
|
+
port: get('DB_PORT').coerce.number(),
|
|
156
|
+
},
|
|
157
|
+
cache: {
|
|
158
|
+
enabled: get('CACHE_ENABLED').coerce.boolean(),
|
|
159
|
+
},
|
|
160
|
+
}))
|
|
161
|
+
.parse();
|
|
162
|
+
|
|
163
|
+
expect(config).toEqual({
|
|
164
|
+
database: {
|
|
165
|
+
host: '',
|
|
166
|
+
port: 0,
|
|
167
|
+
},
|
|
168
|
+
cache: {
|
|
169
|
+
enabled: false,
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should track variables with transforms', () => {
|
|
175
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
176
|
+
|
|
177
|
+
sniffer.create((get) => ({
|
|
178
|
+
origins: get('ALLOWED_ORIGINS')
|
|
179
|
+
.string()
|
|
180
|
+
.transform((v) => v.split(',')),
|
|
181
|
+
port: get('PORT').string().transform(Number),
|
|
182
|
+
}));
|
|
183
|
+
|
|
184
|
+
// Should track the env vars even with transforms
|
|
185
|
+
expect(sniffer.getEnvironmentVariables()).toEqual([
|
|
186
|
+
'ALLOWED_ORIGINS',
|
|
187
|
+
'PORT',
|
|
188
|
+
]);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('Service registration simulation', () => {
|
|
193
|
+
it('should allow simulated service registration to succeed', () => {
|
|
194
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
195
|
+
|
|
196
|
+
// Simulate a service that would normally fail without env vars
|
|
197
|
+
const mockService = {
|
|
198
|
+
serviceName: 'database' as const,
|
|
199
|
+
register(envParser: SnifferEnvironmentParser) {
|
|
200
|
+
const config = envParser
|
|
201
|
+
.create((get) => ({
|
|
202
|
+
url: get('DATABASE_URL').string(),
|
|
203
|
+
poolSize: get('DB_POOL_SIZE').coerce.number(),
|
|
204
|
+
}))
|
|
205
|
+
.parse();
|
|
206
|
+
|
|
207
|
+
// Service uses parsed values to create connection
|
|
208
|
+
return { url: config.url, poolSize: config.poolSize };
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
// Should not throw
|
|
213
|
+
expect(() => mockService.register(sniffer)).not.toThrow();
|
|
214
|
+
|
|
215
|
+
// Should have tracked the env vars
|
|
216
|
+
expect(sniffer.getEnvironmentVariables()).toEqual([
|
|
217
|
+
'DATABASE_URL',
|
|
218
|
+
'DB_POOL_SIZE',
|
|
219
|
+
]);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
it('should work with multiple services', () => {
|
|
223
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
224
|
+
|
|
225
|
+
const databaseService = {
|
|
226
|
+
serviceName: 'db' as const,
|
|
227
|
+
register(envParser: SnifferEnvironmentParser) {
|
|
228
|
+
return envParser
|
|
229
|
+
.create((get) => ({
|
|
230
|
+
host: get('DB_HOST').string(),
|
|
231
|
+
port: get('DB_PORT').coerce.number(),
|
|
232
|
+
}))
|
|
233
|
+
.parse();
|
|
234
|
+
},
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const cacheService = {
|
|
238
|
+
serviceName: 'cache' as const,
|
|
239
|
+
register(envParser: SnifferEnvironmentParser) {
|
|
240
|
+
return envParser
|
|
241
|
+
.create((get) => ({
|
|
242
|
+
url: get('REDIS_URL').string(),
|
|
243
|
+
ttl: get('CACHE_TTL').coerce.number(),
|
|
244
|
+
}))
|
|
245
|
+
.parse();
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
// Register both services
|
|
250
|
+
databaseService.register(sniffer);
|
|
251
|
+
cacheService.register(sniffer);
|
|
252
|
+
|
|
253
|
+
// Should have tracked all env vars from both services
|
|
254
|
+
expect(sniffer.getEnvironmentVariables()).toEqual([
|
|
255
|
+
'CACHE_TTL',
|
|
256
|
+
'DB_HOST',
|
|
257
|
+
'DB_PORT',
|
|
258
|
+
'REDIS_URL',
|
|
259
|
+
]);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
it('should handle async service registration', async () => {
|
|
263
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
264
|
+
|
|
265
|
+
const asyncService = {
|
|
266
|
+
serviceName: 'async' as const,
|
|
267
|
+
async register(envParser: SnifferEnvironmentParser) {
|
|
268
|
+
const config = envParser
|
|
269
|
+
.create((get) => ({
|
|
270
|
+
apiKey: get('API_KEY').string(),
|
|
271
|
+
endpoint: get('API_ENDPOINT').string(),
|
|
272
|
+
}))
|
|
273
|
+
.parse();
|
|
274
|
+
|
|
275
|
+
// Simulate async initialization
|
|
276
|
+
await Promise.resolve();
|
|
277
|
+
return config;
|
|
278
|
+
},
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
await expect(asyncService.register(sniffer)).resolves.not.toThrow();
|
|
282
|
+
expect(sniffer.getEnvironmentVariables()).toEqual([
|
|
283
|
+
'API_ENDPOINT',
|
|
284
|
+
'API_KEY',
|
|
285
|
+
]);
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
describe('Edge cases', () => {
|
|
290
|
+
it('should return empty array when no variables accessed', () => {
|
|
291
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
292
|
+
|
|
293
|
+
sniffer.create(() => ({}));
|
|
294
|
+
|
|
295
|
+
expect(sniffer.getEnvironmentVariables()).toEqual([]);
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('should handle deeply nested objects', () => {
|
|
299
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
300
|
+
|
|
301
|
+
sniffer.create((get) => ({
|
|
302
|
+
level1: {
|
|
303
|
+
level2: {
|
|
304
|
+
level3: {
|
|
305
|
+
value: get('DEEP_VALUE').string(),
|
|
306
|
+
},
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
}));
|
|
310
|
+
|
|
311
|
+
expect(sniffer.getEnvironmentVariables()).toEqual(['DEEP_VALUE']);
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
it('should handle multiple create calls', () => {
|
|
315
|
+
const sniffer = new SnifferEnvironmentParser();
|
|
316
|
+
|
|
317
|
+
sniffer.create((get) => ({
|
|
318
|
+
first: get('FIRST_VAR').string(),
|
|
319
|
+
}));
|
|
320
|
+
|
|
321
|
+
sniffer.create((get) => ({
|
|
322
|
+
second: get('SECOND_VAR').string(),
|
|
323
|
+
}));
|
|
324
|
+
|
|
325
|
+
// Should accumulate vars from both create calls
|
|
326
|
+
expect(sniffer.getEnvironmentVariables()).toEqual([
|
|
327
|
+
'FIRST_VAR',
|
|
328
|
+
'SECOND_VAR',
|
|
329
|
+
]);
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { EnvironmentParser } from './EnvironmentParser';
|
|
1
|
+
export { EnvironmentParser, ConfigParser } from './EnvironmentParser';
|