@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,83 @@
|
|
|
1
|
+
import snakecase from "lodash.snakecase";
|
|
2
|
+
|
|
3
|
+
//#region src/EnvironmentBuilder.ts
|
|
4
|
+
/**
|
|
5
|
+
* Converts a string to environment variable case format (UPPER_SNAKE_CASE).
|
|
6
|
+
* Numbers following underscores are preserved without the underscore.
|
|
7
|
+
*
|
|
8
|
+
* @param name - The string to convert
|
|
9
|
+
* @returns The converted string in environment variable format
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
13
|
+
* environmentCase('apiV2') // 'APIV2'
|
|
14
|
+
*/
|
|
15
|
+
function environmentCase(name) {
|
|
16
|
+
return snakecase(name).toUpperCase().replace(/_\d+/g, (r) => {
|
|
17
|
+
return r.replace("_", "");
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* A generic, extensible class for building environment variables from
|
|
22
|
+
* objects with type-discriminated values.
|
|
23
|
+
*
|
|
24
|
+
* @template TRecord - The input record type for type inference
|
|
25
|
+
* @template TResolvers - The resolvers type (defaults to TypedResolvers<TRecord>)
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* const env = new EnvironmentBuilder(
|
|
30
|
+
* {
|
|
31
|
+
* apiKey: { type: 'secret', value: 'xyz' },
|
|
32
|
+
* appName: 'my-app'
|
|
33
|
+
* },
|
|
34
|
+
* {
|
|
35
|
+
* // `value` is typed as { value: string } (without `type`)
|
|
36
|
+
* secret: (key, value) => ({ [key]: value.value }),
|
|
37
|
+
* }
|
|
38
|
+
* ).build();
|
|
39
|
+
* // { API_KEY: 'xyz', APP_NAME: 'my-app' }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
var EnvironmentBuilder = class {
|
|
43
|
+
record;
|
|
44
|
+
resolvers;
|
|
45
|
+
options;
|
|
46
|
+
constructor(record, resolvers, options = {}) {
|
|
47
|
+
this.record = record;
|
|
48
|
+
this.resolvers = resolvers;
|
|
49
|
+
this.options = { onUnmatchedValue: options.onUnmatchedValue ?? ((key, value) => {
|
|
50
|
+
console.warn(`No resolver found for key "${key}":`, { value });
|
|
51
|
+
}) };
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Build environment variables from the input record.
|
|
55
|
+
*
|
|
56
|
+
* - Plain string values are passed through with key transformation
|
|
57
|
+
* - Object values with a `type` property are matched against resolvers
|
|
58
|
+
* - Resolvers receive values without the `type` key
|
|
59
|
+
* - Only root-level keys are transformed to UPPER_SNAKE_CASE
|
|
60
|
+
*
|
|
61
|
+
* @returns A record of environment variables
|
|
62
|
+
*/
|
|
63
|
+
build() {
|
|
64
|
+
const env = {};
|
|
65
|
+
for (const [key, value] of Object.entries(this.record)) {
|
|
66
|
+
if (typeof value === "string") {
|
|
67
|
+
env[environmentCase(key)] = value;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const { type,...rest } = value;
|
|
71
|
+
const resolver = this.resolvers[type];
|
|
72
|
+
if (resolver) {
|
|
73
|
+
const resolved = resolver(key, rest);
|
|
74
|
+
for (const [resolvedKey, resolvedValue] of Object.entries(resolved)) env[environmentCase(resolvedKey)] = resolvedValue;
|
|
75
|
+
} else this.options.onUnmatchedValue(key, value);
|
|
76
|
+
}
|
|
77
|
+
return env;
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
export { EnvironmentBuilder, environmentCase };
|
|
83
|
+
//# sourceMappingURL=EnvironmentBuilder-DfmYRBm-.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-DfmYRBm-.mjs","names":["name: string","record: TRecord","resolvers: TResolvers","options: EnvironmentBuilderOptions","env: EnvRecord"],"sources":["../src/EnvironmentBuilder.ts"],"sourcesContent":["import snakecase from 'lodash.snakecase';\n\n/**\n * Converts a string to environment variable case format (UPPER_SNAKE_CASE).\n * Numbers following underscores are preserved without the underscore.\n *\n * @param name - The string to convert\n * @returns The converted string in environment variable format\n *\n * @example\n * environmentCase('myVariable') // 'MY_VARIABLE'\n * environmentCase('apiV2') // 'APIV2'\n */\nexport function environmentCase(name: string): string {\n return snakecase(name)\n .toUpperCase()\n .replace(/_\\d+/g, (r) => {\n return r.replace('_', '');\n });\n}\n\n/**\n * A record of environment variable names to their values.\n * Values can be primitives or nested records.\n */\nexport interface EnvRecord {\n [key: string]: EnvValue;\n}\n\n/**\n * Represents a value that can be stored in an environment record.\n * Can be a primitive value or a nested record of environment values.\n */\nexport type EnvValue = string | number | boolean | EnvRecord;\n\n/**\n * A resolver function that converts a typed value into environment variables.\n *\n * @template T - The type of value this resolver handles (without the `type` key)\n * @param key - The key name from the input record\n * @param value - The value to resolve (without the `type` key)\n * @returns A record of environment variable names to their values\n */\nexport type EnvironmentResolver<T = any> = (key: string, value: T) => EnvRecord;\n\n/**\n * A map of type discriminator strings to their resolver functions.\n */\nexport type Resolvers = Record<string, EnvironmentResolver<any>>;\n\n/**\n * Options for configuring the EnvironmentBuilder.\n */\nexport interface EnvironmentBuilderOptions {\n /**\n * Handler called when a value's type doesn't match any registered resolver.\n * Defaults to console.warn.\n */\n onUnmatchedValue?: (key: string, value: unknown) => void;\n}\n\n/**\n * Input value type - either a string or an object with a `type` discriminator.\n */\nexport type InputValue = string | { type: string; [key: string]: unknown };\n\n/**\n * Base type for typed input values with a specific type discriminator.\n */\nexport type TypedInputValue<TType extends string = string> = {\n type: TType;\n [key: string]: unknown;\n};\n\n/**\n * Extracts the `type` string value from an input value.\n */\ntype ExtractType<T> = T extends { type: infer U extends string } ? U : never;\n\n/**\n * Removes the `type` key from an object type.\n */\ntype OmitType<T> = T extends { type: string } ? Omit<T, 'type'> : never;\n\n/**\n * Extracts all unique `type` values from a record (excluding plain strings).\n */\ntype AllTypeValues<TRecord extends Record<string, InputValue>> = {\n [K in keyof TRecord]: ExtractType<TRecord[K]>;\n}[keyof TRecord];\n\n/**\n * For a given type value, finds the corresponding value type (without `type` key).\n */\ntype ValueForType<\n TRecord extends Record<string, InputValue>,\n TType extends string,\n> = {\n [K in keyof TRecord]: TRecord[K] extends { type: TType }\n ? OmitType<TRecord[K]>\n : never;\n}[keyof TRecord];\n\n/**\n * Generates typed resolvers based on the input record.\n * Keys are the `type` values, values are resolver functions receiving the value without `type`.\n */\nexport type TypedResolvers<TRecord extends Record<string, InputValue>> = {\n [TType in AllTypeValues<TRecord>]: EnvironmentResolver<\n ValueForType<TRecord, TType>\n >;\n};\n\n/**\n * A generic, extensible class for building environment variables from\n * objects with type-discriminated values.\n *\n * @template TRecord - The input record type for type inference\n * @template TResolvers - The resolvers type (defaults to TypedResolvers<TRecord>)\n *\n * @example\n * ```typescript\n * const env = new EnvironmentBuilder(\n * {\n * apiKey: { type: 'secret', value: 'xyz' },\n * appName: 'my-app'\n * },\n * {\n * // `value` is typed as { value: string } (without `type`)\n * secret: (key, value) => ({ [key]: value.value }),\n * }\n * ).build();\n * // { API_KEY: 'xyz', APP_NAME: 'my-app' }\n * ```\n */\nexport class EnvironmentBuilder<\n TRecord extends Record<string, InputValue> = Record<string, InputValue>,\n TResolvers extends Resolvers = TypedResolvers<TRecord>,\n> {\n private readonly record: TRecord;\n private readonly resolvers: TResolvers;\n private readonly options: Required<EnvironmentBuilderOptions>;\n\n constructor(\n record: TRecord,\n resolvers: TResolvers,\n options: EnvironmentBuilderOptions = {},\n ) {\n this.record = record;\n this.resolvers = resolvers;\n this.options = {\n onUnmatchedValue:\n options.onUnmatchedValue ??\n ((key, value) => {\n console.warn(`No resolver found for key \"${key}\":`, { value });\n }),\n };\n }\n\n /**\n * Build environment variables from the input record.\n *\n * - Plain string values are passed through with key transformation\n * - Object values with a `type` property are matched against resolvers\n * - Resolvers receive values without the `type` key\n * - Only root-level keys are transformed to UPPER_SNAKE_CASE\n *\n * @returns A record of environment variables\n */\n build(): EnvRecord {\n const env: EnvRecord = {};\n\n for (const [key, value] of Object.entries(this.record)) {\n // Handle plain string values\n if (typeof value === 'string') {\n env[environmentCase(key)] = value;\n continue;\n }\n\n // Handle objects with type discriminator\n const { type, ...rest } = value;\n const resolver = this.resolvers[type];\n if (resolver) {\n const resolved = resolver(key, rest);\n // Transform only root-level keys from resolver output\n for (const [resolvedKey, resolvedValue] of Object.entries(resolved)) {\n env[environmentCase(resolvedKey)] = resolvedValue;\n }\n } else {\n this.options.onUnmatchedValue(key, value);\n }\n }\n\n return env;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAgB,gBAAgBA,MAAsB;AACpD,QAAO,UAAU,KAAK,CACnB,aAAa,CACb,QAAQ,SAAS,CAAC,MAAM;AACvB,SAAO,EAAE,QAAQ,KAAK,GAAG;CAC1B,EAAC;AACL;;;;;;;;;;;;;;;;;;;;;;;AAoHD,IAAa,qBAAb,MAGE;CACA,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YACEC,QACAC,WACAC,UAAqC,CAAE,GACvC;AACA,OAAK,SAAS;AACd,OAAK,YAAY;AACjB,OAAK,UAAU,EACb,kBACE,QAAQ,qBACP,CAAC,KAAK,UAAU;AACf,WAAQ,MAAM,6BAA6B,IAAI,KAAK,EAAE,MAAO,EAAC;EAC/D,GACJ;CACF;;;;;;;;;;;CAYD,QAAmB;EACjB,MAAMC,MAAiB,CAAE;AAEzB,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,KAAK,OAAO,EAAE;AAEtD,cAAW,UAAU,UAAU;AAC7B,QAAI,gBAAgB,IAAI,IAAI;AAC5B;GACD;GAGD,MAAM,EAAE,KAAM,GAAG,MAAM,GAAG;GAC1B,MAAM,WAAW,KAAK,UAAU;AAChC,OAAI,UAAU;IACZ,MAAM,WAAW,SAAS,KAAK,KAAK;AAEpC,SAAK,MAAM,CAAC,aAAa,cAAc,IAAI,OAAO,QAAQ,SAAS,CACjE,KAAI,gBAAgB,YAAY,IAAI;GAEvC,MACC,MAAK,QAAQ,iBAAiB,KAAK,MAAM;EAE5C;AAED,SAAO;CACR;AACF"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const lodash_snakecase = require_chunk.__toESM(require("lodash.snakecase"));
|
|
3
|
+
|
|
4
|
+
//#region src/EnvironmentBuilder.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('apiV2') // '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
|
+
* A generic, extensible class for building environment variables from
|
|
23
|
+
* objects with type-discriminated values.
|
|
24
|
+
*
|
|
25
|
+
* @template TRecord - The input record type for type inference
|
|
26
|
+
* @template TResolvers - The resolvers type (defaults to TypedResolvers<TRecord>)
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* const env = new EnvironmentBuilder(
|
|
31
|
+
* {
|
|
32
|
+
* apiKey: { type: 'secret', value: 'xyz' },
|
|
33
|
+
* appName: 'my-app'
|
|
34
|
+
* },
|
|
35
|
+
* {
|
|
36
|
+
* // `value` is typed as { value: string } (without `type`)
|
|
37
|
+
* secret: (key, value) => ({ [key]: value.value }),
|
|
38
|
+
* }
|
|
39
|
+
* ).build();
|
|
40
|
+
* // { API_KEY: 'xyz', APP_NAME: 'my-app' }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
var EnvironmentBuilder = class {
|
|
44
|
+
record;
|
|
45
|
+
resolvers;
|
|
46
|
+
options;
|
|
47
|
+
constructor(record, resolvers, options = {}) {
|
|
48
|
+
this.record = record;
|
|
49
|
+
this.resolvers = resolvers;
|
|
50
|
+
this.options = { onUnmatchedValue: options.onUnmatchedValue ?? ((key, value) => {
|
|
51
|
+
console.warn(`No resolver found for key "${key}":`, { value });
|
|
52
|
+
}) };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build environment variables from the input record.
|
|
56
|
+
*
|
|
57
|
+
* - Plain string values are passed through with key transformation
|
|
58
|
+
* - Object values with a `type` property are matched against resolvers
|
|
59
|
+
* - Resolvers receive values without the `type` key
|
|
60
|
+
* - Only root-level keys are transformed to UPPER_SNAKE_CASE
|
|
61
|
+
*
|
|
62
|
+
* @returns A record of environment variables
|
|
63
|
+
*/
|
|
64
|
+
build() {
|
|
65
|
+
const env = {};
|
|
66
|
+
for (const [key, value] of Object.entries(this.record)) {
|
|
67
|
+
if (typeof value === "string") {
|
|
68
|
+
env[environmentCase(key)] = value;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const { type,...rest } = value;
|
|
72
|
+
const resolver = this.resolvers[type];
|
|
73
|
+
if (resolver) {
|
|
74
|
+
const resolved = resolver(key, rest);
|
|
75
|
+
for (const [resolvedKey, resolvedValue] of Object.entries(resolved)) env[environmentCase(resolvedKey)] = resolvedValue;
|
|
76
|
+
} else this.options.onUnmatchedValue(key, value);
|
|
77
|
+
}
|
|
78
|
+
return env;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
Object.defineProperty(exports, 'EnvironmentBuilder', {
|
|
84
|
+
enumerable: true,
|
|
85
|
+
get: function () {
|
|
86
|
+
return EnvironmentBuilder;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
Object.defineProperty(exports, 'environmentCase', {
|
|
90
|
+
enumerable: true,
|
|
91
|
+
get: function () {
|
|
92
|
+
return environmentCase;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
//# sourceMappingURL=EnvironmentBuilder-W2wku49g.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-W2wku49g.cjs","names":["name: string","record: TRecord","resolvers: TResolvers","options: EnvironmentBuilderOptions","env: EnvRecord"],"sources":["../src/EnvironmentBuilder.ts"],"sourcesContent":["import snakecase from 'lodash.snakecase';\n\n/**\n * Converts a string to environment variable case format (UPPER_SNAKE_CASE).\n * Numbers following underscores are preserved without the underscore.\n *\n * @param name - The string to convert\n * @returns The converted string in environment variable format\n *\n * @example\n * environmentCase('myVariable') // 'MY_VARIABLE'\n * environmentCase('apiV2') // 'APIV2'\n */\nexport function environmentCase(name: string): string {\n return snakecase(name)\n .toUpperCase()\n .replace(/_\\d+/g, (r) => {\n return r.replace('_', '');\n });\n}\n\n/**\n * A record of environment variable names to their values.\n * Values can be primitives or nested records.\n */\nexport interface EnvRecord {\n [key: string]: EnvValue;\n}\n\n/**\n * Represents a value that can be stored in an environment record.\n * Can be a primitive value or a nested record of environment values.\n */\nexport type EnvValue = string | number | boolean | EnvRecord;\n\n/**\n * A resolver function that converts a typed value into environment variables.\n *\n * @template T - The type of value this resolver handles (without the `type` key)\n * @param key - The key name from the input record\n * @param value - The value to resolve (without the `type` key)\n * @returns A record of environment variable names to their values\n */\nexport type EnvironmentResolver<T = any> = (key: string, value: T) => EnvRecord;\n\n/**\n * A map of type discriminator strings to their resolver functions.\n */\nexport type Resolvers = Record<string, EnvironmentResolver<any>>;\n\n/**\n * Options for configuring the EnvironmentBuilder.\n */\nexport interface EnvironmentBuilderOptions {\n /**\n * Handler called when a value's type doesn't match any registered resolver.\n * Defaults to console.warn.\n */\n onUnmatchedValue?: (key: string, value: unknown) => void;\n}\n\n/**\n * Input value type - either a string or an object with a `type` discriminator.\n */\nexport type InputValue = string | { type: string; [key: string]: unknown };\n\n/**\n * Base type for typed input values with a specific type discriminator.\n */\nexport type TypedInputValue<TType extends string = string> = {\n type: TType;\n [key: string]: unknown;\n};\n\n/**\n * Extracts the `type` string value from an input value.\n */\ntype ExtractType<T> = T extends { type: infer U extends string } ? U : never;\n\n/**\n * Removes the `type` key from an object type.\n */\ntype OmitType<T> = T extends { type: string } ? Omit<T, 'type'> : never;\n\n/**\n * Extracts all unique `type` values from a record (excluding plain strings).\n */\ntype AllTypeValues<TRecord extends Record<string, InputValue>> = {\n [K in keyof TRecord]: ExtractType<TRecord[K]>;\n}[keyof TRecord];\n\n/**\n * For a given type value, finds the corresponding value type (without `type` key).\n */\ntype ValueForType<\n TRecord extends Record<string, InputValue>,\n TType extends string,\n> = {\n [K in keyof TRecord]: TRecord[K] extends { type: TType }\n ? OmitType<TRecord[K]>\n : never;\n}[keyof TRecord];\n\n/**\n * Generates typed resolvers based on the input record.\n * Keys are the `type` values, values are resolver functions receiving the value without `type`.\n */\nexport type TypedResolvers<TRecord extends Record<string, InputValue>> = {\n [TType in AllTypeValues<TRecord>]: EnvironmentResolver<\n ValueForType<TRecord, TType>\n >;\n};\n\n/**\n * A generic, extensible class for building environment variables from\n * objects with type-discriminated values.\n *\n * @template TRecord - The input record type for type inference\n * @template TResolvers - The resolvers type (defaults to TypedResolvers<TRecord>)\n *\n * @example\n * ```typescript\n * const env = new EnvironmentBuilder(\n * {\n * apiKey: { type: 'secret', value: 'xyz' },\n * appName: 'my-app'\n * },\n * {\n * // `value` is typed as { value: string } (without `type`)\n * secret: (key, value) => ({ [key]: value.value }),\n * }\n * ).build();\n * // { API_KEY: 'xyz', APP_NAME: 'my-app' }\n * ```\n */\nexport class EnvironmentBuilder<\n TRecord extends Record<string, InputValue> = Record<string, InputValue>,\n TResolvers extends Resolvers = TypedResolvers<TRecord>,\n> {\n private readonly record: TRecord;\n private readonly resolvers: TResolvers;\n private readonly options: Required<EnvironmentBuilderOptions>;\n\n constructor(\n record: TRecord,\n resolvers: TResolvers,\n options: EnvironmentBuilderOptions = {},\n ) {\n this.record = record;\n this.resolvers = resolvers;\n this.options = {\n onUnmatchedValue:\n options.onUnmatchedValue ??\n ((key, value) => {\n console.warn(`No resolver found for key \"${key}\":`, { value });\n }),\n };\n }\n\n /**\n * Build environment variables from the input record.\n *\n * - Plain string values are passed through with key transformation\n * - Object values with a `type` property are matched against resolvers\n * - Resolvers receive values without the `type` key\n * - Only root-level keys are transformed to UPPER_SNAKE_CASE\n *\n * @returns A record of environment variables\n */\n build(): EnvRecord {\n const env: EnvRecord = {};\n\n for (const [key, value] of Object.entries(this.record)) {\n // Handle plain string values\n if (typeof value === 'string') {\n env[environmentCase(key)] = value;\n continue;\n }\n\n // Handle objects with type discriminator\n const { type, ...rest } = value;\n const resolver = this.resolvers[type];\n if (resolver) {\n const resolved = resolver(key, rest);\n // Transform only root-level keys from resolver output\n for (const [resolvedKey, resolvedValue] of Object.entries(resolved)) {\n env[environmentCase(resolvedKey)] = resolvedValue;\n }\n } else {\n this.options.onUnmatchedValue(key, value);\n }\n }\n\n return env;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,SAAgB,gBAAgBA,MAAsB;AACpD,QAAO,8BAAU,KAAK,CACnB,aAAa,CACb,QAAQ,SAAS,CAAC,MAAM;AACvB,SAAO,EAAE,QAAQ,KAAK,GAAG;CAC1B,EAAC;AACL;;;;;;;;;;;;;;;;;;;;;;;AAoHD,IAAa,qBAAb,MAGE;CACA,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YACEC,QACAC,WACAC,UAAqC,CAAE,GACvC;AACA,OAAK,SAAS;AACd,OAAK,YAAY;AACjB,OAAK,UAAU,EACb,kBACE,QAAQ,qBACP,CAAC,KAAK,UAAU;AACf,WAAQ,MAAM,6BAA6B,IAAI,KAAK,EAAE,MAAO,EAAC;EAC/D,GACJ;CACF;;;;;;;;;;;CAYD,QAAmB;EACjB,MAAMC,MAAiB,CAAE;AAEzB,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,KAAK,OAAO,EAAE;AAEtD,cAAW,UAAU,UAAU;AAC7B,QAAI,gBAAgB,IAAI,IAAI;AAC5B;GACD;GAGD,MAAM,EAAE,KAAM,GAAG,MAAM,GAAG;GAC1B,MAAM,WAAW,KAAK,UAAU;AAChC,OAAI,UAAU;IACZ,MAAM,WAAW,SAAS,KAAK,KAAK;AAEpC,SAAK,MAAM,CAAC,aAAa,cAAc,IAAI,OAAO,QAAQ,SAAS,CACjE,KAAI,gBAAgB,YAAY,IAAI;GAEvC,MACC,MAAK,QAAQ,iBAAiB,KAAK,MAAM;EAE5C;AAED,SAAO;CACR;AACF"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
//#region src/EnvironmentBuilder.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Converts a string to environment variable case format (UPPER_SNAKE_CASE).
|
|
4
|
+
* Numbers following underscores are preserved without the underscore.
|
|
5
|
+
*
|
|
6
|
+
* @param name - The string to convert
|
|
7
|
+
* @returns The converted string in environment variable format
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
11
|
+
* environmentCase('apiV2') // 'APIV2'
|
|
12
|
+
*/
|
|
13
|
+
declare function environmentCase(name: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* A record of environment variable names to their values.
|
|
16
|
+
* Values can be primitives or nested records.
|
|
17
|
+
*/
|
|
18
|
+
interface EnvRecord {
|
|
19
|
+
[key: string]: EnvValue;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Represents a value that can be stored in an environment record.
|
|
23
|
+
* Can be a primitive value or a nested record of environment values.
|
|
24
|
+
*/
|
|
25
|
+
type EnvValue = string | number | boolean | EnvRecord;
|
|
26
|
+
/**
|
|
27
|
+
* A resolver function that converts a typed value into environment variables.
|
|
28
|
+
*
|
|
29
|
+
* @template T - The type of value this resolver handles (without the `type` key)
|
|
30
|
+
* @param key - The key name from the input record
|
|
31
|
+
* @param value - The value to resolve (without the `type` key)
|
|
32
|
+
* @returns A record of environment variable names to their values
|
|
33
|
+
*/
|
|
34
|
+
type EnvironmentResolver<T = any> = (key: string, value: T) => EnvRecord;
|
|
35
|
+
/**
|
|
36
|
+
* A map of type discriminator strings to their resolver functions.
|
|
37
|
+
*/
|
|
38
|
+
type Resolvers = Record<string, EnvironmentResolver<any>>;
|
|
39
|
+
/**
|
|
40
|
+
* Options for configuring the EnvironmentBuilder.
|
|
41
|
+
*/
|
|
42
|
+
interface EnvironmentBuilderOptions {
|
|
43
|
+
/**
|
|
44
|
+
* Handler called when a value's type doesn't match any registered resolver.
|
|
45
|
+
* Defaults to console.warn.
|
|
46
|
+
*/
|
|
47
|
+
onUnmatchedValue?: (key: string, value: unknown) => void;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Input value type - either a string or an object with a `type` discriminator.
|
|
51
|
+
*/
|
|
52
|
+
type InputValue = string | {
|
|
53
|
+
type: string;
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Base type for typed input values with a specific type discriminator.
|
|
58
|
+
*/
|
|
59
|
+
type TypedInputValue<TType extends string = string> = {
|
|
60
|
+
type: TType;
|
|
61
|
+
[key: string]: unknown;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Extracts the `type` string value from an input value.
|
|
65
|
+
*/
|
|
66
|
+
type ExtractType<T> = T extends {
|
|
67
|
+
type: infer U extends string;
|
|
68
|
+
} ? U : never;
|
|
69
|
+
/**
|
|
70
|
+
* Removes the `type` key from an object type.
|
|
71
|
+
*/
|
|
72
|
+
type OmitType<T> = T extends {
|
|
73
|
+
type: string;
|
|
74
|
+
} ? Omit<T, 'type'> : never;
|
|
75
|
+
/**
|
|
76
|
+
* Extracts all unique `type` values from a record (excluding plain strings).
|
|
77
|
+
*/
|
|
78
|
+
type AllTypeValues<TRecord extends Record<string, InputValue>> = { [K in keyof TRecord]: ExtractType<TRecord[K]> }[keyof TRecord];
|
|
79
|
+
/**
|
|
80
|
+
* For a given type value, finds the corresponding value type (without `type` key).
|
|
81
|
+
*/
|
|
82
|
+
type ValueForType<TRecord extends Record<string, InputValue>, TType extends string> = { [K in keyof TRecord]: TRecord[K] extends {
|
|
83
|
+
type: TType;
|
|
84
|
+
} ? OmitType<TRecord[K]> : never }[keyof TRecord];
|
|
85
|
+
/**
|
|
86
|
+
* Generates typed resolvers based on the input record.
|
|
87
|
+
* Keys are the `type` values, values are resolver functions receiving the value without `type`.
|
|
88
|
+
*/
|
|
89
|
+
type TypedResolvers<TRecord extends Record<string, InputValue>> = { [TType in AllTypeValues<TRecord>]: EnvironmentResolver<ValueForType<TRecord, TType>> };
|
|
90
|
+
/**
|
|
91
|
+
* A generic, extensible class for building environment variables from
|
|
92
|
+
* objects with type-discriminated values.
|
|
93
|
+
*
|
|
94
|
+
* @template TRecord - The input record type for type inference
|
|
95
|
+
* @template TResolvers - The resolvers type (defaults to TypedResolvers<TRecord>)
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* ```typescript
|
|
99
|
+
* const env = new EnvironmentBuilder(
|
|
100
|
+
* {
|
|
101
|
+
* apiKey: { type: 'secret', value: 'xyz' },
|
|
102
|
+
* appName: 'my-app'
|
|
103
|
+
* },
|
|
104
|
+
* {
|
|
105
|
+
* // `value` is typed as { value: string } (without `type`)
|
|
106
|
+
* secret: (key, value) => ({ [key]: value.value }),
|
|
107
|
+
* }
|
|
108
|
+
* ).build();
|
|
109
|
+
* // { API_KEY: 'xyz', APP_NAME: 'my-app' }
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
declare class EnvironmentBuilder<TRecord extends Record<string, InputValue> = Record<string, InputValue>, TResolvers extends Resolvers = TypedResolvers<TRecord>> {
|
|
113
|
+
private readonly record;
|
|
114
|
+
private readonly resolvers;
|
|
115
|
+
private readonly options;
|
|
116
|
+
constructor(record: TRecord, resolvers: TResolvers, options?: EnvironmentBuilderOptions);
|
|
117
|
+
/**
|
|
118
|
+
* Build environment variables from the input record.
|
|
119
|
+
*
|
|
120
|
+
* - Plain string values are passed through with key transformation
|
|
121
|
+
* - Object values with a `type` property are matched against resolvers
|
|
122
|
+
* - Resolvers receive values without the `type` key
|
|
123
|
+
* - Only root-level keys are transformed to UPPER_SNAKE_CASE
|
|
124
|
+
*
|
|
125
|
+
* @returns A record of environment variables
|
|
126
|
+
*/
|
|
127
|
+
build(): EnvRecord;
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
export { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase };
|
|
131
|
+
//# sourceMappingURL=EnvironmentBuilder-Xuf2Dd9u.d.cts.map
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase } from "./EnvironmentBuilder-Xuf2Dd9u.cjs";
|
|
2
|
+
export { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase } from "./EnvironmentBuilder-DHfDXJUm.mjs";
|
|
2
|
+
export { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase };
|
|
@@ -15,9 +15,11 @@ var ConfigParser = class {
|
|
|
15
15
|
* Creates a new ConfigParser instance.
|
|
16
16
|
*
|
|
17
17
|
* @param config - The configuration object to parse
|
|
18
|
+
* @param envVars - Set of environment variable names that were accessed
|
|
18
19
|
*/
|
|
19
|
-
constructor(config) {
|
|
20
|
+
constructor(config, envVars = /* @__PURE__ */ new Set()) {
|
|
20
21
|
this.config = config;
|
|
22
|
+
this.envVars = envVars;
|
|
21
23
|
}
|
|
22
24
|
/**
|
|
23
25
|
* Parses the config object and validates it against the Zod schemas
|
|
@@ -46,6 +48,25 @@ var ConfigParser = class {
|
|
|
46
48
|
if (errors.length > 0) throw new zod_v4.z.ZodError(errors);
|
|
47
49
|
return parsedConfig;
|
|
48
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns an array of environment variable names that were accessed during config creation.
|
|
53
|
+
* This is useful for deployment and configuration management to know which env vars are required.
|
|
54
|
+
*
|
|
55
|
+
* @returns Array of environment variable names, sorted alphabetically
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const config = envParser.create((get) => ({
|
|
60
|
+
* dbUrl: get('DATABASE_URL').string(),
|
|
61
|
+
* port: get('PORT').number()
|
|
62
|
+
* }));
|
|
63
|
+
*
|
|
64
|
+
* config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
getEnvironmentVariables() {
|
|
68
|
+
return Array.from(this.envVars).sort();
|
|
69
|
+
}
|
|
49
70
|
};
|
|
50
71
|
/**
|
|
51
72
|
* Parses environment variables with type-safe validation using Zod schemas.
|
|
@@ -67,6 +88,10 @@ var ConfigParser = class {
|
|
|
67
88
|
* ```
|
|
68
89
|
*/
|
|
69
90
|
var EnvironmentParser = class {
|
|
91
|
+
/**
|
|
92
|
+
* Set to track which environment variable names have been accessed
|
|
93
|
+
*/
|
|
94
|
+
accessedVars = /* @__PURE__ */ new Set();
|
|
70
95
|
/**
|
|
71
96
|
* Creates a new EnvironmentParser instance.
|
|
72
97
|
*
|
|
@@ -134,6 +159,7 @@ var EnvironmentParser = class {
|
|
|
134
159
|
* @returns A proxied Zod object with wrapped schema creators
|
|
135
160
|
*/
|
|
136
161
|
getZodGetter = (name) => {
|
|
162
|
+
this.accessedVars.add(name);
|
|
137
163
|
return new Proxy({ ...zod_v4.z }, { get: (target, prop) => {
|
|
138
164
|
const value = target[prop];
|
|
139
165
|
if (typeof value === "function") return (...args) => {
|
|
@@ -159,7 +185,23 @@ var EnvironmentParser = class {
|
|
|
159
185
|
*/
|
|
160
186
|
create(builder) {
|
|
161
187
|
const config = builder(this.getZodGetter);
|
|
162
|
-
return new ConfigParser(config);
|
|
188
|
+
return new ConfigParser(config, this.accessedVars);
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Returns an array of environment variable names that were accessed via the getter.
|
|
192
|
+
* This is useful for build-time analysis to determine which env vars a service needs.
|
|
193
|
+
*
|
|
194
|
+
* @returns Array of environment variable names, sorted alphabetically
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```typescript
|
|
198
|
+
* const sniffer = new EnvironmentParser({});
|
|
199
|
+
* service.register(sniffer);
|
|
200
|
+
* const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
getEnvironmentVariables() {
|
|
204
|
+
return Array.from(this.accessedVars).sort();
|
|
163
205
|
}
|
|
164
206
|
};
|
|
165
207
|
|
|
@@ -175,4 +217,5 @@ Object.defineProperty(exports, 'EnvironmentParser', {
|
|
|
175
217
|
get: function () {
|
|
176
218
|
return EnvironmentParser;
|
|
177
219
|
}
|
|
178
|
-
});
|
|
220
|
+
});
|
|
221
|
+
//# sourceMappingURL=EnvironmentParser-Bt246UeP.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-Bt246UeP.cjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","z","schema: z.ZodType","name: string","issue: z.core.$ZodIssue","builder: (get: EnvFetcher) => TReturn"],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":["import get from 'lodash.get';\nimport set from 'lodash.set';\nimport { z } from 'zod/v4';\n\n/**\n * Parses and validates configuration objects against Zod schemas.\n * Handles nested configurations and aggregates validation errors.\n *\n * @template TResponse - The shape of the configuration object\n */\nexport class ConfigParser<TResponse extends EmptyObject> {\n /**\n * Creates a new ConfigParser instance.\n *\n * @param config - The configuration object to parse\n * @param envVars - Set of environment variable names that were accessed\n */\n constructor(\n private readonly config: TResponse,\n private readonly envVars: Set<string> = new Set(),\n ) {}\n /**\n * Parses the config object and validates it against the Zod schemas\n * @returns The parsed config object\n */\n parse(): InferConfig<TResponse> {\n const errors: z.core.$ZodIssue[] = [];\n\n const parseDeep = <T>(config: T, path: string[] = []) => {\n const result: EmptyObject = {};\n\n if (config && typeof config !== 'object') {\n return config;\n }\n\n for (const key in config) {\n const schema = config[key];\n const currentPath = [...path, key];\n\n if (schema instanceof z.ZodType) {\n const parsed = schema.safeParse(undefined);\n if (parsed.success) {\n set(result, key, parsed.data);\n } else {\n // If the schema is invalid, assign the error\n errors.push(\n ...parsed.error.issues.map((issue) => ({\n ...issue,\n path: [...currentPath, ...(issue.path as string[])],\n })),\n );\n }\n } else if (schema) {\n set(result, key, parseDeep(schema as EmptyObject, currentPath));\n }\n }\n\n return result;\n };\n\n const parsedConfig = parseDeep(\n this.config,\n ) as unknown as InferConfig<TResponse>;\n\n if (errors.length > 0) {\n // If there are errors, throw them\n throw new z.ZodError(errors);\n }\n\n return parsedConfig;\n }\n\n /**\n * Returns an array of environment variable names that were accessed during config creation.\n * This is useful for deployment and configuration management to know which env vars are required.\n *\n * @returns Array of environment variable names, sorted alphabetically\n *\n * @example\n * ```typescript\n * const config = envParser.create((get) => ({\n * dbUrl: get('DATABASE_URL').string(),\n * port: get('PORT').number()\n * }));\n *\n * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n * ```\n */\n getEnvironmentVariables(): string[] {\n return Array.from(this.envVars).sort();\n }\n}\n\n/**\n * Parses environment variables with type-safe validation using Zod schemas.\n * Provides a fluent API for defining environment variable schemas with automatic\n * error context enrichment.\n *\n * @template T - The type of the configuration object (typically process.env)\n *\n * @example\n * ```typescript\n * const config = new EnvironmentParser(process.env)\n * .create((get) => ({\n * port: get('PORT').string().transform(Number).default(3000),\n * database: {\n * url: get('DATABASE_URL').string().url()\n * }\n * }))\n * .parse();\n * ```\n */\nexport class EnvironmentParser<T extends EmptyObject> {\n /**\n * Set to track which environment variable names have been accessed\n */\n private readonly accessedVars: Set<string> = new Set();\n\n /**\n * Creates a new EnvironmentParser instance.\n *\n * @param config - The configuration object to parse (typically process.env)\n */\n constructor(private readonly config: T) {}\n\n /**\n * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n * with environment variable context.\n *\n * @param schema - The Zod schema to wrap\n * @param name - The environment variable name for error context\n * @returns A wrapped Zod schema with enhanced error reporting\n */\n private wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n // Create a proxy that intercepts all method calls on the schema\n return new Proxy(schema, {\n get: (target, prop) => {\n if (prop === 'parse') {\n return () => {\n const value = get(this.config, name);\n try {\n return target.parse(value);\n } catch (error) {\n if (error instanceof z.ZodError) {\n // Modify the error to include the environment variable name\n const modifiedIssues = error.issues.map((issue) => ({\n ...issue,\n message: `Environment variable \"${name}\": ${issue.message}`,\n path: [name, ...issue.path],\n }));\n throw new z.ZodError(modifiedIssues);\n }\n throw error;\n }\n };\n }\n\n if (prop === 'safeParse') {\n return () => {\n const value = get(this.config, name);\n const result = target.safeParse(value);\n\n if (!result.success) {\n // Modify the error to include the environment variable name\n const modifiedIssues = result.error.issues.map(\n (issue: z.core.$ZodIssue) => ({\n ...issue,\n message: `Environment variable \"${name}\": ${issue.message}`,\n path: [name, ...issue.path],\n }),\n );\n return {\n success: false as const,\n error: new z.ZodError(modifiedIssues),\n };\n }\n\n return result;\n };\n }\n\n // For any method that returns a new schema (like transform, optional, etc.),\n // wrap the result as well\n const originalProp = target[prop as keyof typeof target];\n if (typeof originalProp === 'function') {\n return (...args: any[]) => {\n const result = originalProp.apply(target, args);\n // If the result is a ZodType, wrap it too\n if (result && typeof result === 'object' && 'parse' in result) {\n return this.wrapSchema(result, name);\n }\n return result;\n };\n }\n\n return originalProp;\n },\n });\n };\n\n /**\n * Creates a proxied version of the Zod object that wraps all schema creators\n * to provide enhanced error messages with environment variable context.\n *\n * @param name - The environment variable name\n * @returns A proxied Zod object with wrapped schema creators\n */\n private getZodGetter = (name: string) => {\n // Track that this environment variable was accessed\n this.accessedVars.add(name);\n\n // Return an object that has all Zod schemas but with our wrapper\n return new Proxy(\n { ...z },\n {\n get: (target, prop) => {\n // deno-lint-ignore ban-ts-comment\n // @ts-ignore\n const value = target[prop];\n\n if (typeof value === 'function') {\n // Return a wrapper around each Zod schema creator\n return (...args: any[]) => {\n const schema = value(...args);\n return this.wrapSchema(schema, name);\n };\n }\n\n // Handle objects like z.coerce\n if (value && typeof value === 'object') {\n return new Proxy(value, {\n get: (nestedTarget, nestedProp) => {\n const nestedValue =\n nestedTarget[nestedProp as keyof typeof nestedTarget];\n if (typeof nestedValue === 'function') {\n return (...args: any[]) => {\n const schema = nestedValue(...args);\n return this.wrapSchema(schema, name);\n };\n }\n return nestedValue;\n },\n });\n }\n\n return value;\n },\n },\n );\n };\n\n /**\n * Creates a new ConfigParser object that can be used to parse the config object\n *\n * @param builder - A function that takes a getter function and returns a config object\n * @returns A ConfigParser object that can be used to parse the config object\n */\n create<TReturn extends EmptyObject>(\n builder: (get: EnvFetcher) => TReturn,\n ): ConfigParser<TReturn> {\n const config = builder(this.getZodGetter);\n return new ConfigParser(config, this.accessedVars);\n }\n\n /**\n * Returns an array of environment variable names that were accessed via the getter.\n * This is useful for build-time analysis to determine which env vars a service needs.\n *\n * @returns Array of environment variable names, sorted alphabetically\n *\n * @example\n * ```typescript\n * const sniffer = new EnvironmentParser({});\n * service.register(sniffer);\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n * ```\n */\n getEnvironmentVariables(): string[] {\n return Array.from(this.accessedVars).sort();\n }\n}\n\n/**\n * Infers the TypeScript type of a configuration object based on its Zod schemas.\n * Recursively processes nested objects and extracts types from Zod schemas.\n *\n * @template T - The configuration object type\n */\nexport type InferConfig<T extends EmptyObject> = {\n [K in keyof T]: T[K] extends z.ZodSchema\n ? z.infer<T[K]>\n : T[K] extends Record<string, unknown>\n ? InferConfig<T[K]>\n : T[K];\n};\n\n/**\n * Function type for fetching environment variables with Zod validation.\n * Returns a Zod object scoped to a specific environment variable.\n *\n * @template TPath - The environment variable path type\n * @param name - The environment variable name\n * @returns A Zod object for defining the schema\n */\nexport type EnvFetcher<TPath extends string = string> = (\n name: TPath,\n) => typeof z;\n\n/**\n * Function type for building environment configuration objects.\n * Takes an EnvFetcher and returns a configuration object with Zod schemas.\n *\n * @template TResponse - The response configuration object type\n * @param get - The environment variable fetcher function\n * @returns The configuration object with Zod schemas\n */\nexport type EnvironmentBuilder<TResponse extends EmptyObject> = (\n get: EnvFetcher,\n) => TResponse;\n\n/**\n * Type alias for a generic object with unknown values.\n * Used as a constraint for configuration objects.\n */\nexport type EmptyObject = Record<string | number | symbol, unknown>;\n"],"mappings":"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"}
|
|
@@ -10,17 +10,36 @@ import { z } from "zod/v4";
|
|
|
10
10
|
*/
|
|
11
11
|
declare class ConfigParser<TResponse extends EmptyObject> {
|
|
12
12
|
private readonly config;
|
|
13
|
+
private readonly envVars;
|
|
13
14
|
/**
|
|
14
15
|
* Creates a new ConfigParser instance.
|
|
15
16
|
*
|
|
16
17
|
* @param config - The configuration object to parse
|
|
18
|
+
* @param envVars - Set of environment variable names that were accessed
|
|
17
19
|
*/
|
|
18
|
-
constructor(config: TResponse);
|
|
20
|
+
constructor(config: TResponse, envVars?: Set<string>);
|
|
19
21
|
/**
|
|
20
22
|
* Parses the config object and validates it against the Zod schemas
|
|
21
23
|
* @returns The parsed config object
|
|
22
24
|
*/
|
|
23
25
|
parse(): InferConfig<TResponse>;
|
|
26
|
+
/**
|
|
27
|
+
* Returns an array of environment variable names that were accessed during config creation.
|
|
28
|
+
* This is useful for deployment and configuration management to know which env vars are required.
|
|
29
|
+
*
|
|
30
|
+
* @returns Array of environment variable names, sorted alphabetically
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```typescript
|
|
34
|
+
* const config = envParser.create((get) => ({
|
|
35
|
+
* dbUrl: get('DATABASE_URL').string(),
|
|
36
|
+
* port: get('PORT').number()
|
|
37
|
+
* }));
|
|
38
|
+
*
|
|
39
|
+
* config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
getEnvironmentVariables(): string[];
|
|
24
43
|
}
|
|
25
44
|
/**
|
|
26
45
|
* Parses environment variables with type-safe validation using Zod schemas.
|
|
@@ -43,6 +62,10 @@ declare class ConfigParser<TResponse extends EmptyObject> {
|
|
|
43
62
|
*/
|
|
44
63
|
declare class EnvironmentParser<T extends EmptyObject> {
|
|
45
64
|
private readonly config;
|
|
65
|
+
/**
|
|
66
|
+
* Set to track which environment variable names have been accessed
|
|
67
|
+
*/
|
|
68
|
+
private readonly accessedVars;
|
|
46
69
|
/**
|
|
47
70
|
* Creates a new EnvironmentParser instance.
|
|
48
71
|
*
|
|
@@ -73,6 +96,20 @@ declare class EnvironmentParser<T extends EmptyObject> {
|
|
|
73
96
|
* @returns A ConfigParser object that can be used to parse the config object
|
|
74
97
|
*/
|
|
75
98
|
create<TReturn extends EmptyObject>(builder: (get: EnvFetcher) => TReturn): ConfigParser<TReturn>;
|
|
99
|
+
/**
|
|
100
|
+
* Returns an array of environment variable names that were accessed via the getter.
|
|
101
|
+
* This is useful for build-time analysis to determine which env vars a service needs.
|
|
102
|
+
*
|
|
103
|
+
* @returns Array of environment variable names, sorted alphabetically
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* const sniffer = new EnvironmentParser({});
|
|
108
|
+
* service.register(sniffer);
|
|
109
|
+
* const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
getEnvironmentVariables(): string[];
|
|
76
113
|
}
|
|
77
114
|
/**
|
|
78
115
|
* Infers the TypeScript type of a configuration object based on its Zod schemas.
|
|
@@ -105,4 +142,5 @@ type EnvironmentBuilder<TResponse extends EmptyObject> = (get: EnvFetcher) => TR
|
|
|
105
142
|
*/
|
|
106
143
|
type EmptyObject = Record<string | number | symbol, unknown>;
|
|
107
144
|
//#endregion
|
|
108
|
-
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig };
|
|
145
|
+
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig };
|
|
146
|
+
//# sourceMappingURL=EnvironmentParser-CVWU1ooT.d.mts.map
|
|
@@ -14,9 +14,11 @@ var ConfigParser = class {
|
|
|
14
14
|
* Creates a new ConfigParser instance.
|
|
15
15
|
*
|
|
16
16
|
* @param config - The configuration object to parse
|
|
17
|
+
* @param envVars - Set of environment variable names that were accessed
|
|
17
18
|
*/
|
|
18
|
-
constructor(config) {
|
|
19
|
+
constructor(config, envVars = /* @__PURE__ */ new Set()) {
|
|
19
20
|
this.config = config;
|
|
21
|
+
this.envVars = envVars;
|
|
20
22
|
}
|
|
21
23
|
/**
|
|
22
24
|
* Parses the config object and validates it against the Zod schemas
|
|
@@ -45,6 +47,25 @@ var ConfigParser = class {
|
|
|
45
47
|
if (errors.length > 0) throw new z.ZodError(errors);
|
|
46
48
|
return parsedConfig;
|
|
47
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* Returns an array of environment variable names that were accessed during config creation.
|
|
52
|
+
* This is useful for deployment and configuration management to know which env vars are required.
|
|
53
|
+
*
|
|
54
|
+
* @returns Array of environment variable names, sorted alphabetically
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```typescript
|
|
58
|
+
* const config = envParser.create((get) => ({
|
|
59
|
+
* dbUrl: get('DATABASE_URL').string(),
|
|
60
|
+
* port: get('PORT').number()
|
|
61
|
+
* }));
|
|
62
|
+
*
|
|
63
|
+
* config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
getEnvironmentVariables() {
|
|
67
|
+
return Array.from(this.envVars).sort();
|
|
68
|
+
}
|
|
48
69
|
};
|
|
49
70
|
/**
|
|
50
71
|
* Parses environment variables with type-safe validation using Zod schemas.
|
|
@@ -66,6 +87,10 @@ var ConfigParser = class {
|
|
|
66
87
|
* ```
|
|
67
88
|
*/
|
|
68
89
|
var EnvironmentParser = class {
|
|
90
|
+
/**
|
|
91
|
+
* Set to track which environment variable names have been accessed
|
|
92
|
+
*/
|
|
93
|
+
accessedVars = /* @__PURE__ */ new Set();
|
|
69
94
|
/**
|
|
70
95
|
* Creates a new EnvironmentParser instance.
|
|
71
96
|
*
|
|
@@ -133,6 +158,7 @@ var EnvironmentParser = class {
|
|
|
133
158
|
* @returns A proxied Zod object with wrapped schema creators
|
|
134
159
|
*/
|
|
135
160
|
getZodGetter = (name) => {
|
|
161
|
+
this.accessedVars.add(name);
|
|
136
162
|
return new Proxy({ ...z }, { get: (target, prop) => {
|
|
137
163
|
const value = target[prop];
|
|
138
164
|
if (typeof value === "function") return (...args) => {
|
|
@@ -158,9 +184,26 @@ var EnvironmentParser = class {
|
|
|
158
184
|
*/
|
|
159
185
|
create(builder) {
|
|
160
186
|
const config = builder(this.getZodGetter);
|
|
161
|
-
return new ConfigParser(config);
|
|
187
|
+
return new ConfigParser(config, this.accessedVars);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Returns an array of environment variable names that were accessed via the getter.
|
|
191
|
+
* This is useful for build-time analysis to determine which env vars a service needs.
|
|
192
|
+
*
|
|
193
|
+
* @returns Array of environment variable names, sorted alphabetically
|
|
194
|
+
*
|
|
195
|
+
* @example
|
|
196
|
+
* ```typescript
|
|
197
|
+
* const sniffer = new EnvironmentParser({});
|
|
198
|
+
* service.register(sniffer);
|
|
199
|
+
* const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']
|
|
200
|
+
* ```
|
|
201
|
+
*/
|
|
202
|
+
getEnvironmentVariables() {
|
|
203
|
+
return Array.from(this.accessedVars).sort();
|
|
162
204
|
}
|
|
163
205
|
};
|
|
164
206
|
|
|
165
207
|
//#endregion
|
|
166
|
-
export { ConfigParser, EnvironmentParser };
|
|
208
|
+
export { ConfigParser, EnvironmentParser };
|
|
209
|
+
//# sourceMappingURL=EnvironmentParser-c06agx31.mjs.map
|