@geekmidas/envkit 0.0.2 → 0.0.4
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-Bo2CCl_K.cjs +178 -0
- package/dist/EnvironmentParser-jKrGMBhP.mjs +166 -0
- package/dist/EnvironmentParser.cjs +1 -1
- package/dist/EnvironmentParser.mjs +1 -1
- package/dist/__tests__/ConfigParser.spec.cjs +323 -0
- package/dist/__tests__/ConfigParser.spec.mjs +322 -0
- package/dist/__tests__/EnvironmentParser.spec.cjs +422 -0
- package/dist/__tests__/EnvironmentParser.spec.mjs +421 -0
- package/dist/chunk-CUT6urMc.cjs +30 -0
- package/dist/index.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sst.cjs +131 -0
- package/dist/sst.mjs +128 -0
- package/package.json +10 -3
- package/src/EnvironmentParser.ts +166 -49
- package/src/__tests__/ConfigParser.spec.ts +394 -0
- package/src/__tests__/EnvironmentParser.spec.ts +692 -0
- package/src/sst.ts +221 -0
- package/dist/EnvironmentParser-Dd6TbwJC.mjs +0 -99
- package/dist/EnvironmentParser-nZnZXM_Y.cjs +0 -133
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import { EnvironmentParser } from "../EnvironmentParser-jKrGMBhP.mjs";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { describe, expect, it } from "vitest";
|
|
4
|
+
|
|
5
|
+
//#region src/__tests__/EnvironmentParser.spec.ts
|
|
6
|
+
describe("EnvironmentParser", () => {
|
|
7
|
+
describe("Basic parsing functionality", () => {
|
|
8
|
+
it("should parse simple string values", () => {
|
|
9
|
+
const env = { APP_NAME: "Test App" };
|
|
10
|
+
const parser = new EnvironmentParser(env);
|
|
11
|
+
const config = parser.create((get) => ({ appName: get("APP_NAME").string() })).parse();
|
|
12
|
+
expect(config).toEqual({ appName: "Test App" });
|
|
13
|
+
});
|
|
14
|
+
it("should parse with default values when env var is missing", () => {
|
|
15
|
+
const env = {};
|
|
16
|
+
const parser = new EnvironmentParser(env);
|
|
17
|
+
const config = parser.create((get) => ({
|
|
18
|
+
appName: get("APP_NAME").string().default("Default App"),
|
|
19
|
+
port: get("PORT").string().transform(Number).default(3e3)
|
|
20
|
+
})).parse();
|
|
21
|
+
expect(config).toEqual({
|
|
22
|
+
appName: "Default App",
|
|
23
|
+
port: 3e3
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
it("should transform string to number", () => {
|
|
27
|
+
const env = { PORT: "8080" };
|
|
28
|
+
const parser = new EnvironmentParser(env);
|
|
29
|
+
const config = parser.create((get) => ({ port: get("PORT").string().transform(Number) })).parse();
|
|
30
|
+
expect(config).toEqual({ port: 8080 });
|
|
31
|
+
});
|
|
32
|
+
it("should transform string to boolean", () => {
|
|
33
|
+
const env = {
|
|
34
|
+
FEATURE_ENABLED: "true",
|
|
35
|
+
FEATURE_DISABLED: "false",
|
|
36
|
+
FEATURE_TRUTHY: "yes"
|
|
37
|
+
};
|
|
38
|
+
const parser = new EnvironmentParser(env);
|
|
39
|
+
const config = parser.create((get) => ({
|
|
40
|
+
enabled: get("FEATURE_ENABLED").string().transform((v) => v === "true"),
|
|
41
|
+
disabled: get("FEATURE_DISABLED").string().transform((v) => v === "true"),
|
|
42
|
+
truthy: get("FEATURE_TRUTHY").string().transform((v) => v === "true")
|
|
43
|
+
})).parse();
|
|
44
|
+
expect(config).toEqual({
|
|
45
|
+
enabled: true,
|
|
46
|
+
disabled: false,
|
|
47
|
+
truthy: false
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
it("should handle optional values", () => {
|
|
51
|
+
const env = { REQUIRED: "value" };
|
|
52
|
+
const parser = new EnvironmentParser(env);
|
|
53
|
+
const config = parser.create((get) => ({
|
|
54
|
+
required: get("REQUIRED").string(),
|
|
55
|
+
optional: get("OPTIONAL").string().optional()
|
|
56
|
+
})).parse();
|
|
57
|
+
expect(config).toEqual({
|
|
58
|
+
required: "value",
|
|
59
|
+
optional: void 0
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
it("should validate URLs", () => {
|
|
63
|
+
const env = {
|
|
64
|
+
VALID_URL: "https://example.com",
|
|
65
|
+
DATABASE_URL: "postgresql://user:pass@localhost:5432/db"
|
|
66
|
+
};
|
|
67
|
+
const parser = new EnvironmentParser(env);
|
|
68
|
+
const config = parser.create((get) => ({
|
|
69
|
+
apiUrl: get("VALID_URL").string().url(),
|
|
70
|
+
dbUrl: get("DATABASE_URL").string().url()
|
|
71
|
+
})).parse();
|
|
72
|
+
expect(config).toEqual({
|
|
73
|
+
apiUrl: "https://example.com",
|
|
74
|
+
dbUrl: "postgresql://user:pass@localhost:5432/db"
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
it("should validate email addresses", () => {
|
|
78
|
+
const env = { ADMIN_EMAIL: "admin@example.com" };
|
|
79
|
+
const parser = new EnvironmentParser(env);
|
|
80
|
+
const config = parser.create((get) => ({ adminEmail: get("ADMIN_EMAIL").string().email() })).parse();
|
|
81
|
+
expect(config).toEqual({ adminEmail: "admin@example.com" });
|
|
82
|
+
});
|
|
83
|
+
it("should validate enums", () => {
|
|
84
|
+
const env = { NODE_ENV: "production" };
|
|
85
|
+
const parser = new EnvironmentParser(env);
|
|
86
|
+
const config = parser.create((get) => ({ env: get("NODE_ENV").enum([
|
|
87
|
+
"development",
|
|
88
|
+
"staging",
|
|
89
|
+
"production"
|
|
90
|
+
]) })).parse();
|
|
91
|
+
expect(config).toEqual({ env: "production" });
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe("Nested configuration", () => {
|
|
95
|
+
it("should handle nested objects", () => {
|
|
96
|
+
const env = {
|
|
97
|
+
DB_HOST: "localhost",
|
|
98
|
+
DB_PORT: "5432",
|
|
99
|
+
DB_NAME: "myapp",
|
|
100
|
+
API_KEY: "secret123",
|
|
101
|
+
API_URL: "https://api.example.com"
|
|
102
|
+
};
|
|
103
|
+
const parser = new EnvironmentParser(env);
|
|
104
|
+
const config = parser.create((get) => ({
|
|
105
|
+
database: {
|
|
106
|
+
host: get("DB_HOST").string(),
|
|
107
|
+
port: get("DB_PORT").string().transform(Number),
|
|
108
|
+
name: get("DB_NAME").string()
|
|
109
|
+
},
|
|
110
|
+
api: {
|
|
111
|
+
key: get("API_KEY").string(),
|
|
112
|
+
url: get("API_URL").string().url()
|
|
113
|
+
}
|
|
114
|
+
})).parse();
|
|
115
|
+
expect(config).toEqual({
|
|
116
|
+
database: {
|
|
117
|
+
host: "localhost",
|
|
118
|
+
port: 5432,
|
|
119
|
+
name: "myapp"
|
|
120
|
+
},
|
|
121
|
+
api: {
|
|
122
|
+
key: "secret123",
|
|
123
|
+
url: "https://api.example.com"
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
it("should handle deeply nested objects", () => {
|
|
128
|
+
const env = {
|
|
129
|
+
FEATURE_AUTH_ENABLED: "true",
|
|
130
|
+
FEATURE_AUTH_PROVIDER: "oauth",
|
|
131
|
+
FEATURE_CACHE_ENABLED: "false",
|
|
132
|
+
FEATURE_CACHE_TTL: "3600"
|
|
133
|
+
};
|
|
134
|
+
const parser = new EnvironmentParser(env);
|
|
135
|
+
const config = parser.create((get) => ({ features: {
|
|
136
|
+
authentication: {
|
|
137
|
+
enabled: get("FEATURE_AUTH_ENABLED").string().transform((v) => v === "true"),
|
|
138
|
+
provider: get("FEATURE_AUTH_PROVIDER").string()
|
|
139
|
+
},
|
|
140
|
+
cache: {
|
|
141
|
+
enabled: get("FEATURE_CACHE_ENABLED").string().transform((v) => v === "true"),
|
|
142
|
+
ttl: get("FEATURE_CACHE_TTL").string().transform(Number)
|
|
143
|
+
}
|
|
144
|
+
} })).parse();
|
|
145
|
+
expect(config).toEqual({ features: {
|
|
146
|
+
authentication: {
|
|
147
|
+
enabled: true,
|
|
148
|
+
provider: "oauth"
|
|
149
|
+
},
|
|
150
|
+
cache: {
|
|
151
|
+
enabled: false,
|
|
152
|
+
ttl: 3600
|
|
153
|
+
}
|
|
154
|
+
} });
|
|
155
|
+
});
|
|
156
|
+
it("should handle mixed nested objects with defaults", () => {
|
|
157
|
+
const env = {
|
|
158
|
+
DB_HOST: "custom-host",
|
|
159
|
+
REDIS_TTL: "7200"
|
|
160
|
+
};
|
|
161
|
+
const parser = new EnvironmentParser(env);
|
|
162
|
+
const config = parser.create((get) => ({
|
|
163
|
+
database: {
|
|
164
|
+
host: get("DB_HOST").string(),
|
|
165
|
+
port: get("DB_PORT").string().transform(Number).default(5432),
|
|
166
|
+
ssl: get("DB_SSL").string().transform((v) => v === "true").default(false)
|
|
167
|
+
},
|
|
168
|
+
cache: { redis: {
|
|
169
|
+
host: get("REDIS_HOST").string().default("localhost"),
|
|
170
|
+
port: get("REDIS_PORT").string().transform(Number).default(6379),
|
|
171
|
+
ttl: get("REDIS_TTL").string().transform(Number)
|
|
172
|
+
} }
|
|
173
|
+
})).parse();
|
|
174
|
+
expect(config).toEqual({
|
|
175
|
+
database: {
|
|
176
|
+
host: "custom-host",
|
|
177
|
+
port: 5432,
|
|
178
|
+
ssl: false
|
|
179
|
+
},
|
|
180
|
+
cache: { redis: {
|
|
181
|
+
host: "localhost",
|
|
182
|
+
port: 6379,
|
|
183
|
+
ttl: 7200
|
|
184
|
+
} }
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
describe("Error handling and validation", () => {
|
|
189
|
+
it("should throw ZodError for missing required values", () => {
|
|
190
|
+
const env = {};
|
|
191
|
+
const parser = new EnvironmentParser(env);
|
|
192
|
+
expect(() => {
|
|
193
|
+
parser.create((get) => ({ required: get("REQUIRED_VAR").string() })).parse();
|
|
194
|
+
}).toThrow(z.ZodError);
|
|
195
|
+
});
|
|
196
|
+
it("should throw ZodError with descriptive error messages", () => {
|
|
197
|
+
const env = {};
|
|
198
|
+
const parser = new EnvironmentParser(env);
|
|
199
|
+
try {
|
|
200
|
+
parser.create((get) => ({
|
|
201
|
+
apiKey: get("API_KEY").string().min(32),
|
|
202
|
+
dbUrl: get("DATABASE_URL").string().url()
|
|
203
|
+
})).parse();
|
|
204
|
+
} catch (error) {
|
|
205
|
+
expect(error).toBeInstanceOf(z.ZodError);
|
|
206
|
+
const zodError = error;
|
|
207
|
+
expect(zodError.issues).toHaveLength(2);
|
|
208
|
+
expect(zodError.issues[0].message).toContain("Environment variable \"API_KEY\"");
|
|
209
|
+
expect(zodError.issues[1].message).toContain("Environment variable \"DATABASE_URL\"");
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
it("should validate minimum string length", () => {
|
|
213
|
+
const env = { API_KEY: "short" };
|
|
214
|
+
const parser = new EnvironmentParser(env);
|
|
215
|
+
expect(() => {
|
|
216
|
+
parser.create((get) => ({ apiKey: get("API_KEY").string().min(32) })).parse();
|
|
217
|
+
}).toThrow(z.ZodError);
|
|
218
|
+
});
|
|
219
|
+
it("should validate maximum string length", () => {
|
|
220
|
+
const env = { SHORT_TEXT: "a".repeat(100) };
|
|
221
|
+
const parser = new EnvironmentParser(env);
|
|
222
|
+
expect(() => {
|
|
223
|
+
parser.create((get) => ({ shortText: get("SHORT_TEXT").string().max(50) })).parse();
|
|
224
|
+
}).toThrow(z.ZodError);
|
|
225
|
+
});
|
|
226
|
+
it("should validate exact string length", () => {
|
|
227
|
+
const env = {
|
|
228
|
+
VALID_KEY: "a".repeat(32),
|
|
229
|
+
INVALID_KEY: "short"
|
|
230
|
+
};
|
|
231
|
+
const parser = new EnvironmentParser(env);
|
|
232
|
+
const validConfig = parser.create((get) => ({ key: get("VALID_KEY").string().length(32) })).parse();
|
|
233
|
+
expect(validConfig).toEqual({ key: "a".repeat(32) });
|
|
234
|
+
expect(() => {
|
|
235
|
+
parser.create((get) => ({ key: get("INVALID_KEY").string().length(32) })).parse();
|
|
236
|
+
}).toThrow(z.ZodError);
|
|
237
|
+
});
|
|
238
|
+
it("should validate invalid URLs", () => {
|
|
239
|
+
const env = { INVALID_URL: "not-a-url" };
|
|
240
|
+
const parser = new EnvironmentParser(env);
|
|
241
|
+
expect(() => {
|
|
242
|
+
parser.create((get) => ({ url: get("INVALID_URL").string().url() })).parse();
|
|
243
|
+
}).toThrow(z.ZodError);
|
|
244
|
+
});
|
|
245
|
+
it("should validate invalid email addresses", () => {
|
|
246
|
+
const env = { INVALID_EMAIL: "not-an-email" };
|
|
247
|
+
const parser = new EnvironmentParser(env);
|
|
248
|
+
expect(() => {
|
|
249
|
+
parser.create((get) => ({ email: get("INVALID_EMAIL").string().email() })).parse();
|
|
250
|
+
}).toThrow(z.ZodError);
|
|
251
|
+
});
|
|
252
|
+
it("should validate invalid enum values", () => {
|
|
253
|
+
const env = { NODE_ENV: "invalid" };
|
|
254
|
+
const parser = new EnvironmentParser(env);
|
|
255
|
+
expect(() => {
|
|
256
|
+
parser.create((get) => ({ env: get("NODE_ENV").enum([
|
|
257
|
+
"development",
|
|
258
|
+
"staging",
|
|
259
|
+
"production"
|
|
260
|
+
]) })).parse();
|
|
261
|
+
}).toThrow(z.ZodError);
|
|
262
|
+
});
|
|
263
|
+
it("should validate number ranges", () => {
|
|
264
|
+
const env = {
|
|
265
|
+
VALID_PORT: "8080",
|
|
266
|
+
INVALID_PORT_LOW: "0",
|
|
267
|
+
INVALID_PORT_HIGH: "70000"
|
|
268
|
+
};
|
|
269
|
+
const parser = new EnvironmentParser(env);
|
|
270
|
+
const validConfig = parser.create((get) => ({ port: get("VALID_PORT").coerce.number().min(1).max(65535) })).parse();
|
|
271
|
+
expect(validConfig).toEqual({ port: 8080 });
|
|
272
|
+
expect(() => {
|
|
273
|
+
parser.create((get) => ({ port: get("INVALID_PORT_LOW").coerce.number().min(1).max(65535) })).parse();
|
|
274
|
+
}).toThrow(z.ZodError);
|
|
275
|
+
expect(() => {
|
|
276
|
+
parser.create((get) => ({ port: get("INVALID_PORT_HIGH").coerce.number().min(1).max(65535) })).parse();
|
|
277
|
+
}).toThrow(z.ZodError);
|
|
278
|
+
});
|
|
279
|
+
it("should handle transformation errors", () => {
|
|
280
|
+
const env = { INVALID_NUMBER: "not-a-number" };
|
|
281
|
+
const parser = new EnvironmentParser(env);
|
|
282
|
+
expect(() => {
|
|
283
|
+
parser.create((get) => ({ number: get("INVALID_NUMBER").string().transform((v) => {
|
|
284
|
+
const num = Number(v);
|
|
285
|
+
if (isNaN(num)) throw new Error("Invalid number");
|
|
286
|
+
return num;
|
|
287
|
+
}) })).parse();
|
|
288
|
+
}).toThrow();
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
describe("Complex scenarios", () => {
|
|
292
|
+
it("should handle array transformations", () => {
|
|
293
|
+
const env = {
|
|
294
|
+
ALLOWED_ORIGINS: "http://localhost:3000,https://example.com,https://app.example.com",
|
|
295
|
+
FEATURE_FLAGS: "auth,cache,notifications"
|
|
296
|
+
};
|
|
297
|
+
const parser = new EnvironmentParser(env);
|
|
298
|
+
const config = parser.create((get) => ({
|
|
299
|
+
cors: { origins: get("ALLOWED_ORIGINS").string().transform((origins) => origins.split(",").map((o) => o.trim())) },
|
|
300
|
+
features: get("FEATURE_FLAGS").string().transform((flags) => flags.split(",").map((f) => f.trim()))
|
|
301
|
+
})).parse();
|
|
302
|
+
expect(config).toEqual({
|
|
303
|
+
cors: { origins: [
|
|
304
|
+
"http://localhost:3000",
|
|
305
|
+
"https://example.com",
|
|
306
|
+
"https://app.example.com"
|
|
307
|
+
] },
|
|
308
|
+
features: [
|
|
309
|
+
"auth",
|
|
310
|
+
"cache",
|
|
311
|
+
"notifications"
|
|
312
|
+
]
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
it("should handle JSON parsing", () => {
|
|
316
|
+
const env = {
|
|
317
|
+
FEATURE_CONFIG: "{\"auth\":true,\"cache\":false,\"debug\":true}",
|
|
318
|
+
RATE_LIMITS: "{\"requests\":100,\"window\":60000}"
|
|
319
|
+
};
|
|
320
|
+
const parser = new EnvironmentParser(env);
|
|
321
|
+
const config = parser.create((get) => ({
|
|
322
|
+
features: get("FEATURE_CONFIG").string().transform((str) => JSON.parse(str)).pipe(z.record(z.string(), z.boolean())),
|
|
323
|
+
rateLimits: get("RATE_LIMITS").string().transform((str) => JSON.parse(str)).pipe(z.object({
|
|
324
|
+
requests: z.number(),
|
|
325
|
+
window: z.number()
|
|
326
|
+
}))
|
|
327
|
+
})).parse();
|
|
328
|
+
expect(config).toEqual({
|
|
329
|
+
features: {
|
|
330
|
+
auth: true,
|
|
331
|
+
cache: false,
|
|
332
|
+
debug: true
|
|
333
|
+
},
|
|
334
|
+
rateLimits: {
|
|
335
|
+
requests: 100,
|
|
336
|
+
window: 6e4
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
it("should handle custom refinements", () => {
|
|
341
|
+
const env = {
|
|
342
|
+
CORS_ORIGINS: "https://example.com,https://app.example.com",
|
|
343
|
+
API_KEYS: "key1234567890123456789012345678901,key2345678901234567890123456789012"
|
|
344
|
+
};
|
|
345
|
+
const parser = new EnvironmentParser(env);
|
|
346
|
+
const config = parser.create((get) => ({
|
|
347
|
+
cors: { origins: get("CORS_ORIGINS").string().transform((origins) => origins.split(",").map((o) => o.trim())).refine((origins) => origins.every((o) => o.startsWith("https://")), { message: "All CORS origins must use HTTPS" }) },
|
|
348
|
+
apiKeys: get("API_KEYS").string().transform((keys) => keys.split(",").map((k) => k.trim())).pipe(z.array(z.string().min(32)))
|
|
349
|
+
})).parse();
|
|
350
|
+
expect(config).toEqual({
|
|
351
|
+
cors: { origins: ["https://example.com", "https://app.example.com"] },
|
|
352
|
+
apiKeys: ["key1234567890123456789012345678901", "key2345678901234567890123456789012"]
|
|
353
|
+
});
|
|
354
|
+
});
|
|
355
|
+
it("should fail refinements with descriptive errors", () => {
|
|
356
|
+
const env = { CORS_ORIGINS: "http://example.com,https://app.example.com" };
|
|
357
|
+
const parser = new EnvironmentParser(env);
|
|
358
|
+
expect(() => {
|
|
359
|
+
parser.create((get) => ({ cors: { origins: get("CORS_ORIGINS").string().transform((origins) => origins.split(",").map((o) => o.trim())).refine((origins) => origins.every((o) => o.startsWith("https://")), { message: "All CORS origins must use HTTPS" }) } })).parse();
|
|
360
|
+
}).toThrow(z.ZodError);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
describe("Type inference", () => {
|
|
364
|
+
it("should correctly infer string types", () => {
|
|
365
|
+
const env = { APP_NAME: "Test App" };
|
|
366
|
+
const parser = new EnvironmentParser(env);
|
|
367
|
+
const config = parser.create((get) => ({ appName: get("APP_NAME").string() })).parse();
|
|
368
|
+
const _typeCheck = true;
|
|
369
|
+
const _typeCheck2 = true;
|
|
370
|
+
expect(_typeCheck).toBe(true);
|
|
371
|
+
expect(_typeCheck2).toBe(true);
|
|
372
|
+
});
|
|
373
|
+
it("should correctly infer number types after transformation", () => {
|
|
374
|
+
const env = { PORT: "3000" };
|
|
375
|
+
const parser = new EnvironmentParser(env);
|
|
376
|
+
const config = parser.create((get) => ({ port: get("PORT").string().transform(Number) })).parse();
|
|
377
|
+
const _typeCheck = true;
|
|
378
|
+
const _typeCheck2 = true;
|
|
379
|
+
expect(_typeCheck).toBe(true);
|
|
380
|
+
expect(_typeCheck2).toBe(true);
|
|
381
|
+
});
|
|
382
|
+
it("should correctly infer boolean types after transformation", () => {
|
|
383
|
+
const env = { FEATURE_ENABLED: "true" };
|
|
384
|
+
const parser = new EnvironmentParser(env);
|
|
385
|
+
const config = parser.create((get) => ({ enabled: get("FEATURE_ENABLED").string().transform((v) => v === "true") })).parse();
|
|
386
|
+
const _typeCheck = true;
|
|
387
|
+
const _typeCheck2 = true;
|
|
388
|
+
expect(_typeCheck).toBe(true);
|
|
389
|
+
expect(_typeCheck2).toBe(true);
|
|
390
|
+
});
|
|
391
|
+
it("should correctly infer optional types", () => {
|
|
392
|
+
const env = { REQUIRED: "value" };
|
|
393
|
+
const parser = new EnvironmentParser(env);
|
|
394
|
+
const config = parser.create((get) => ({
|
|
395
|
+
required: get("REQUIRED").string(),
|
|
396
|
+
optional: get("OPTIONAL").string().optional()
|
|
397
|
+
})).parse();
|
|
398
|
+
});
|
|
399
|
+
it("should correctly infer nested object types", () => {
|
|
400
|
+
const env = {
|
|
401
|
+
DB_HOST: "localhost",
|
|
402
|
+
DB_PORT: "5432",
|
|
403
|
+
API_KEY: "secret"
|
|
404
|
+
};
|
|
405
|
+
const parser = new EnvironmentParser(env);
|
|
406
|
+
const config = parser.create((get) => ({
|
|
407
|
+
database: {
|
|
408
|
+
host: get("DB_HOST").string(),
|
|
409
|
+
port: get("DB_PORT").string().transform(Number)
|
|
410
|
+
},
|
|
411
|
+
api: { key: get("API_KEY").string() }
|
|
412
|
+
})).parse();
|
|
413
|
+
const _typeCheck = true;
|
|
414
|
+
const _typeCheck2 = true;
|
|
415
|
+
expect(_typeCheck).toBe(true);
|
|
416
|
+
expect(_typeCheck2).toBe(true);
|
|
417
|
+
});
|
|
418
|
+
});
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
//#endregion
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
Object.defineProperty(exports, '__toESM', {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return __toESM;
|
|
29
|
+
}
|
|
30
|
+
});
|
package/dist/index.cjs
CHANGED
package/dist/index.mjs
CHANGED
package/dist/sst.cjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const lodash_snakecase = require_chunk.__toESM(require("lodash.snakecase"));
|
|
3
|
+
|
|
4
|
+
//#region src/sst.ts
|
|
5
|
+
/**
|
|
6
|
+
* Converts a string to environment variable case format (UPPER_SNAKE_CASE).
|
|
7
|
+
* Numbers following underscores are preserved without the underscore.
|
|
8
|
+
*
|
|
9
|
+
* @param name - The string to convert
|
|
10
|
+
* @returns The converted string in environment variable format
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
14
|
+
* environmentCase('api_v2') // 'APIV2'
|
|
15
|
+
*/
|
|
16
|
+
function environmentCase(name) {
|
|
17
|
+
return (0, lodash_snakecase.default)(name).toUpperCase().replace(/_\d+/g, (r) => {
|
|
18
|
+
return r.replace("_", "");
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Enumeration of supported SST (Serverless Stack Toolkit) resource types.
|
|
23
|
+
* Used to identify and process different AWS and SST resources.
|
|
24
|
+
*/
|
|
25
|
+
let ResourceType = /* @__PURE__ */ function(ResourceType$1) {
|
|
26
|
+
ResourceType$1["ApiGatewayV2"] = "sst.aws.ApiGatewayV2";
|
|
27
|
+
ResourceType$1["Postgres"] = "sst.aws.Postgres";
|
|
28
|
+
ResourceType$1["Function"] = "sst.aws.Function";
|
|
29
|
+
ResourceType$1["Bucket"] = "sst.aws.Bucket";
|
|
30
|
+
ResourceType$1["Vpc"] = "sst.aws.Vpc";
|
|
31
|
+
ResourceType$1["Secret"] = "sst.sst.Secret";
|
|
32
|
+
ResourceType$1["SSTSecret"] = "sst:sst:Secret";
|
|
33
|
+
ResourceType$1["SSTFunction"] = "sst:sst:Function";
|
|
34
|
+
ResourceType$1["SSTApiGatewayV2"] = "sst:aws:ApiGatewayV2";
|
|
35
|
+
ResourceType$1["SSTPostgres"] = "sst:aws:Postgres";
|
|
36
|
+
ResourceType$1["SSTBucket"] = "sst:aws:Bucket";
|
|
37
|
+
return ResourceType$1;
|
|
38
|
+
}({});
|
|
39
|
+
/**
|
|
40
|
+
* Processes a Secret resource into environment variables.
|
|
41
|
+
*
|
|
42
|
+
* @param name - The resource name
|
|
43
|
+
* @param value - The Secret resource
|
|
44
|
+
* @returns Object with environment variable mappings
|
|
45
|
+
*/
|
|
46
|
+
const secret = (name, value) => ({ [environmentCase(name)]: value.value });
|
|
47
|
+
/**
|
|
48
|
+
* Processes a Postgres database resource into environment variables.
|
|
49
|
+
* Creates multiple environment variables for database connection details.
|
|
50
|
+
*
|
|
51
|
+
* @param key - The resource key
|
|
52
|
+
* @param value - The Postgres resource
|
|
53
|
+
* @returns Object with database connection environment variables
|
|
54
|
+
*/
|
|
55
|
+
const postgres = (key, value) => {
|
|
56
|
+
const prefix = `${environmentCase(key)}`;
|
|
57
|
+
return {
|
|
58
|
+
[`${prefix}_NAME`]: value.database,
|
|
59
|
+
[`${prefix}_HOST`]: value.host,
|
|
60
|
+
[`${prefix}_PASSWORD`]: value.password,
|
|
61
|
+
[`${prefix}_PORT`]: value.port,
|
|
62
|
+
[`${prefix}_USERNAME`]: value.username
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
/**
|
|
66
|
+
* Processes a Bucket resource into environment variables.
|
|
67
|
+
*
|
|
68
|
+
* @param name - The resource name
|
|
69
|
+
* @param value - The Bucket resource
|
|
70
|
+
* @returns Object with bucket name environment variable
|
|
71
|
+
*/
|
|
72
|
+
const bucket = (name, value) => {
|
|
73
|
+
const prefix = `${environmentCase(name)}`;
|
|
74
|
+
return { [`${prefix}_NAME`]: value.name };
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* No-operation processor for resources that don't require environment variables.
|
|
78
|
+
*
|
|
79
|
+
* @param name - The resource name (unused)
|
|
80
|
+
* @param value - The resource value (unused)
|
|
81
|
+
* @returns Empty object
|
|
82
|
+
*/
|
|
83
|
+
const noop = (name, value) => ({});
|
|
84
|
+
/**
|
|
85
|
+
* Map of resource types to their corresponding processor functions.
|
|
86
|
+
* Each processor converts resource data into environment variables.
|
|
87
|
+
*/
|
|
88
|
+
const processors = {
|
|
89
|
+
[ResourceType.ApiGatewayV2]: noop,
|
|
90
|
+
[ResourceType.Function]: noop,
|
|
91
|
+
[ResourceType.Vpc]: noop,
|
|
92
|
+
[ResourceType.Secret]: secret,
|
|
93
|
+
[ResourceType.Postgres]: postgres,
|
|
94
|
+
[ResourceType.Bucket]: bucket,
|
|
95
|
+
[ResourceType.SSTSecret]: secret,
|
|
96
|
+
[ResourceType.SSTBucket]: bucket,
|
|
97
|
+
[ResourceType.SSTFunction]: noop,
|
|
98
|
+
[ResourceType.SSTPostgres]: postgres,
|
|
99
|
+
[ResourceType.SSTApiGatewayV2]: noop
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Normalizes SST resources and plain strings into environment variables.
|
|
103
|
+
* Processes resources based on their type and converts names to environment case.
|
|
104
|
+
*
|
|
105
|
+
* @param record - Object containing resources and/or string values
|
|
106
|
+
* @returns Normalized environment variables object
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* normalizeResourceEnv({
|
|
110
|
+
* apiUrl: 'https://api.example.com',
|
|
111
|
+
* database: { type: ResourceType.Postgres, ... }
|
|
112
|
+
* })
|
|
113
|
+
*/
|
|
114
|
+
function normalizeResourceEnv(record) {
|
|
115
|
+
const env = {};
|
|
116
|
+
for (const [k, value] of Object.entries(record)) {
|
|
117
|
+
if (typeof value === "string") {
|
|
118
|
+
env[environmentCase(k)] = value;
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const processor = processors[value.type];
|
|
122
|
+
if (processor) Object.assign(env, processor(k, value));
|
|
123
|
+
else console.warn(`No processor found for resource type: `, { value });
|
|
124
|
+
}
|
|
125
|
+
return env;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
exports.ResourceType = ResourceType;
|
|
130
|
+
exports.environmentCase = environmentCase;
|
|
131
|
+
exports.normalizeResourceEnv = normalizeResourceEnv;
|