@geekmidas/envkit 9.0.2 → 10.0.0-alpha.1

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 (50) hide show
  1. package/README.md +2 -2
  2. package/dist/{EnvironmentBuilder-DDgLJAAo.cjs → EnvironmentBuilder-15SdFJdK.cjs} +2 -2
  3. package/dist/EnvironmentBuilder-15SdFJdK.cjs.map +1 -0
  4. package/dist/{EnvironmentBuilder-CFen3oIg.mjs → EnvironmentBuilder-C-2fViCT.mjs} +2 -2
  5. package/dist/EnvironmentBuilder-C-2fViCT.mjs.map +1 -0
  6. package/dist/{EnvironmentBuilder-DHfDXJUm.d.mts → EnvironmentBuilder-CoBQ9Xp2.d.mts} +2 -2
  7. package/dist/{EnvironmentBuilder-CNgcdzSR.d.cts.map → EnvironmentBuilder-CoBQ9Xp2.d.mts.map} +1 -1
  8. package/dist/{EnvironmentBuilder-CNgcdzSR.d.cts → EnvironmentBuilder-DeIle4QN.d.cts} +2 -2
  9. package/dist/{EnvironmentBuilder-DHfDXJUm.d.mts.map → EnvironmentBuilder-DeIle4QN.d.cts.map} +1 -1
  10. package/dist/index.cjs +1 -1
  11. package/dist/index.d.cts +1 -1
  12. package/dist/index.d.mts +1 -1
  13. package/dist/index.mjs +1 -1
  14. package/dist/sst.cjs +13 -6
  15. package/dist/sst.cjs.map +1 -1
  16. package/dist/sst.d.cts +15 -4
  17. package/dist/sst.d.cts.map +1 -1
  18. package/dist/sst.d.mts +15 -4
  19. package/dist/sst.d.mts.map +1 -1
  20. package/dist/sst.mjs +13 -6
  21. package/dist/sst.mjs.map +1 -1
  22. package/package.json +6 -2
  23. package/CHANGELOG.md +0 -115
  24. package/dist/EnvironmentBuilder-CFen3oIg.mjs.map +0 -1
  25. package/dist/EnvironmentBuilder-DDgLJAAo.cjs.map +0 -1
  26. package/docs/api-reference.md +0 -302
  27. package/docs/async-secrets-design.md +0 -355
  28. package/examples/basic-usage.ts +0 -386
  29. package/src/EnvironmentBuilder.ts +0 -192
  30. package/src/EnvironmentParser.ts +0 -330
  31. package/src/SnifferEnvironmentParser.ts +0 -334
  32. package/src/SstEnvValidator.ts +0 -369
  33. package/src/SstEnvironmentBuilder.ts +0 -343
  34. package/src/__tests__/ConfigParser.spec.ts +0 -394
  35. package/src/__tests__/EnvironmentBuilder.spec.ts +0 -254
  36. package/src/__tests__/EnvironmentParser.spec.ts +0 -839
  37. package/src/__tests__/SnifferEnvironmentParser.spec.ts +0 -644
  38. package/src/__tests__/SstEnvValidator.spec.ts +0 -236
  39. package/src/__tests__/SstEnvironmentBuilder.spec.ts +0 -397
  40. package/src/__tests__/credentials.integration.spec.ts +0 -239
  41. package/src/__tests__/credentials.spec.ts +0 -136
  42. package/src/__tests__/formatter.spec.ts +0 -268
  43. package/src/__tests__/sst.spec.ts +0 -437
  44. package/src/credentials.ts +0 -112
  45. package/src/formatter.ts +0 -146
  46. package/src/index.ts +0 -24
  47. package/src/sst.ts +0 -76
  48. package/sst-env.d.ts +0 -8
  49. package/tsconfig.json +0 -9
  50. package/tsdown.config.ts +0 -18
@@ -1,394 +0,0 @@
1
- import { describe, expect, it } from 'vitest';
2
- import { z } from 'zod/v4';
3
- import { ConfigParser } from '../EnvironmentParser';
4
-
5
- describe('ConfigParser', () => {
6
- describe('Basic functionality', () => {
7
- it('should parse simple Zod schemas', () => {
8
- const config = {
9
- name: z.string().default('Test'),
10
- age: z.number().default(25),
11
- active: z.boolean().default(true),
12
- };
13
-
14
- const parser = new ConfigParser(config);
15
- const result = parser.parse();
16
-
17
- expect(result).toEqual({
18
- name: 'Test',
19
- age: 25,
20
- active: true,
21
- });
22
- });
23
-
24
- it('should handle optional values', () => {
25
- const config = {
26
- required: z.string().default('value'),
27
- optional: z.string().optional(),
28
- };
29
-
30
- const parser = new ConfigParser(config);
31
- const result = parser.parse();
32
-
33
- expect(result).toEqual({
34
- required: 'value',
35
- optional: undefined,
36
- });
37
- });
38
-
39
- it('should validate and use provided default values', () => {
40
- const config = {
41
- port: z.number().default(3000),
42
- host: z.string().default('localhost'),
43
- debug: z.boolean().default(false),
44
- };
45
-
46
- const parser = new ConfigParser(config);
47
- const result = parser.parse();
48
-
49
- expect(result).toEqual({
50
- port: 3000,
51
- host: 'localhost',
52
- debug: false,
53
- });
54
- });
55
- });
56
-
57
- describe('Nested objects', () => {
58
- it('should parse nested configuration objects', () => {
59
- const config = {
60
- database: {
61
- host: z.string().default('localhost'),
62
- port: z.number().default(5432),
63
- ssl: z.boolean().default(false),
64
- },
65
- api: {
66
- key: z.string().default('default-key'),
67
- timeout: z.number().default(5000),
68
- },
69
- };
70
-
71
- const parser = new ConfigParser(config);
72
- const result = parser.parse();
73
-
74
- expect(result).toEqual({
75
- database: {
76
- host: 'localhost',
77
- port: 5432,
78
- ssl: false,
79
- },
80
- api: {
81
- key: 'default-key',
82
- timeout: 5000,
83
- },
84
- });
85
- });
86
-
87
- it('should handle deeply nested objects', () => {
88
- const config = {
89
- app: {
90
- name: z.string().default('MyApp'),
91
- version: z.string().default('1.0.0'),
92
- features: {
93
- auth: {
94
- enabled: z.boolean().default(true),
95
- provider: z.string().default('local'),
96
- },
97
- cache: {
98
- enabled: z.boolean().default(false),
99
- ttl: z.number().default(3600),
100
- },
101
- },
102
- },
103
- };
104
-
105
- const parser = new ConfigParser(config);
106
- const result = parser.parse();
107
-
108
- expect(result).toEqual({
109
- app: {
110
- name: 'MyApp',
111
- version: '1.0.0',
112
- features: {
113
- auth: {
114
- enabled: true,
115
- provider: 'local',
116
- },
117
- cache: {
118
- enabled: false,
119
- ttl: 3600,
120
- },
121
- },
122
- },
123
- });
124
- });
125
-
126
- it('should handle mixed nested and flat configuration', () => {
127
- const config = {
128
- appName: z.string().default('Test App'),
129
- database: {
130
- url: z.string().default('postgres://localhost/test'),
131
- poolSize: z.number().default(10),
132
- },
133
- port: z.number().default(3000),
134
- features: {
135
- logging: {
136
- level: z.string().default('info'),
137
- pretty: z.boolean().default(true),
138
- },
139
- },
140
- };
141
-
142
- const parser = new ConfigParser(config);
143
- const result = parser.parse();
144
-
145
- expect(result).toEqual({
146
- appName: 'Test App',
147
- database: {
148
- url: 'postgres://localhost/test',
149
- poolSize: 10,
150
- },
151
- port: 3000,
152
- features: {
153
- logging: {
154
- level: 'info',
155
- pretty: true,
156
- },
157
- },
158
- });
159
- });
160
- });
161
-
162
- describe('Error handling', () => {
163
- it('should throw ZodError for schemas without defaults', () => {
164
- const config = {
165
- required: z.string(),
166
- alsoRequired: z.number(),
167
- };
168
-
169
- const parser = new ConfigParser(config);
170
-
171
- expect(() => parser.parse()).toThrow(z.ZodError);
172
- });
173
-
174
- it('should collect multiple validation errors', () => {
175
- const config = {
176
- field1: z.string(),
177
- field2: z.number(),
178
- field3: z.boolean(),
179
- };
180
-
181
- const parser = new ConfigParser(config);
182
-
183
- try {
184
- parser.parse();
185
- // Should not reach here
186
- expect(true).toBe(false);
187
- } catch (error) {
188
- expect(error).toBeInstanceOf(z.ZodError);
189
- const zodError = error as z.ZodError;
190
- expect(zodError.issues).toHaveLength(3);
191
- }
192
- });
193
-
194
- it('should include correct paths in nested validation errors', () => {
195
- const config = {
196
- database: {
197
- host: z.string(),
198
- port: z.number(),
199
- },
200
- api: {
201
- key: z.string(),
202
- },
203
- };
204
-
205
- const parser = new ConfigParser(config);
206
-
207
- try {
208
- parser.parse();
209
- // Should not reach here
210
- expect(true).toBe(false);
211
- } catch (error) {
212
- expect(error).toBeInstanceOf(z.ZodError);
213
- const zodError = error as z.ZodError;
214
-
215
- const paths = zodError.issues.map((err) => err.path.join('.'));
216
- expect(paths).toContain('database.host');
217
- expect(paths).toContain('database.port');
218
- expect(paths).toContain('api.key');
219
- }
220
- });
221
-
222
- it('should use default values that pass validation', () => {
223
- const config = {
224
- port: z.number().min(1000).max(65535).default(3000),
225
- email: z.string().email().default('admin@example.com'),
226
- };
227
-
228
- const parser = new ConfigParser(config);
229
- const result = parser.parse();
230
-
231
- expect(result).toEqual({
232
- port: 3000,
233
- email: 'admin@example.com',
234
- });
235
- });
236
- });
237
-
238
- describe('Type safety', () => {
239
- it('should infer correct types for simple configuration', () => {
240
- const config = {
241
- name: z.string().default('test'),
242
- count: z.number().default(42),
243
- enabled: z.boolean().default(true),
244
- };
245
-
246
- const parser = new ConfigParser(config);
247
- const result = parser.parse();
248
-
249
- // TypeScript should infer the correct types
250
- type ResultType = typeof result;
251
- type ExpectedType = {
252
- name: string;
253
- count: number;
254
- enabled: boolean;
255
- };
256
-
257
- const _typeCheck: ResultType extends ExpectedType ? true : false = true;
258
- const _typeCheck2: ExpectedType extends ResultType ? true : false = true;
259
-
260
- expect(_typeCheck).toBe(true);
261
- expect(_typeCheck2).toBe(true);
262
- });
263
-
264
- it('should infer correct types for nested configuration', () => {
265
- const config = {
266
- database: {
267
- host: z.string().default('localhost'),
268
- port: z.number().default(5432),
269
- },
270
- features: {
271
- auth: z.boolean().default(true),
272
- },
273
- };
274
-
275
- const parser = new ConfigParser(config);
276
- const result = parser.parse();
277
-
278
- // TypeScript should infer the correct nested structure
279
- type ResultType = typeof result;
280
- type ExpectedType = {
281
- database: { host: string; port: number };
282
- features: { auth: boolean };
283
- };
284
-
285
- const _typeCheck: ResultType extends ExpectedType ? true : false = true;
286
- const _typeCheck2: ExpectedType extends ResultType ? true : false = true;
287
-
288
- expect(_typeCheck).toBe(true);
289
- expect(_typeCheck2).toBe(true);
290
- });
291
-
292
- it('should handle optional types correctly', () => {
293
- const config = {
294
- required: z.string().default('value'),
295
- optional: z.string().optional(),
296
- nullable: z.string().nullable().default(null),
297
- };
298
-
299
- const parser = new ConfigParser(config);
300
- const _result = parser.parse();
301
- });
302
- });
303
-
304
- describe('Complex schemas', () => {
305
- it('should handle enum schemas', () => {
306
- const config = {
307
- environment: z
308
- .enum(['development', 'staging', 'production'])
309
- .default('development'),
310
- logLevel: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
311
- };
312
-
313
- const parser = new ConfigParser(config);
314
- const result = parser.parse();
315
-
316
- expect(result).toEqual({
317
- environment: 'development',
318
- logLevel: 'info',
319
- });
320
- });
321
-
322
- it('should handle union schemas', () => {
323
- const config = {
324
- port: z.union([z.string(), z.number()]).default(3000),
325
- timeout: z.union([z.number(), z.null()]).default(null),
326
- };
327
-
328
- const parser = new ConfigParser(config);
329
- const result = parser.parse();
330
-
331
- expect(result).toEqual({
332
- port: 3000,
333
- timeout: null,
334
- });
335
- });
336
-
337
- it('should handle array schemas', () => {
338
- const config = {
339
- tags: z.array(z.string()).default(['tag1', 'tag2']),
340
- ports: z.array(z.number()).default([3000, 3001]),
341
- };
342
-
343
- const parser = new ConfigParser(config);
344
- const result = parser.parse();
345
-
346
- expect(result).toEqual({
347
- tags: ['tag1', 'tag2'],
348
- ports: [3000, 3001],
349
- });
350
- });
351
-
352
- it('should handle record schemas', () => {
353
- const config = {
354
- metadata: z
355
- .record(z.string(), z.string())
356
- .default({ key1: 'value1', key2: 'value2' }),
357
- counters: z
358
- .record(z.string(), z.number())
359
- .default({ count1: 1, count2: 2 }),
360
- };
361
-
362
- const parser = new ConfigParser(config);
363
- const result = parser.parse();
364
-
365
- expect(result).toEqual({
366
- metadata: { key1: 'value1', key2: 'value2' },
367
- counters: { count1: 1, count2: 2 },
368
- });
369
- });
370
-
371
- it('should handle transformed schemas', () => {
372
- const config = {
373
- portString: z.string().transform(Number).default(8080),
374
- booleanString: z
375
- .string()
376
- .transform((v) => v === 'true')
377
- .default(false),
378
- jsonString: z
379
- .string()
380
- .transform((v) => JSON.parse(v))
381
- .default({ key: 'value' }),
382
- };
383
-
384
- const parser = new ConfigParser(config);
385
- const result = parser.parse();
386
-
387
- expect(result).toEqual({
388
- portString: 8080,
389
- booleanString: false,
390
- jsonString: { key: 'value' },
391
- });
392
- });
393
- });
394
- });
@@ -1,254 +0,0 @@
1
- import { describe, expect, it, vi } from 'vitest';
2
-
3
- import { EnvironmentBuilder, environmentCase } from '../EnvironmentBuilder';
4
-
5
- describe('environmentCase', () => {
6
- it('should convert camelCase to UPPER_SNAKE_CASE', () => {
7
- expect(environmentCase('myVariable')).toBe('MY_VARIABLE');
8
- expect(environmentCase('apiUrl')).toBe('API_URL');
9
- expect(environmentCase('databaseName')).toBe('DATABASE_NAME');
10
- });
11
-
12
- it('should handle already snake_case', () => {
13
- expect(environmentCase('my_variable')).toBe('MY_VARIABLE');
14
- expect(environmentCase('api_url')).toBe('API_URL');
15
- });
16
-
17
- it('should remove underscore directly before numbers', () => {
18
- // The regex /_\d+/g only removes underscores that are directly followed by digits
19
- expect(environmentCase('api_v2')).toBe('API_V2');
20
- expect(environmentCase('value_123')).toBe('VALUE123');
21
- expect(environmentCase('my_var_2')).toBe('MY_VAR2');
22
- });
23
-
24
- it('should handle single words', () => {
25
- expect(environmentCase('name')).toBe('NAME');
26
- expect(environmentCase('port')).toBe('PORT');
27
- });
28
- });
29
-
30
- describe('EnvironmentBuilder', () => {
31
- describe('basic functionality', () => {
32
- it('should pass through plain string values with key transformation', () => {
33
- const env = new EnvironmentBuilder(
34
- {
35
- appName: 'my-app',
36
- nodeEnv: 'production',
37
- },
38
- {},
39
- ).build();
40
-
41
- expect(env).toEqual({
42
- APP_NAME: 'my-app',
43
- NODE_ENV: 'production',
44
- });
45
- });
46
-
47
- it('should resolve object values using type-based resolvers', () => {
48
- const env = new EnvironmentBuilder(
49
- {
50
- apiKey: { type: 'secret', value: 'xyz' },
51
- },
52
- {
53
- secret: (key, value: { type: string; value: string }) => ({
54
- [key]: value.value,
55
- }),
56
- },
57
- ).build();
58
-
59
- expect(env).toEqual({
60
- API_KEY: 'xyz',
61
- });
62
- });
63
-
64
- it('should transform resolver output keys to UPPER_SNAKE_CASE', () => {
65
- const env = new EnvironmentBuilder(
66
- {
67
- auth: { type: 'auth0', domain: 'example.auth0.com', clientId: 'abc' },
68
- },
69
- {
70
- auth0: (
71
- key,
72
- value: { type: string; domain: string; clientId: string },
73
- ) => ({
74
- [`${key}Domain`]: value.domain,
75
- [`${key}ClientId`]: value.clientId,
76
- }),
77
- },
78
- ).build();
79
-
80
- expect(env).toEqual({
81
- AUTH_DOMAIN: 'example.auth0.com',
82
- AUTH_CLIENT_ID: 'abc',
83
- });
84
- });
85
-
86
- it('should handle mixed string and object values', () => {
87
- const env = new EnvironmentBuilder(
88
- {
89
- appName: 'my-app',
90
- apiKey: { type: 'secret', value: 'secret-key' },
91
- nodeEnv: 'production',
92
- },
93
- {
94
- secret: (key, value: { type: string; value: string }) => ({
95
- [key]: value.value,
96
- }),
97
- },
98
- ).build();
99
-
100
- expect(env).toEqual({
101
- APP_NAME: 'my-app',
102
- API_KEY: 'secret-key',
103
- NODE_ENV: 'production',
104
- });
105
- });
106
- });
107
-
108
- describe('nested values', () => {
109
- it('should support nested object values', () => {
110
- const env = new EnvironmentBuilder(
111
- {
112
- database: {
113
- type: 'multi-db',
114
- primary: 'pg://primary',
115
- replica: 'pg://replica',
116
- },
117
- },
118
- {
119
- 'multi-db': (
120
- key,
121
- value: { type: string; primary: string; replica: string },
122
- ) => ({
123
- [key]: {
124
- primary: value.primary,
125
- replica: value.replica,
126
- },
127
- }),
128
- },
129
- ).build();
130
-
131
- expect(env).toEqual({
132
- DATABASE: {
133
- primary: 'pg://primary',
134
- replica: 'pg://replica',
135
- },
136
- });
137
- });
138
-
139
- it('should not transform nested object keys', () => {
140
- const env = new EnvironmentBuilder(
141
- {
142
- config: { type: 'nested', camelCase: 'value', snake_case: 'value2' },
143
- },
144
- {
145
- nested: (
146
- key,
147
- value: { type: string; camelCase: string; snake_case: string },
148
- ) => ({
149
- [key]: {
150
- camelCase: value.camelCase,
151
- snake_case: value.snake_case,
152
- },
153
- }),
154
- },
155
- ).build();
156
-
157
- expect(env).toEqual({
158
- CONFIG: {
159
- camelCase: 'value',
160
- snake_case: 'value2',
161
- },
162
- });
163
- });
164
- });
165
-
166
- describe('unmatched values', () => {
167
- it('should call onUnmatchedValue for objects without matching resolver', () => {
168
- const onUnmatchedValue = vi.fn();
169
-
170
- new EnvironmentBuilder(
171
- {
172
- unknown: { type: 'unknown-type', data: 'test' },
173
- },
174
- {},
175
- { onUnmatchedValue },
176
- ).build();
177
-
178
- expect(onUnmatchedValue).toHaveBeenCalledWith('unknown', {
179
- type: 'unknown-type',
180
- data: 'test',
181
- });
182
- });
183
- });
184
-
185
- describe('value types', () => {
186
- it('should support number values', () => {
187
- const env = new EnvironmentBuilder(
188
- {
189
- port: { type: 'port', value: 3000 },
190
- },
191
- {
192
- port: (key, value: { type: string; value: number }) => ({
193
- [key]: value.value,
194
- }),
195
- },
196
- ).build();
197
-
198
- expect(env).toEqual({
199
- PORT: 3000,
200
- });
201
- });
202
-
203
- it('should support boolean values', () => {
204
- const env = new EnvironmentBuilder(
205
- {
206
- enabled: { type: 'flag', value: true },
207
- },
208
- {
209
- flag: (key, value: { type: string; value: boolean }) => ({
210
- [key]: value.value,
211
- }),
212
- },
213
- ).build();
214
-
215
- expect(env).toEqual({
216
- ENABLED: true,
217
- });
218
- });
219
- });
220
-
221
- describe('multiple resolvers', () => {
222
- it('should use the correct resolver for each type', () => {
223
- const env = new EnvironmentBuilder(
224
- {
225
- secret: { type: 'secret', value: 'my-secret' },
226
- database: { type: 'postgres', host: 'localhost', port: 5432 },
227
- bucket: { type: 'bucket', name: 'my-bucket' },
228
- },
229
- {
230
- secret: (key, value: { type: string; value: string }) => ({
231
- [key]: value.value,
232
- }),
233
- postgres: (
234
- key,
235
- value: { type: string; host: string; port: number },
236
- ) => ({
237
- [`${key}Host`]: value.host,
238
- [`${key}Port`]: value.port,
239
- }),
240
- bucket: (key, value: { type: string; name: string }) => ({
241
- [`${key}Name`]: value.name,
242
- }),
243
- },
244
- ).build();
245
-
246
- expect(env).toEqual({
247
- SECRET: 'my-secret',
248
- DATABASE_HOST: 'localhost',
249
- DATABASE_PORT: 5432,
250
- BUCKET_NAME: 'my-bucket',
251
- });
252
- });
253
- });
254
- });