@geekmidas/envkit 0.0.7 → 0.1.0
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/README.md +228 -174
- package/dist/EnvironmentBuilder-DHfDXJUm.d.mts +131 -0
- package/dist/EnvironmentBuilder-DfmYRBm-.mjs +83 -0
- package/dist/EnvironmentBuilder-DfmYRBm-.mjs.map +1 -0
- package/dist/EnvironmentBuilder-W2wku49g.cjs +95 -0
- package/dist/EnvironmentBuilder-W2wku49g.cjs.map +1 -0
- package/dist/EnvironmentBuilder-Xuf2Dd9u.d.cts +131 -0
- package/dist/EnvironmentBuilder.cjs +4 -0
- package/dist/EnvironmentBuilder.d.cts +2 -0
- package/dist/EnvironmentBuilder.d.mts +2 -0
- package/dist/EnvironmentBuilder.mjs +3 -0
- package/dist/{EnvironmentParser-BDPDLv6i.cjs → EnvironmentParser-Bt246UeP.cjs} +46 -3
- package/dist/EnvironmentParser-Bt246UeP.cjs.map +1 -0
- package/dist/{EnvironmentParser-C-arQEHQ.d.mts → EnvironmentParser-CVWU1ooT.d.mts} +40 -2
- package/dist/{EnvironmentParser-CQUOGqc0.mjs → EnvironmentParser-c06agx31.mjs} +46 -3
- package/dist/EnvironmentParser-c06agx31.mjs.map +1 -0
- package/dist/{EnvironmentParser-X4h2Vp4r.d.cts → EnvironmentParser-tV-JjCg7.d.cts} +40 -2
- 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/SstEnvironmentBuilder-BuFw1hCe.cjs +125 -0
- package/dist/SstEnvironmentBuilder-BuFw1hCe.cjs.map +1 -0
- package/dist/SstEnvironmentBuilder-CjURMGjW.d.mts +177 -0
- package/dist/SstEnvironmentBuilder-D4oSo_KX.d.cts +177 -0
- package/dist/SstEnvironmentBuilder-DEa3lTUB.mjs +108 -0
- package/dist/SstEnvironmentBuilder-DEa3lTUB.mjs.map +1 -0
- package/dist/SstEnvironmentBuilder.cjs +7 -0
- package/dist/SstEnvironmentBuilder.d.cts +3 -0
- package/dist/SstEnvironmentBuilder.d.mts +3 -0
- package/dist/SstEnvironmentBuilder.mjs +4 -0
- package/dist/index.cjs +6 -2
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.mjs +3 -2
- package/dist/sst.cjs +30 -4
- package/dist/sst.cjs.map +1 -0
- package/dist/sst.d.cts +15 -93
- package/dist/sst.d.mts +15 -93
- package/dist/sst.mjs +26 -2
- package/dist/sst.mjs.map +1 -0
- package/docs/async-secrets-design.md +355 -0
- package/package.json +11 -2
- package/src/EnvironmentBuilder.ts +196 -0
- package/src/EnvironmentParser.ts +51 -2
- package/src/SnifferEnvironmentParser.ts +209 -0
- package/src/SstEnvironmentBuilder.ts +298 -0
- package/src/__tests__/EnvironmentBuilder.spec.ts +274 -0
- package/src/__tests__/EnvironmentParser.spec.ts +147 -0
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +332 -0
- package/src/__tests__/SstEnvironmentBuilder.spec.ts +373 -0
- package/src/__tests__/sst.spec.ts +1 -1
- package/src/index.ts +13 -1
- package/src/sst.ts +45 -207
- 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,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
|
+
});
|
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type Bucket,
|
|
5
|
+
type Postgres,
|
|
6
|
+
ResourceType,
|
|
7
|
+
type Secret,
|
|
8
|
+
type SnsTopic,
|
|
9
|
+
SstEnvironmentBuilder,
|
|
10
|
+
sstResolvers,
|
|
11
|
+
} from '../SstEnvironmentBuilder';
|
|
12
|
+
|
|
13
|
+
describe('SstEnvironmentBuilder', () => {
|
|
14
|
+
describe('basic functionality', () => {
|
|
15
|
+
it('should pass through plain string values with key transformation', () => {
|
|
16
|
+
const env = new SstEnvironmentBuilder({
|
|
17
|
+
appName: 'my-app',
|
|
18
|
+
nodeEnv: 'production',
|
|
19
|
+
}).build();
|
|
20
|
+
|
|
21
|
+
expect(env).toEqual({
|
|
22
|
+
APP_NAME: 'my-app',
|
|
23
|
+
NODE_ENV: 'production',
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should handle empty input', () => {
|
|
28
|
+
const env = new SstEnvironmentBuilder({}).build();
|
|
29
|
+
expect(env).toEqual({});
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
describe('Secret resource', () => {
|
|
34
|
+
it('should process Secret resource correctly', () => {
|
|
35
|
+
const secret: Secret = {
|
|
36
|
+
type: ResourceType.Secret,
|
|
37
|
+
value: 'super-secret-value',
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const env = new SstEnvironmentBuilder({
|
|
41
|
+
mySecret: secret,
|
|
42
|
+
}).build();
|
|
43
|
+
|
|
44
|
+
expect(env).toEqual({
|
|
45
|
+
MY_SECRET: 'super-secret-value',
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('should process SSTSecret resource correctly', () => {
|
|
50
|
+
const secret: Secret = {
|
|
51
|
+
type: ResourceType.SSTSecret,
|
|
52
|
+
value: 'another-secret',
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const env = new SstEnvironmentBuilder({
|
|
56
|
+
appSecret: secret,
|
|
57
|
+
}).build();
|
|
58
|
+
|
|
59
|
+
expect(env).toEqual({
|
|
60
|
+
APP_SECRET: 'another-secret',
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe('Postgres resource', () => {
|
|
66
|
+
it('should process Postgres resource correctly', () => {
|
|
67
|
+
const postgres: Postgres = {
|
|
68
|
+
type: ResourceType.Postgres,
|
|
69
|
+
database: 'myapp',
|
|
70
|
+
host: 'localhost',
|
|
71
|
+
password: 'password123',
|
|
72
|
+
port: 5432,
|
|
73
|
+
username: 'postgres',
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const env = new SstEnvironmentBuilder({
|
|
77
|
+
database: postgres,
|
|
78
|
+
}).build();
|
|
79
|
+
|
|
80
|
+
expect(env).toEqual({
|
|
81
|
+
DATABASE_NAME: 'myapp',
|
|
82
|
+
DATABASE_HOST: 'localhost',
|
|
83
|
+
DATABASE_PASSWORD: 'password123',
|
|
84
|
+
DATABASE_PORT: 5432,
|
|
85
|
+
DATABASE_USERNAME: 'postgres',
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should process SSTPostgres resource correctly', () => {
|
|
90
|
+
const postgres: Postgres = {
|
|
91
|
+
type: ResourceType.SSTPostgres,
|
|
92
|
+
database: 'prod_db',
|
|
93
|
+
host: 'prod.example.com',
|
|
94
|
+
password: 'prod-password',
|
|
95
|
+
port: 5433,
|
|
96
|
+
username: 'prod_user',
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const env = new SstEnvironmentBuilder({
|
|
100
|
+
mainDb: postgres,
|
|
101
|
+
}).build();
|
|
102
|
+
|
|
103
|
+
expect(env).toEqual({
|
|
104
|
+
MAIN_DB_NAME: 'prod_db',
|
|
105
|
+
MAIN_DB_HOST: 'prod.example.com',
|
|
106
|
+
MAIN_DB_PASSWORD: 'prod-password',
|
|
107
|
+
MAIN_DB_PORT: 5433,
|
|
108
|
+
MAIN_DB_USERNAME: 'prod_user',
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('Bucket resource', () => {
|
|
114
|
+
it('should process Bucket resource correctly', () => {
|
|
115
|
+
const bucket: Bucket = {
|
|
116
|
+
type: ResourceType.Bucket,
|
|
117
|
+
name: 'my-s3-bucket',
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const env = new SstEnvironmentBuilder({
|
|
121
|
+
uploadBucket: bucket,
|
|
122
|
+
}).build();
|
|
123
|
+
|
|
124
|
+
expect(env).toEqual({
|
|
125
|
+
UPLOAD_BUCKET_NAME: 'my-s3-bucket',
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('should process SSTBucket resource correctly', () => {
|
|
130
|
+
const bucket: Bucket = {
|
|
131
|
+
type: ResourceType.SSTBucket,
|
|
132
|
+
name: 'assets-bucket-prod',
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const env = new SstEnvironmentBuilder({
|
|
136
|
+
assetStorage: bucket,
|
|
137
|
+
}).build();
|
|
138
|
+
|
|
139
|
+
expect(env).toEqual({
|
|
140
|
+
ASSET_STORAGE_NAME: 'assets-bucket-prod',
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe('SnsTopic resource', () => {
|
|
146
|
+
it('should process SnsTopic resource correctly', () => {
|
|
147
|
+
const topic: SnsTopic = {
|
|
148
|
+
type: ResourceType.SnsTopic,
|
|
149
|
+
arn: 'arn:aws:sns:us-east-1:123456789:my-topic',
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const env = new SstEnvironmentBuilder({
|
|
153
|
+
eventsTopic: topic,
|
|
154
|
+
}).build();
|
|
155
|
+
|
|
156
|
+
expect(env).toEqual({
|
|
157
|
+
EVENTS_TOPIC_ARN: 'arn:aws:sns:us-east-1:123456789:my-topic',
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe('noop resources', () => {
|
|
163
|
+
it('should not add environment variables for ApiGatewayV2', () => {
|
|
164
|
+
const env = new SstEnvironmentBuilder({
|
|
165
|
+
api: {
|
|
166
|
+
type: ResourceType.ApiGatewayV2,
|
|
167
|
+
url: 'https://api.example.com',
|
|
168
|
+
},
|
|
169
|
+
}).build();
|
|
170
|
+
|
|
171
|
+
expect(env).toEqual({});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it('should not add environment variables for Function', () => {
|
|
175
|
+
const env = new SstEnvironmentBuilder({
|
|
176
|
+
handler: {
|
|
177
|
+
type: ResourceType.Function,
|
|
178
|
+
name: 'my-lambda',
|
|
179
|
+
},
|
|
180
|
+
}).build();
|
|
181
|
+
|
|
182
|
+
expect(env).toEqual({});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should not add environment variables for Vpc', () => {
|
|
186
|
+
const env = new SstEnvironmentBuilder({
|
|
187
|
+
network: {
|
|
188
|
+
type: ResourceType.Vpc,
|
|
189
|
+
bastion: 'bastion-host',
|
|
190
|
+
},
|
|
191
|
+
}).build();
|
|
192
|
+
|
|
193
|
+
expect(env).toEqual({});
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('mixed resources', () => {
|
|
198
|
+
it('should handle mix of strings and resources', () => {
|
|
199
|
+
const postgres: Postgres = {
|
|
200
|
+
type: ResourceType.Postgres,
|
|
201
|
+
database: 'app_db',
|
|
202
|
+
host: 'db.example.com',
|
|
203
|
+
password: 'db-pass',
|
|
204
|
+
port: 5432,
|
|
205
|
+
username: 'app_user',
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const secret: Secret = {
|
|
209
|
+
type: ResourceType.Secret,
|
|
210
|
+
value: 'jwt-secret',
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const bucket: Bucket = {
|
|
214
|
+
type: ResourceType.Bucket,
|
|
215
|
+
name: 'uploads-bucket',
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const topic: SnsTopic = {
|
|
219
|
+
type: ResourceType.SnsTopic,
|
|
220
|
+
arn: 'arn:aws:sns:us-east-1:123456789:events',
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
const env = new SstEnvironmentBuilder({
|
|
224
|
+
nodeEnv: 'production',
|
|
225
|
+
appName: 'My App',
|
|
226
|
+
database: postgres,
|
|
227
|
+
jwtSecret: secret,
|
|
228
|
+
uploads: bucket,
|
|
229
|
+
events: topic,
|
|
230
|
+
apiVersion: 'v2',
|
|
231
|
+
}).build();
|
|
232
|
+
|
|
233
|
+
expect(env).toEqual({
|
|
234
|
+
NODE_ENV: 'production',
|
|
235
|
+
APP_NAME: 'My App',
|
|
236
|
+
DATABASE_NAME: 'app_db',
|
|
237
|
+
DATABASE_HOST: 'db.example.com',
|
|
238
|
+
DATABASE_PASSWORD: 'db-pass',
|
|
239
|
+
DATABASE_PORT: 5432,
|
|
240
|
+
DATABASE_USERNAME: 'app_user',
|
|
241
|
+
JWT_SECRET: 'jwt-secret',
|
|
242
|
+
UPLOADS_NAME: 'uploads-bucket',
|
|
243
|
+
EVENTS_ARN: 'arn:aws:sns:us-east-1:123456789:events',
|
|
244
|
+
API_VERSION: 'v2',
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
describe('additional resolvers', () => {
|
|
250
|
+
it('should allow custom resolvers', () => {
|
|
251
|
+
const env = new SstEnvironmentBuilder(
|
|
252
|
+
{
|
|
253
|
+
custom: { type: 'my-custom-type' as const, data: 'custom-data' },
|
|
254
|
+
secret: { type: ResourceType.Secret, value: 'secret-value' },
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
'my-custom-type': (key: string, value: { data: string }) => ({
|
|
258
|
+
[`${key}Data`]: value.data,
|
|
259
|
+
}),
|
|
260
|
+
},
|
|
261
|
+
).build();
|
|
262
|
+
|
|
263
|
+
expect(env).toEqual({
|
|
264
|
+
CUSTOM_DATA: 'custom-data',
|
|
265
|
+
SECRET: 'secret-value',
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it('should allow custom resolvers to override built-in resolvers', () => {
|
|
270
|
+
const env = new SstEnvironmentBuilder(
|
|
271
|
+
{
|
|
272
|
+
mySecret: { type: ResourceType.Secret, value: 'original-value' },
|
|
273
|
+
},
|
|
274
|
+
{
|
|
275
|
+
[ResourceType.Secret]: (
|
|
276
|
+
key: string,
|
|
277
|
+
value: { type: string; value: string },
|
|
278
|
+
) => ({
|
|
279
|
+
[`${key}Custom`]: `modified-${value.value}`,
|
|
280
|
+
}),
|
|
281
|
+
},
|
|
282
|
+
).build();
|
|
283
|
+
|
|
284
|
+
expect(env).toEqual({
|
|
285
|
+
MY_SECRET_CUSTOM: 'modified-original-value',
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
describe('edge cases', () => {
|
|
291
|
+
it('should warn for unknown resource types', () => {
|
|
292
|
+
const consoleWarnSpy = vi
|
|
293
|
+
.spyOn(console, 'warn')
|
|
294
|
+
.mockImplementation(() => {});
|
|
295
|
+
|
|
296
|
+
const unknownResource = {
|
|
297
|
+
type: 'unknown.resource.Type',
|
|
298
|
+
value: 'something',
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
const env = new SstEnvironmentBuilder({
|
|
302
|
+
unknown: unknownResource,
|
|
303
|
+
}).build();
|
|
304
|
+
|
|
305
|
+
expect(env).toEqual({});
|
|
306
|
+
expect(consoleWarnSpy).toHaveBeenCalled();
|
|
307
|
+
|
|
308
|
+
consoleWarnSpy.mockRestore();
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('should handle resources with special characters in keys', () => {
|
|
312
|
+
const secret: Secret = {
|
|
313
|
+
type: ResourceType.Secret,
|
|
314
|
+
value: 'value',
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const env = new SstEnvironmentBuilder({
|
|
318
|
+
'my-secret-key': secret,
|
|
319
|
+
'another.secret': secret,
|
|
320
|
+
}).build();
|
|
321
|
+
|
|
322
|
+
expect(env).toEqual({
|
|
323
|
+
MY_SECRET_KEY: 'value',
|
|
324
|
+
ANOTHER_SECRET: 'value',
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it('should preserve numeric values for postgres port', () => {
|
|
329
|
+
const postgres: Postgres = {
|
|
330
|
+
type: ResourceType.Postgres,
|
|
331
|
+
database: 'test',
|
|
332
|
+
host: 'localhost',
|
|
333
|
+
password: 'pass',
|
|
334
|
+
port: 5432,
|
|
335
|
+
username: 'user',
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const env = new SstEnvironmentBuilder({
|
|
339
|
+
db: postgres,
|
|
340
|
+
}).build();
|
|
341
|
+
|
|
342
|
+
expect(env.DB_PORT).toBe(5432);
|
|
343
|
+
expect(typeof env.DB_PORT).toBe('number');
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
describe('sstResolvers export', () => {
|
|
348
|
+
it('should export pre-configured SST resolvers', () => {
|
|
349
|
+
expect(sstResolvers).toBeDefined();
|
|
350
|
+
expect(typeof sstResolvers[ResourceType.Secret]).toBe('function');
|
|
351
|
+
expect(typeof sstResolvers[ResourceType.Postgres]).toBe('function');
|
|
352
|
+
expect(typeof sstResolvers[ResourceType.Bucket]).toBe('function');
|
|
353
|
+
expect(typeof sstResolvers[ResourceType.SnsTopic]).toBe('function');
|
|
354
|
+
});
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
describe('ResourceType enum', () => {
|
|
358
|
+
it('should have all expected resource types', () => {
|
|
359
|
+
expect(ResourceType.ApiGatewayV2).toBe('sst.aws.ApiGatewayV2');
|
|
360
|
+
expect(ResourceType.Postgres).toBe('sst.aws.Postgres');
|
|
361
|
+
expect(ResourceType.Function).toBe('sst.aws.Function');
|
|
362
|
+
expect(ResourceType.Bucket).toBe('sst.aws.Bucket');
|
|
363
|
+
expect(ResourceType.Vpc).toBe('sst.aws.Vpc');
|
|
364
|
+
expect(ResourceType.Secret).toBe('sst.sst.Secret');
|
|
365
|
+
expect(ResourceType.SSTSecret).toBe('sst:sst:Secret');
|
|
366
|
+
expect(ResourceType.SSTFunction).toBe('sst:sst:Function');
|
|
367
|
+
expect(ResourceType.SSTApiGatewayV2).toBe('sst:aws:ApiGatewayV2');
|
|
368
|
+
expect(ResourceType.SSTPostgres).toBe('sst:aws:Postgres');
|
|
369
|
+
expect(ResourceType.SSTBucket).toBe('sst:aws:Bucket');
|
|
370
|
+
expect(ResourceType.SnsTopic).toBe('sst:aws:SnsTopic');
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
});
|