@geekmidas/envkit 0.1.0 → 0.3.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/dist/{EnvironmentBuilder-DfmYRBm-.mjs → EnvironmentBuilder-BSuHZm0y.mjs} +2 -4
- package/dist/EnvironmentBuilder-BSuHZm0y.mjs.map +1 -0
- package/dist/EnvironmentBuilder-DHfDXJUm.d.mts.map +1 -0
- package/dist/{EnvironmentBuilder-W2wku49g.cjs → EnvironmentBuilder-Djr1VsWM.cjs} +2 -4
- package/dist/EnvironmentBuilder-Djr1VsWM.cjs.map +1 -0
- package/dist/EnvironmentBuilder-Xuf2Dd9u.d.cts.map +1 -0
- package/dist/EnvironmentBuilder.cjs +1 -1
- package/dist/EnvironmentBuilder.mjs +1 -1
- package/dist/EnvironmentParser-Bt246UeP.cjs.map +1 -1
- package/dist/{EnvironmentParser-CVWU1ooT.d.mts → EnvironmentParser-CY8TosTN.d.mts} +2 -1
- package/dist/EnvironmentParser-CY8TosTN.d.mts.map +1 -0
- package/dist/{EnvironmentParser-tV-JjCg7.d.cts → EnvironmentParser-DtOL86NU.d.cts} +2 -1
- package/dist/EnvironmentParser-DtOL86NU.d.cts.map +1 -0
- package/dist/EnvironmentParser-c06agx31.mjs.map +1 -1
- package/dist/EnvironmentParser.d.cts +1 -1
- package/dist/EnvironmentParser.d.mts +1 -1
- package/dist/SnifferEnvironmentParser.cjs.map +1 -1
- package/dist/SnifferEnvironmentParser.d.cts +3 -2
- package/dist/SnifferEnvironmentParser.d.cts.map +1 -0
- package/dist/SnifferEnvironmentParser.d.mts +3 -2
- package/dist/SnifferEnvironmentParser.d.mts.map +1 -0
- package/dist/SnifferEnvironmentParser.mjs.map +1 -1
- package/dist/{SstEnvironmentBuilder-DEa3lTUB.mjs → SstEnvironmentBuilder-BEBFSUYr.mjs} +2 -2
- package/dist/SstEnvironmentBuilder-BEBFSUYr.mjs.map +1 -0
- package/dist/SstEnvironmentBuilder-CjURMGjW.d.mts.map +1 -0
- package/dist/SstEnvironmentBuilder-D4oSo_KX.d.cts.map +1 -0
- package/dist/{SstEnvironmentBuilder-BuFw1hCe.cjs → SstEnvironmentBuilder-wFnN2M5O.cjs} +2 -2
- package/dist/SstEnvironmentBuilder-wFnN2M5O.cjs.map +1 -0
- package/dist/SstEnvironmentBuilder.cjs +2 -2
- package/dist/SstEnvironmentBuilder.mjs +2 -2
- package/dist/credentials.cjs +66 -0
- package/dist/credentials.cjs.map +1 -0
- package/dist/credentials.d.cts +31 -0
- package/dist/credentials.d.cts.map +1 -0
- package/dist/credentials.d.mts +31 -0
- package/dist/credentials.d.mts.map +1 -0
- package/dist/credentials.mjs +62 -0
- package/dist/credentials.mjs.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.mjs +1 -1
- package/dist/sst.cjs +2 -2
- package/dist/sst.cjs.map +1 -1
- package/dist/sst.d.cts +1 -0
- package/dist/sst.d.cts.map +1 -0
- package/dist/sst.d.mts +1 -0
- package/dist/sst.d.mts.map +1 -0
- package/dist/sst.mjs +2 -2
- package/dist/sst.mjs.map +1 -1
- package/examples/basic-usage.ts +329 -333
- package/package.json +6 -1
- package/src/EnvironmentBuilder.ts +76 -80
- package/src/EnvironmentParser.ts +231 -231
- package/src/SnifferEnvironmentParser.ts +178 -178
- package/src/SstEnvironmentBuilder.ts +127 -127
- package/src/__tests__/ConfigParser.spec.ts +388 -388
- package/src/__tests__/EnvironmentBuilder.spec.ts +245 -265
- package/src/__tests__/EnvironmentParser.spec.ts +828 -828
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +380 -326
- package/src/__tests__/SstEnvironmentBuilder.spec.ts +347 -367
- package/src/__tests__/credentials.integration.spec.ts +239 -0
- package/src/__tests__/credentials.spec.ts +136 -0
- package/src/__tests__/sst.spec.ts +390 -413
- package/src/credentials.ts +99 -0
- package/src/index.ts +11 -11
- package/src/sst.ts +24 -24
- package/sst-env.d.ts +0 -1
- package/tsconfig.json +9 -0
- package/dist/EnvironmentBuilder-DfmYRBm-.mjs.map +0 -1
- package/dist/EnvironmentBuilder-W2wku49g.cjs.map +0 -1
- package/dist/SstEnvironmentBuilder-BuFw1hCe.cjs.map +0 -1
- package/dist/SstEnvironmentBuilder-DEa3lTUB.mjs.map +0 -1
|
@@ -46,9 +46,7 @@ var EnvironmentBuilder = class {
|
|
|
46
46
|
constructor(record, resolvers, options = {}) {
|
|
47
47
|
this.record = record;
|
|
48
48
|
this.resolvers = resolvers;
|
|
49
|
-
this.options = { onUnmatchedValue: options.onUnmatchedValue ?? ((
|
|
50
|
-
console.warn(`No resolver found for key "${key}":`, { value });
|
|
51
|
-
}) };
|
|
49
|
+
this.options = { onUnmatchedValue: options.onUnmatchedValue ?? ((_key, _value) => {}) };
|
|
52
50
|
}
|
|
53
51
|
/**
|
|
54
52
|
* Build environment variables from the input record.
|
|
@@ -80,4 +78,4 @@ var EnvironmentBuilder = class {
|
|
|
80
78
|
|
|
81
79
|
//#endregion
|
|
82
80
|
export { EnvironmentBuilder, environmentCase };
|
|
83
|
-
//# sourceMappingURL=EnvironmentBuilder-
|
|
81
|
+
//# sourceMappingURL=EnvironmentBuilder-BSuHZm0y.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-BSuHZm0y.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\treturn snakecase(name)\n\t\t.toUpperCase()\n\t\t.replace(/_\\d+/g, (r) => {\n\t\t\treturn r.replace('_', '');\n\t\t});\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\t[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\t/**\n\t * Handler called when a value's type doesn't match any registered resolver.\n\t * Defaults to console.warn.\n\t */\n\tonUnmatchedValue?: (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\ttype: TType;\n\t[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\t[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\tTRecord extends Record<string, InputValue>,\n\tTType extends string,\n> = {\n\t[K in keyof TRecord]: TRecord[K] extends { type: TType }\n\t\t? OmitType<TRecord[K]>\n\t\t: 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\t[TType in AllTypeValues<TRecord>]: EnvironmentResolver<\n\t\tValueForType<TRecord, TType>\n\t>;\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\tTRecord extends Record<string, InputValue> = Record<string, InputValue>,\n\tTResolvers extends Resolvers = TypedResolvers<TRecord>,\n> {\n\tprivate readonly record: TRecord;\n\tprivate readonly resolvers: TResolvers;\n\tprivate readonly options: Required<EnvironmentBuilderOptions>;\n\n\tconstructor(\n\t\trecord: TRecord,\n\t\tresolvers: TResolvers,\n\t\toptions: EnvironmentBuilderOptions = {},\n\t) {\n\t\tthis.record = record;\n\t\tthis.resolvers = resolvers;\n\t\tthis.options = {\n\t\t\tonUnmatchedValue: options.onUnmatchedValue ?? ((_key, _value) => {}),\n\t\t};\n\t}\n\n\t/**\n\t * Build environment variables from the input record.\n\t *\n\t * - Plain string values are passed through with key transformation\n\t * - Object values with a `type` property are matched against resolvers\n\t * - Resolvers receive values without the `type` key\n\t * - Only root-level keys are transformed to UPPER_SNAKE_CASE\n\t *\n\t * @returns A record of environment variables\n\t */\n\tbuild(): EnvRecord {\n\t\tconst env: EnvRecord = {};\n\n\t\tfor (const [key, value] of Object.entries(this.record)) {\n\t\t\t// Handle plain string values\n\t\t\tif (typeof value === 'string') {\n\t\t\t\tenv[environmentCase(key)] = value;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Handle objects with type discriminator\n\t\t\tconst { type, ...rest } = value;\n\t\t\tconst resolver = this.resolvers[type];\n\t\t\tif (resolver) {\n\t\t\t\tconst resolved = resolver(key, rest);\n\t\t\t\t// Transform only root-level keys from resolver output\n\t\t\t\tfor (const [resolvedKey, resolvedValue] of Object.entries(resolved)) {\n\t\t\t\t\tenv[environmentCase(resolvedKey)] = resolvedValue;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthis.options.onUnmatchedValue(key, value);\n\t\t\t}\n\t\t}\n\n\t\treturn env;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAgB,gBAAgBA,MAAsB;AACrD,QAAO,UAAU,KAAK,CACpB,aAAa,CACb,QAAQ,SAAS,CAAC,MAAM;AACxB,SAAO,EAAE,QAAQ,KAAK,GAAG;CACzB,EAAC;AACH;;;;;;;;;;;;;;;;;;;;;;;AAoHD,IAAa,qBAAb,MAGE;CACD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YACCC,QACAC,WACAC,UAAqC,CAAE,GACtC;AACD,OAAK,SAAS;AACd,OAAK,YAAY;AACjB,OAAK,UAAU,EACd,kBAAkB,QAAQ,qBAAqB,CAAC,MAAM,WAAW,CAAE,GACnE;CACD;;;;;;;;;;;CAYD,QAAmB;EAClB,MAAMC,MAAiB,CAAE;AAEzB,OAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,KAAK,OAAO,EAAE;AAEvD,cAAW,UAAU,UAAU;AAC9B,QAAI,gBAAgB,IAAI,IAAI;AAC5B;GACA;GAGD,MAAM,EAAE,KAAM,GAAG,MAAM,GAAG;GAC1B,MAAM,WAAW,KAAK,UAAU;AAChC,OAAI,UAAU;IACb,MAAM,WAAW,SAAS,KAAK,KAAK;AAEpC,SAAK,MAAM,CAAC,aAAa,cAAc,IAAI,OAAO,QAAQ,SAAS,CAClE,KA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AAA,GA3DV,CA2DU,SAAA;EAAU,IAAzB,EAAA,KAAA,WAAA,MAAA;CAAM,GA3D4C,CA2D5C,GAAsC,KAAA;;;;KAtDxD,QAuD2B,CAAA,CAAA,CAAA,GAvDb,CAuDa,SAAA;EAAc,IAOpC,EAAA,MAAA;CAAO,GA9D+B,IA+DnC,CA/DwC,CA+DxC,EAAA,MAAA,CAAA,GAAA,KAAA;;;AAoBM;KA9Ed,8BAA8B,eAAe,6BACrC,UAAU,YAAY,QAAQ,YACnC;;;;KAKH,6BACY,eAAe,mDAGnB,UAAU,QAAQ;QAAmB;IAC9C,SAAS,QAAQ,oBAEb;;;;;KAMI,+BAA+B,eAAe,2BAC/C,cAAc,WAAW,oBAClC,aAAa,SAAS;;;;;;;;;;;;;;;;;;;;;;;cA0BX,mCACI,eAAe,cAAc,eAAe,gCACzC,YAAY,eAAe;;;;sBAOrC,oBACG,sBACF;;;;;;;;;;;WAmBD"}
|
|
@@ -47,9 +47,7 @@ var EnvironmentBuilder = class {
|
|
|
47
47
|
constructor(record, resolvers, options = {}) {
|
|
48
48
|
this.record = record;
|
|
49
49
|
this.resolvers = resolvers;
|
|
50
|
-
this.options = { onUnmatchedValue: options.onUnmatchedValue ?? ((
|
|
51
|
-
console.warn(`No resolver found for key "${key}":`, { value });
|
|
52
|
-
}) };
|
|
50
|
+
this.options = { onUnmatchedValue: options.onUnmatchedValue ?? ((_key, _value) => {}) };
|
|
53
51
|
}
|
|
54
52
|
/**
|
|
55
53
|
* Build environment variables from the input record.
|
|
@@ -92,4 +90,4 @@ Object.defineProperty(exports, 'environmentCase', {
|
|
|
92
90
|
return environmentCase;
|
|
93
91
|
}
|
|
94
92
|
});
|
|
95
|
-
//# sourceMappingURL=EnvironmentBuilder-
|
|
93
|
+
//# sourceMappingURL=EnvironmentBuilder-Djr1VsWM.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-Djr1VsWM.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\treturn snakecase(name)\n\t\t.toUpperCase()\n\t\t.replace(/_\\d+/g, (r) => {\n\t\t\treturn r.replace('_', '');\n\t\t});\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\t[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\t/**\n\t * Handler called when a value's type doesn't match any registered resolver.\n\t * Defaults to console.warn.\n\t */\n\tonUnmatchedValue?: (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\ttype: TType;\n\t[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\t[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\tTRecord extends Record<string, InputValue>,\n\tTType extends string,\n> = {\n\t[K in keyof TRecord]: TRecord[K] extends { type: TType }\n\t\t? OmitType<TRecord[K]>\n\t\t: 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\t[TType in AllTypeValues<TRecord>]: EnvironmentResolver<\n\t\tValueForType<TRecord, TType>\n\t>;\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\tTRecord extends Record<string, InputValue> = Record<string, InputValue>,\n\tTResolvers extends Resolvers = TypedResolvers<TRecord>,\n> {\n\tprivate readonly record: TRecord;\n\tprivate readonly resolvers: TResolvers;\n\tprivate readonly options: Required<EnvironmentBuilderOptions>;\n\n\tconstructor(\n\t\trecord: TRecord,\n\t\tresolvers: TResolvers,\n\t\toptions: EnvironmentBuilderOptions = {},\n\t) {\n\t\tthis.record = record;\n\t\tthis.resolvers = resolvers;\n\t\tthis.options = {\n\t\t\tonUnmatchedValue: options.onUnmatchedValue ?? ((_key, _value) => {}),\n\t\t};\n\t}\n\n\t/**\n\t * Build environment variables from the input record.\n\t *\n\t * - Plain string values are passed through with key transformation\n\t * - Object values with a `type` property are matched against resolvers\n\t * - Resolvers receive values without the `type` key\n\t * - Only root-level keys are transformed to UPPER_SNAKE_CASE\n\t *\n\t * @returns A record of environment variables\n\t */\n\tbuild(): EnvRecord {\n\t\tconst env: EnvRecord = {};\n\n\t\tfor (const [key, value] of Object.entries(this.record)) {\n\t\t\t// Handle plain string values\n\t\t\tif (typeof value === 'string') {\n\t\t\t\tenv[environmentCase(key)] = value;\n\t\t\t\tcontinue;\n\t\t\t}\n\n\t\t\t// Handle objects with type discriminator\n\t\t\tconst { type, ...rest } = value;\n\t\t\tconst resolver = this.resolvers[type];\n\t\t\tif (resolver) {\n\t\t\t\tconst resolved = resolver(key, rest);\n\t\t\t\t// Transform only root-level keys from resolver output\n\t\t\t\tfor (const [resolvedKey, resolvedValue] of Object.entries(resolved)) {\n\t\t\t\t\tenv[environmentCase(resolvedKey)] = resolvedValue;\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\tthis.options.onUnmatchedValue(key, value);\n\t\t\t}\n\t\t}\n\n\t\treturn env;\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;AAaA,SAAgB,gBAAgBA,MAAsB;AACrD,QAAO,8BAAU,KAAK,CACpB,aAAa,CACb,QAAQ,SAAS,CAAC,MAAM;AACxB,SAAO,EAAE,QAAQ,KAAK,GAAG;CACzB,EAAC;AACH;;;;;;;;;;;;;;;;;;;;;;;AAoHD,IAAa,qBAAb,MAGE;CACD,AAAiB;CACjB,AAAiB;CACjB,AAAiB;CAEjB,YACCC,QACAC,WACAC,UAAqC,CAAE,GACtC;AACD,OAAK,SAAS;AACd,OAAK,YAAY;AACjB,OAAK,UAAU,EACd,kBAAkB,QAAQ,qBAAqB,CAAC,MAAM,WAAW,CAAE,GACnE;CACD;;;;;;;;;;;CAYD,QAAmB;EAClB,MA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AAA,EAAA,KAAA,EAAA,OAAA,EAAA,GAAA,IAAA;;;;;AAGnC,KArCH,UAAA,GAqCG,MAAA,GAAA;EAMH,IAAA,EAAA,MAAA;EAAc,CAAA,GAAA,EAAA,MAAA,CAAA,EAAA,OAAA;CAAA;;;;AAEX,KAxCH,eAwCG,CAAA,cAAA,MAAA,GAAA,MAAA,CAAA,GAAA;EAAO,IAAE,EAvCjB,KAuCiB;EAAK,CAAA,GAA3B,EAAA,MAAA,CAAA,EAAA,OAAA;CAAY;AADyC;AA2BvD;;KA1DK,WA2D2B,CAAA,CAAA,CAAA,GA3DV,CA2DU,SAAA;EAAU,IAAzB,EAAA,KAAA,WAAA,MAAA;CAAM,GA3D4C,CA2D5C,GAAsC,KAAA;;;;KAtDxD,QAuD2B,CAAA,CAAA,CAAA,GAvDb,CAuDa,SAAA;EAAc,IAOpC,EAAA,MAAA;CAAO,GA9D+B,IA+DnC,CA/DwC,CA+DxC,EAAA,MAAA,CAAA,GAAA,KAAA;;;AAoBM;KA9Ed,8BAA8B,eAAe,6BACrC,UAAU,YAAY,QAAQ,YACnC;;;;KAKH,6BACY,eAAe,mDAGnB,UAAU,QAAQ;QAAmB;IAC9C,SAAS,QAAQ,oBAEb;;;;;KAMI,+BAA+B,eAAe,2BAC/C,cAAc,WAAW,oBAClC,aAAa,SAAS;;;;;;;;;;;;;;;;;;;;;;;cA0BX,mCACI,eAAe,cAAc,eAAe,gCACzC,YAAY,eAAe;;;;sBAOrC,oBACG,sBACF;;;;;;;;;;;WAmBD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_EnvironmentBuilder = require('./EnvironmentBuilder-
|
|
1
|
+
const require_EnvironmentBuilder = require('./EnvironmentBuilder-Djr1VsWM.cjs');
|
|
2
2
|
|
|
3
3
|
exports.EnvironmentBuilder = require_EnvironmentBuilder.EnvironmentBuilder;
|
|
4
4
|
exports.environmentCase = require_EnvironmentBuilder.environmentCase;
|
|
@@ -1 +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":";;;;;;;;;;;;AAUA,IAAa,eAAb,MAAyD;;;;;;;CAOvD,YACmBA,QACAC,0BAAuB,IAAI,OAC5C;EAFiB;EACA;CACf;;;;;CAKJ,QAAgC;EAC9B,MAAMC,SAA6B,CAAE;EAErC,MAAM,YAAY,CAAIC,QAAWC,OAAiB,CAAE,MAAK;GACvD,MAAMC,SAAsB,CAAE;AAE9B,OAAI,iBAAiB,WAAW,SAC9B,QAAO;AAGT,QAAK,MAAM,OAAO,QAAQ;IACxB,MAAM,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EJ;;;;;;;CAQD,OACEI,SACuB;EACvB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACtC;;;;;;;;;;;;;;CAeD,0BAAoC;AAClC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC5C;AACF"}
|
|
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\t/**\n\t * Creates a new ConfigParser instance.\n\t *\n\t * @param config - The configuration object to parse\n\t * @param envVars - Set of environment variable names that were accessed\n\t */\n\tconstructor(\n\t\tprivate readonly config: TResponse,\n\t\tprivate readonly envVars: Set<string> = new Set(),\n\t) {}\n\t/**\n\t * Parses the config object and validates it against the Zod schemas\n\t * @returns The parsed config object\n\t */\n\tparse(): InferConfig<TResponse> {\n\t\tconst errors: z.core.$ZodIssue[] = [];\n\n\t\tconst parseDeep = <T>(config: T, path: string[] = []) => {\n\t\t\tconst result: EmptyObject = {};\n\n\t\t\tif (config && typeof config !== 'object') {\n\t\t\t\treturn config;\n\t\t\t}\n\n\t\t\tfor (const key in config) {\n\t\t\t\tconst schema = config[key];\n\t\t\t\tconst currentPath = [...path, key];\n\n\t\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\t\tif (parsed.success) {\n\t\t\t\t\t\tset(result, key, parsed.data);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If the schema is invalid, assign the error\n\t\t\t\t\t\terrors.push(\n\t\t\t\t\t\t\t...parsed.error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [...currentPath, ...(issue.path as string[])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else if (schema) {\n\t\t\t\t\tset(result, key, parseDeep(schema as EmptyObject, currentPath));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst parsedConfig = parseDeep(\n\t\t\tthis.config,\n\t\t) as unknown as InferConfig<TResponse>;\n\n\t\tif (errors.length > 0) {\n\t\t\t// If there are errors, throw them\n\t\t\tthrow new z.ZodError(errors);\n\t\t}\n\n\t\treturn parsedConfig;\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed during config creation.\n\t * This is useful for deployment and configuration management to know which env vars are required.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const config = envParser.create((get) => ({\n\t * dbUrl: get('DATABASE_URL').string(),\n\t * port: get('PORT').number()\n\t * }));\n\t *\n\t * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.envVars).sort();\n\t}\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\t/**\n\t * Set to track which environment variable names have been accessed\n\t */\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Creates a new EnvironmentParser instance.\n\t *\n\t * @param config - The configuration object to parse (typically process.env)\n\t */\n\tconstructor(private readonly config: T) {}\n\n\t/**\n\t * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n\t * with environment variable context.\n\t *\n\t * @param schema - The Zod schema to wrap\n\t * @param name - The environment variable name for error context\n\t * @returns A wrapped Zod schema with enhanced error reporting\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\t// Create a proxy that intercepts all method calls on the schema\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.parse(value);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof z.ZodError) {\n\t\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\t\tconst modifiedIssues = error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t\t\tthrow new z.ZodError(modifiedIssues);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthrow error;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\tconst result = target.safeParse(value);\n\n\t\t\t\t\t\tif (!result.success) {\n\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\tconst modifiedIssues = result.error.issues.map(\n\t\t\t\t\t\t\t\t(issue: z.core.$ZodIssue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false as const,\n\t\t\t\t\t\t\t\terror: new z.ZodError(modifiedIssues),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// For any method that returns a new schema (like transform, optional, etc.),\n\t\t\t\t// wrap the result as well\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\t// If the result is a ZodType, wrap it too\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Creates a proxied version of the Zod object that wraps all schema creators\n\t * to provide enhanced error messages with environment variable context.\n\t *\n\t * @param name - The environment variable name\n\t * @returns A proxied Zod object with wrapped schema creators\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\t// Track that this environment variable was accessed\n\t\tthis.accessedVars.add(name);\n\n\t\t// Return an object that has all Zod schemas but with our wrapper\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// deno-lint-ignore ban-ts-comment\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\t// Return a wrapper around each Zod schema creator\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle objects like z.coerce\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a new ConfigParser object that can be used to parse the config object\n\t *\n\t * @param builder - A function that takes a getter function and returns a config object\n\t * @returns A ConfigParser object that can be used to parse the config object\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new ConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed via the getter.\n\t * This is useful for build-time analysis to determine which env vars a service needs.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const sniffer = new EnvironmentParser({});\n\t * service.register(sniffer);\n\t * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\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\t[K in keyof T]: T[K] extends z.ZodSchema\n\t\t? z.infer<T[K]>\n\t\t: T[K] extends Record<string, unknown>\n\t\t\t? InferConfig<T[K]>\n\t\t\t: 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\tname: 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\tget: 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":";;;;;;;;;;;;AAUA,IAAa,eAAb,MAAyD;;;;;;;CAOxD,YACkBA,QACAC,0BAAuB,IAAI,OAC3C;EAFgB;EACA;CACd;;;;;CAKJ,QAAgC;EAC/B,MAAMC,SAA6B,CAAE;EAErC,MAAM,YAAY,CAAIC,QAAWC,OAAiB,CAAE,MAAK;GACxD,MAAMC,SAAsB,CAAE;AAE9B,OAAI,iBAAiB,WAAW,SAC/B,QAAO;AAGR,QAAK,MAAM,OAAO,QAAQ;IACzB,MAAM,SAAS,OAAO;IACtB,MAAM,cAAc,CAAC,GAAG,MAAM,GAAI;AAElC,QAAI,kBAAkBC,SAAE,SAAS;KAChC,MAAM,SAAS,OAAO,iBAAoB;AAC1C,SAAI,OAAO,QACV,yBAAI,QAAQ,KAAK,OAAO,KAAK;SAG7B,QAAO,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AM;CAC3C;AACD"}
|
|
@@ -141,6 +141,7 @@ type EnvironmentBuilder<TResponse extends EmptyObject> = (get: EnvFetcher) => TR
|
|
|
141
141
|
* Used as a constraint for configuration objects.
|
|
142
142
|
*/
|
|
143
143
|
type EmptyObject = Record<string | number | symbol, unknown>;
|
|
144
|
+
//# sourceMappingURL=EnvironmentParser.d.ts.map
|
|
144
145
|
//#endregion
|
|
145
146
|
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig };
|
|
146
|
-
//# sourceMappingURL=EnvironmentParser-
|
|
147
|
+
//# sourceMappingURL=EnvironmentParser-CY8TosTN.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-CY8TosTN.d.mts","names":[],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;;;;AAS4B,cATf,YASe,CAAA,kBATgB,WAShB,CAAA,CAAA;EAAG,iBAMT,MAAA;EAAS,iBAArB,OAAA;EAAW;AAuFrB;;;;;EAiJmC,WAClB,CAAA,MAAA,EAhPU,SAgPV,EAAA,OAAA,CAAA,EA/OW,GA+OX,CAAA,MAAA,CAAA;EAAU;;;AACX;EA6BJ,KAAA,CAAA,CAAA,EAvQF,WAuQa,CAvQD,SAuQC,CAAA;EAAA;;;;;;;;;;;;;;;;EAKjB,uBAAC,CAAA,CAAA,EAAA,MAAA,EAAA;AAAC;AAWR;;;;AAEa;AAUb;;;;;AAEc;AAMd;;;;;;;;cApNa,4BAA4B;;;;;;;;;;;sBAWH;;;;;;;;;;;;;;;;;;;;;;;;yBAsId,4BACP,eAAe,UAC5B,aAAa;;;;;;;;;;;;;;;;;;;;;;KA6BL,sBAAsB,6BACrB,IAAI,EAAE,WAAW,CAAA,CAAE,YAC5B,CAAA,CAAE,MAAM,EAAE,MACV,EAAE,WAAW,0BACZ,YAAY,EAAE,MACd,EAAE;;;;;;;;;KAWK,mDACL,iBACK;;;;;;;;;KAUA,qCAAqC,qBAC3C,eACD;;;;;KAMO,WAAA,GAAc"}
|
|
@@ -141,6 +141,7 @@ type EnvironmentBuilder<TResponse extends EmptyObject> = (get: EnvFetcher) => TR
|
|
|
141
141
|
* Used as a constraint for configuration objects.
|
|
142
142
|
*/
|
|
143
143
|
type EmptyObject = Record<string | number | symbol, unknown>;
|
|
144
|
+
//# sourceMappingURL=EnvironmentParser.d.ts.map
|
|
144
145
|
//#endregion
|
|
145
146
|
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig };
|
|
146
|
-
//# sourceMappingURL=EnvironmentParser-
|
|
147
|
+
//# sourceMappingURL=EnvironmentParser-DtOL86NU.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-DtOL86NU.d.cts","names":[],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AAUA;;;;AAS4B,cATf,YASe,CAAA,kBATgB,WAShB,CAAA,CAAA;EAAG,iBAMT,MAAA;EAAS,iBAArB,OAAA;EAAW;AAuFrB;;;;;EAiJmC,WAClB,CAAA,MAAA,EAhPU,SAgPV,EAAA,OAAA,CAAA,EA/OW,GA+OX,CAAA,MAAA,CAAA;EAAU;;;AACX;EA6BJ,KAAA,CAAA,CAAA,EAvQF,WAuQa,CAvQD,SAuQC,CAAA;EAAA;;;;;;;;;;;;;;;;EAKjB,uBAAC,CAAA,CAAA,EAAA,MAAA,EAAA;AAAC;AAWR;;;;AAEa;AAUb;;;;;AAEc;AAMd;;;;;;;;cApNa,4BAA4B;;;;;;;;;;;sBAWH;;;;;;;;;;;;;;;;;;;;;;;;yBAsId,4BACP,eAAe,UAC5B,aAAa;;;;;;;;;;;;;;;;;;;;;;KA6BL,sBAAsB,6BACrB,IAAI,EAAE,WAAW,CAAA,CAAE,YAC5B,CAAA,CAAE,MAAM,EAAE,MACV,EAAE,WAAW,0BACZ,YAAY,EAAE,MACd,EAAE;;;;;;;;;KAWK,mDACL,iBACK;;;;;;;;;KAUA,qCAAqC,qBAC3C,eACD;;;;;KAMO,WAAA,GAAc"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"EnvironmentParser-c06agx31.mjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","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":";;;;;;;;;;;AAUA,IAAa,eAAb,MAAyD;;;;;;;CAOvD,YACmBA,QACAC,0BAAuB,IAAI,OAC5C;EAFiB;EACA;CACf;;;;;CAKJ,QAAgC;EAC9B,MAAMC,SAA6B,CAAE;EAErC,MAAM,YAAY,CAAIC,QAAWC,OAAiB,CAAE,MAA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GAAG,KAAK;AACnC,YAAO,KAAK,WAAW,QAAQ,KAAK;IACrC;AAEH,WAAO;GACR,EACF;AAGH,UAAO;EACR,EACF;CAEJ;;;;;;;CAQD,OACEE,SACuB;EACvB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACtC;;;;;;;;;;;;;;CAeD,0BAAoC;AAClC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC5C;AACF"}
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-c06agx31.mjs","names":["config: TResponse","envVars: Set<string>","errors: z.core.$ZodIssue[]","config: T","path: string[]","result: EmptyObject","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\t/**\n\t * Creates a new ConfigParser instance.\n\t *\n\t * @param config - The configuration object to parse\n\t * @param envVars - Set of environment variable names that were accessed\n\t */\n\tconstructor(\n\t\tprivate readonly config: TResponse,\n\t\tprivate readonly envVars: Set<string> = new Set(),\n\t) {}\n\t/**\n\t * Parses the config object and validates it against the Zod schemas\n\t * @returns The parsed config object\n\t */\n\tparse(): InferConfig<TResponse> {\n\t\tconst errors: z.core.$ZodIssue[] = [];\n\n\t\tconst parseDeep = <T>(config: T, path: string[] = []) => {\n\t\t\tconst result: EmptyObject = {};\n\n\t\t\tif (config && typeof config !== 'object') {\n\t\t\t\treturn config;\n\t\t\t}\n\n\t\t\tfor (const key in config) {\n\t\t\t\tconst schema = config[key];\n\t\t\t\tconst currentPath = [...path, key];\n\n\t\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\t\tif (parsed.success) {\n\t\t\t\t\t\tset(result, key, parsed.data);\n\t\t\t\t\t} else {\n\t\t\t\t\t\t// If the schema is invalid, assign the error\n\t\t\t\t\t\terrors.push(\n\t\t\t\t\t\t\t...parsed.error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\tpath: [...currentPath, ...(issue.path as string[])],\n\t\t\t\t\t\t\t})),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t} else if (schema) {\n\t\t\t\t\tset(result, key, parseDeep(schema as EmptyObject, currentPath));\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn result;\n\t\t};\n\n\t\tconst parsedConfig = parseDeep(\n\t\t\tthis.config,\n\t\t) as unknown as InferConfig<TResponse>;\n\n\t\tif (errors.length > 0) {\n\t\t\t// If there are errors, throw them\n\t\t\tthrow new z.ZodError(errors);\n\t\t}\n\n\t\treturn parsedConfig;\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed during config creation.\n\t * This is useful for deployment and configuration management to know which env vars are required.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const config = envParser.create((get) => ({\n\t * dbUrl: get('DATABASE_URL').string(),\n\t * port: get('PORT').number()\n\t * }));\n\t *\n\t * config.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.envVars).sort();\n\t}\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\t/**\n\t * Set to track which environment variable names have been accessed\n\t */\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Creates a new EnvironmentParser instance.\n\t *\n\t * @param config - The configuration object to parse (typically process.env)\n\t */\n\tconstructor(private readonly config: T) {}\n\n\t/**\n\t * Wraps a Zod schema to intercept parse/safeParse calls and enrich error messages\n\t * with environment variable context.\n\t *\n\t * @param schema - The Zod schema to wrap\n\t * @param name - The environment variable name for error context\n\t * @returns A wrapped Zod schema with enhanced error reporting\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\t// Create a proxy that intercepts all method calls on the schema\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\ttry {\n\t\t\t\t\t\t\treturn target.parse(value);\n\t\t\t\t\t\t} catch (error) {\n\t\t\t\t\t\t\tif (error instanceof z.ZodError) {\n\t\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\t\tconst modifiedIssues = error.issues.map((issue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}));\n\t\t\t\t\t\t\t\tthrow new z.ZodError(modifiedIssues);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tthrow error;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => {\n\t\t\t\t\t\tconst value = get(this.config, name);\n\t\t\t\t\t\tconst result = target.safeParse(value);\n\n\t\t\t\t\t\tif (!result.success) {\n\t\t\t\t\t\t\t// Modify the error to include the environment variable name\n\t\t\t\t\t\t\tconst modifiedIssues = result.error.issues.map(\n\t\t\t\t\t\t\t\t(issue: z.core.$ZodIssue) => ({\n\t\t\t\t\t\t\t\t\t...issue,\n\t\t\t\t\t\t\t\t\tmessage: `Environment variable \"${name}\": ${issue.message}`,\n\t\t\t\t\t\t\t\t\tpath: [name, ...issue.path],\n\t\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\tsuccess: false as const,\n\t\t\t\t\t\t\t\terror: new z.ZodError(modifiedIssues),\n\t\t\t\t\t\t\t};\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\t// For any method that returns a new schema (like transform, optional, etc.),\n\t\t\t\t// wrap the result as well\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\t// If the result is a ZodType, wrap it too\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Creates a proxied version of the Zod object that wraps all schema creators\n\t * to provide enhanced error messages with environment variable context.\n\t *\n\t * @param name - The environment variable name\n\t * @returns A proxied Zod object with wrapped schema creators\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\t// Track that this environment variable was accessed\n\t\tthis.accessedVars.add(name);\n\n\t\t// Return an object that has all Zod schemas but with our wrapper\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// deno-lint-ignore ban-ts-comment\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\t// Return a wrapper around each Zod schema creator\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\t// Handle objects like z.coerce\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a new ConfigParser object that can be used to parse the config object\n\t *\n\t * @param builder - A function that takes a getter function and returns a config object\n\t * @returns A ConfigParser object that can be used to parse the config object\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new ConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns an array of environment variable names that were accessed via the getter.\n\t * This is useful for build-time analysis to determine which env vars a service needs.\n\t *\n\t * @returns Array of environment variable names, sorted alphabetically\n\t *\n\t * @example\n\t * ```typescript\n\t * const sniffer = new EnvironmentParser({});\n\t * service.register(sniffer);\n\t * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'PORT']\n\t * ```\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\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\t[K in keyof T]: T[K] extends z.ZodSchema\n\t\t? z.infer<T[K]>\n\t\t: T[K] extends Record<string, unknown>\n\t\t\t? InferConfig<T[K]>\n\t\t\t: 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\tname: 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\tget: 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":";;;;;;;;;;;AAUA,IAAa,eAAb,MAAyD;;;;;;;CAOxD,YACkBA,QACAC,0BAAuB,IAAI,OAC3C;EAFgB;EACA;CACd;;;;;CAKJ,QAAgC;EAC/B,MAAMC,SAA6B,CAAE;EAErC,MAAM,YAAY,CAAIC,QAAWC,OAAiB,CAAE,MAAK;GACxD,MAAMC,SAAsB,CAAE;AAE9B,OAAI,iBAAiB,WAAW,SAC/B,QAAO;AAGR,QAAK,MAAM,OAAO,QAAQ;IACzB,MAAM,SAAS,OAAO;IACtB,MAAM,cAAc,CAAC,GAAG,MAAM,GAAI;AAElC,QAAI,kBAAkB,EAAE,SAAS;KAChC,MA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K;CACrC;;;;;;;;;;;;;;CAeD,0BAAoC;AACnC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig } from "./EnvironmentParser-
|
|
1
|
+
import { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig } from "./EnvironmentParser-DtOL86NU.cjs";
|
|
2
2
|
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig };
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig } from "./EnvironmentParser-
|
|
1
|
+
import { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig } from "./EnvironmentParser-CY8TosTN.mjs";
|
|
2
2
|
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentBuilder, EnvironmentParser, InferConfig };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnifferEnvironmentParser.cjs","names":["schema: z.ZodType","name: string","z","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","ConfigParser","config: T","result: EmptyObject"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.cjs","names":["schema: z.ZodType","name: string","z","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","ConfigParser","config: T","result: EmptyObject"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n\tConfigParser,\n\ttype EmptyObject,\n\ttype EnvFetcher,\n} from './EnvironmentParser';\n\n/**\n * A specialized EnvironmentParser for build-time analysis that tracks\n * which environment variables are accessed without requiring actual values.\n *\n * Unlike the regular EnvironmentParser, the sniffer:\n * - Always returns mock values from .parse() and .safeParse()\n * - Never throws validation errors\n * - Tracks all accessed environment variable names\n *\n * This allows service registration to succeed during build-time analysis\n * even when environment variables are not set.\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * await service.register(sniffer); // Always succeeds\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']\n * ```\n */\nexport class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Wraps a Zod schema to always return mock values.\n\t * This ensures .parse() and .safeParse() never fail.\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => this.getMockValue(target);\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => ({\n\t\t\t\t\t\tsuccess: true as const,\n\t\t\t\t\t\tdata: this.getMockValue(target),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Returns a mock value based on the Zod schema type.\n\t */\n\tprivate getMockValue(schema: z.ZodType): unknown {\n\t\t// Return type-appropriate mock values\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\t// For object schemas, build mock object from shape\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getMockValue(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n\n\t/**\n\t * Creates a proxied Zod getter that tracks environment variable access.\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\tthis.accessedVars.add(name);\n\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a ConfigParser that will return mock values when parsed.\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new SnifferConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns all environment variable names that were accessed.\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * A ConfigParser that always succeeds with mock values.\n */\nclass SnifferConfigParser<\n\tTResponse extends EmptyObject,\n> extends ConfigParser<TResponse> {\n\toverride parse(): any {\n\t\treturn this.parseWithMocks(this.getConfig());\n\t}\n\n\tprivate getConfig(): TResponse {\n\t\t// Access the private config via any cast\n\t\treturn (this as any).config;\n\t}\n\n\tprivate parseWithMocks<T>(config: T): any {\n\t\tconst result: EmptyObject = {};\n\n\t\tif (config && typeof config !== 'object') {\n\t\t\treturn config;\n\t\t}\n\n\t\tfor (const key in config) {\n\t\t\tconst schema = config[key];\n\n\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t// Use safeParse which will return mock values from our wrapped schema\n\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\tresult[key] = parsed.success\n\t\t\t\t\t? parsed.data\n\t\t\t\t\t: this.getDefaultForSchema(schema);\n\t\t\t} else if (schema && typeof schema === 'object') {\n\t\t\t\tresult[key] = this.parseWithMocks(schema as EmptyObject);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate getDefaultForSchema(schema: z.ZodType): unknown {\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getDefaultForSchema(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAa,2BAAb,MAA4E;CAC3E,AAAiB,+BAA4B,IAAI;;;;;CAMjD,AAAQ,aAAa,CAACA,QAAmBC,SAA4B;AACpE,SAAO,IAAI,MAAM,QAAQ,EACxB,KAAK,CAAC,QAAQ,SAAS;AACtB,OAAI,SAAS,QACZ,QAAO,MAAM,KAAK,aAAa,OAAO;AAGvC,OAAI,SAAS,YACZ,QAAO,OAAO;IACb,SAAS;IACT,MAAM,KAAK,aAAa,OAAO;GAC/B;GAGF,MAAM,eAAe,OAAO;AAC5B,cAAW,iBAAiB,WAC3B,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,aAAa,MAAM,QAAQ,KAAK;AAC/C,QAAI,iBAAiB,WAAW,YAAY,WAAW,OACtD,QAAO,KAAK,WAAW,QAAQ,KAAK;AAErC,WAAO;GACP;AAGF,UAAO;EACP,EACD;CACD;;;;CAKD,AAAQ,aAAaD,QAA4B;AAEhD,MAAI,kBAAkBE,SAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkBA,SAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkBA,SAAE,WAAY,QAAO;AAC3C,MAAI,kBAAkBA,SAAE,SAAU,QAAO,CAAE;AAC3C,MAAI,kBAAkBA,SAAE,YAAa;AACrC,MAAI,kBAAkBA,SAAE,YAAa,QAAO;AAG5C,MAAI,kBAAkBA,SAAE,aAAa,OAAO,OAAO;GAClD,MAAMC,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,O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AAS;IAEhC,MAAM,SAAS,OAAO,iBAAoB;AAC1C,WAAO,OAAO,OAAO,UAClB,OAAO,OACP,KAAK,oBAAoB,OAAO;GACnC,WAAU,iBAAiB,WAAW,SACtC,QAAO,OAAO,KAAK,eAAe,OAAsB;EAEzD;AAED,SAAO;CACP;CAED,AAAQ,oBAAoBF,QAA4B;AACvD,MAAI,kBAAkBE,SAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkBA,SAAE,UAAW,QAAO;AAC1C,MAAI,kBAAkBA,SAAE,WAAY,QAAO;AAC3C,MAAI,kBAAkBA,SAAE,SAAU,QAAO,CAAE;AAC3C,MAAI,kBAAkBA,SAAE,YAAa;AACrC,MAAI,kBAAkBA,SAAE,YAAa,QAAO;AAE5C,MAAI,kBAAkBA,SAAE,aAAa,OAAO,OAAO;GAClD,MAAMC,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,MAAM,CACtD,KAAI,iBAAiBD,SAAE,QACtB,QAAO,OAAO,KAAK,oBAAoB,MAAM;AAG/C,UAAO;EACP;AAED,SAAO;CACP;AACD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConfigParser, EmptyObject, EnvFetcher } from "./EnvironmentParser-
|
|
1
|
+
import { ConfigParser, EmptyObject, EnvFetcher } from "./EnvironmentParser-DtOL86NU.cjs";
|
|
2
2
|
|
|
3
3
|
//#region src/SnifferEnvironmentParser.d.ts
|
|
4
4
|
|
|
@@ -21,7 +21,7 @@ import { ConfigParser, EmptyObject, EnvFetcher } from "./EnvironmentParser-tV-Jj
|
|
|
21
21
|
* const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
declare class SnifferEnvironmentParser<
|
|
24
|
+
declare class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {
|
|
25
25
|
private readonly accessedVars;
|
|
26
26
|
/**
|
|
27
27
|
* Wraps a Zod schema to always return mock values.
|
|
@@ -45,6 +45,7 @@ declare class SnifferEnvironmentParser<T extends EmptyObject = EmptyObject> {
|
|
|
45
45
|
*/
|
|
46
46
|
getEnvironmentVariables(): string[];
|
|
47
47
|
}
|
|
48
|
+
//# sourceMappingURL=SnifferEnvironmentParser.d.ts.map
|
|
48
49
|
//#endregion
|
|
49
50
|
export { SnifferEnvironmentParser };
|
|
50
51
|
//# sourceMappingURL=SnifferEnvironmentParser.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.d.cts","names":[],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AA0BA;;;;;;;;;AA8GgB;;;;;;;;cA9GH,oCAAoC,cAAc;;;;;;;;;;;;;;;;;;yBA4GvC,4BACP,eAAe,UAC5B,aAAa"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConfigParser, EmptyObject, EnvFetcher } from "./EnvironmentParser-
|
|
1
|
+
import { ConfigParser, EmptyObject, EnvFetcher } from "./EnvironmentParser-CY8TosTN.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/SnifferEnvironmentParser.d.ts
|
|
4
4
|
|
|
@@ -21,7 +21,7 @@ import { ConfigParser, EmptyObject, EnvFetcher } from "./EnvironmentParser-CVWU1
|
|
|
21
21
|
* const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']
|
|
22
22
|
* ```
|
|
23
23
|
*/
|
|
24
|
-
declare class SnifferEnvironmentParser<
|
|
24
|
+
declare class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {
|
|
25
25
|
private readonly accessedVars;
|
|
26
26
|
/**
|
|
27
27
|
* Wraps a Zod schema to always return mock values.
|
|
@@ -45,6 +45,7 @@ declare class SnifferEnvironmentParser<T extends EmptyObject = EmptyObject> {
|
|
|
45
45
|
*/
|
|
46
46
|
getEnvironmentVariables(): string[];
|
|
47
47
|
}
|
|
48
|
+
//# sourceMappingURL=SnifferEnvironmentParser.d.ts.map
|
|
48
49
|
//#endregion
|
|
49
50
|
export { SnifferEnvironmentParser };
|
|
50
51
|
//# sourceMappingURL=SnifferEnvironmentParser.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.d.mts","names":[],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AA0BA;;;;;;;;;AA8GgB;;;;;;;;cA9GH,oCAAoC,cAAc;;;;;;;;;;;;;;;;;;yBA4GvC,4BACP,eAAe,UAC5B,aAAa"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SnifferEnvironmentParser.mjs","names":["schema: z.ZodType","name: string","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","config: T","result: EmptyObject"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n
|
|
1
|
+
{"version":3,"file":"SnifferEnvironmentParser.mjs","names":["schema: z.ZodType","name: string","result: Record<string, unknown>","builder: (get: EnvFetcher) => TReturn","config: T","result: EmptyObject"],"sources":["../src/SnifferEnvironmentParser.ts"],"sourcesContent":["import { z } from 'zod/v4';\nimport {\n\tConfigParser,\n\ttype EmptyObject,\n\ttype EnvFetcher,\n} from './EnvironmentParser';\n\n/**\n * A specialized EnvironmentParser for build-time analysis that tracks\n * which environment variables are accessed without requiring actual values.\n *\n * Unlike the regular EnvironmentParser, the sniffer:\n * - Always returns mock values from .parse() and .safeParse()\n * - Never throws validation errors\n * - Tracks all accessed environment variable names\n *\n * This allows service registration to succeed during build-time analysis\n * even when environment variables are not set.\n *\n * @example\n * ```typescript\n * const sniffer = new SnifferEnvironmentParser();\n * await service.register(sniffer); // Always succeeds\n * const envVars = sniffer.getEnvironmentVariables(); // ['DATABASE_URL', 'API_KEY']\n * ```\n */\nexport class SnifferEnvironmentParser<_T extends EmptyObject = EmptyObject> {\n\tprivate readonly accessedVars: Set<string> = new Set();\n\n\t/**\n\t * Wraps a Zod schema to always return mock values.\n\t * This ensures .parse() and .safeParse() never fail.\n\t */\n\tprivate wrapSchema = (schema: z.ZodType, name: string): z.ZodType => {\n\t\treturn new Proxy(schema, {\n\t\t\tget: (target, prop) => {\n\t\t\t\tif (prop === 'parse') {\n\t\t\t\t\treturn () => this.getMockValue(target);\n\t\t\t\t}\n\n\t\t\t\tif (prop === 'safeParse') {\n\t\t\t\t\treturn () => ({\n\t\t\t\t\t\tsuccess: true as const,\n\t\t\t\t\t\tdata: this.getMockValue(target),\n\t\t\t\t\t});\n\t\t\t\t}\n\n\t\t\t\tconst originalProp = target[prop as keyof typeof target];\n\t\t\t\tif (typeof originalProp === 'function') {\n\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\tconst result = originalProp.apply(target, args);\n\t\t\t\t\t\tif (result && typeof result === 'object' && 'parse' in result) {\n\t\t\t\t\t\t\treturn this.wrapSchema(result, name);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t};\n\t\t\t\t}\n\n\t\t\t\treturn originalProp;\n\t\t\t},\n\t\t});\n\t};\n\n\t/**\n\t * Returns a mock value based on the Zod schema type.\n\t */\n\tprivate getMockValue(schema: z.ZodType): unknown {\n\t\t// Return type-appropriate mock values\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\t// For object schemas, build mock object from shape\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getMockValue(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n\n\t/**\n\t * Creates a proxied Zod getter that tracks environment variable access.\n\t */\n\tprivate getZodGetter = (name: string) => {\n\t\tthis.accessedVars.add(name);\n\n\t\treturn new Proxy(\n\t\t\t{ ...z },\n\t\t\t{\n\t\t\t\tget: (target, prop) => {\n\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\tconst value = target[prop];\n\n\t\t\t\t\tif (typeof value === 'function') {\n\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\tconst schema = value(...args);\n\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\n\t\t\t\t\tif (value && typeof value === 'object') {\n\t\t\t\t\t\treturn new Proxy(value, {\n\t\t\t\t\t\t\tget: (nestedTarget, nestedProp) => {\n\t\t\t\t\t\t\t\tconst nestedValue =\n\t\t\t\t\t\t\t\t\tnestedTarget[nestedProp as keyof typeof nestedTarget];\n\t\t\t\t\t\t\t\tif (typeof nestedValue === 'function') {\n\t\t\t\t\t\t\t\t\treturn (...args: any[]) => {\n\t\t\t\t\t\t\t\t\t\tconst schema = nestedValue(...args);\n\t\t\t\t\t\t\t\t\t\treturn this.wrapSchema(schema, name);\n\t\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\treturn nestedValue;\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\n\t\t\t\t\treturn value;\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\t};\n\n\t/**\n\t * Creates a ConfigParser that will return mock values when parsed.\n\t */\n\tcreate<TReturn extends EmptyObject>(\n\t\tbuilder: (get: EnvFetcher) => TReturn,\n\t): ConfigParser<TReturn> {\n\t\tconst config = builder(this.getZodGetter);\n\t\treturn new SnifferConfigParser(config, this.accessedVars);\n\t}\n\n\t/**\n\t * Returns all environment variable names that were accessed.\n\t */\n\tgetEnvironmentVariables(): string[] {\n\t\treturn Array.from(this.accessedVars).sort();\n\t}\n}\n\n/**\n * A ConfigParser that always succeeds with mock values.\n */\nclass SnifferConfigParser<\n\tTResponse extends EmptyObject,\n> extends ConfigParser<TResponse> {\n\toverride parse(): any {\n\t\treturn this.parseWithMocks(this.getConfig());\n\t}\n\n\tprivate getConfig(): TResponse {\n\t\t// Access the private config via any cast\n\t\treturn (this as any).config;\n\t}\n\n\tprivate parseWithMocks<T>(config: T): any {\n\t\tconst result: EmptyObject = {};\n\n\t\tif (config && typeof config !== 'object') {\n\t\t\treturn config;\n\t\t}\n\n\t\tfor (const key in config) {\n\t\t\tconst schema = config[key];\n\n\t\t\tif (schema instanceof z.ZodType) {\n\t\t\t\t// Use safeParse which will return mock values from our wrapped schema\n\t\t\t\tconst parsed = schema.safeParse(undefined);\n\t\t\t\tresult[key] = parsed.success\n\t\t\t\t\t? parsed.data\n\t\t\t\t\t: this.getDefaultForSchema(schema);\n\t\t\t} else if (schema && typeof schema === 'object') {\n\t\t\t\tresult[key] = this.parseWithMocks(schema as EmptyObject);\n\t\t\t}\n\t\t}\n\n\t\treturn result;\n\t}\n\n\tprivate getDefaultForSchema(schema: z.ZodType): unknown {\n\t\tif (schema instanceof z.ZodString) return '';\n\t\tif (schema instanceof z.ZodNumber) return 0;\n\t\tif (schema instanceof z.ZodBoolean) return false;\n\t\tif (schema instanceof z.ZodArray) return [];\n\t\tif (schema instanceof z.ZodOptional) return undefined;\n\t\tif (schema instanceof z.ZodNullable) return null;\n\n\t\tif (schema instanceof z.ZodObject && schema.shape) {\n\t\t\tconst result: Record<string, unknown> = {};\n\t\t\tfor (const [key, value] of Object.entries(schema.shape)) {\n\t\t\t\tif (value instanceof z.ZodType) {\n\t\t\t\t\tresult[key] = this.getDefaultForSchema(value);\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn result;\n\t\t}\n\n\t\treturn '';\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAA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AAE,WAAY,QAAO;AAC3C,MAAI,kBAAkB,EAAE,SAAU,QAAO,CAAE;AAC3C,MAAI,kBAAkB,EAAE,YAAa;AACrC,MAAI,kBAAkB,EAAE,YAAa,QAAO;AAE5C,MAAI,kBAAkB,EAAE,aAAa,OAAO,OAAO;GAClD,MAAME,SAAkC,CAAE;AAC1C,QAAK,MAAM,CAAC,KAAK,MAAM,IAAI,OAAO,QAAQ,OAAO,MAAM,CACtD,KAAI,iBAAiB,EAAE,QACtB,QAAO,OAAO,KAAK,oBAAoB,MAAM;AAG/C,UAAO;EACP;AAED,SAAO;CACP;AACD"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EnvironmentBuilder } from "./EnvironmentBuilder-
|
|
1
|
+
import { EnvironmentBuilder } from "./EnvironmentBuilder-BSuHZm0y.mjs";
|
|
2
2
|
|
|
3
3
|
//#region src/SstEnvironmentBuilder.ts
|
|
4
4
|
/**
|
|
@@ -105,4 +105,4 @@ var SstEnvironmentBuilder = class {
|
|
|
105
105
|
|
|
106
106
|
//#endregion
|
|
107
107
|
export { ResourceType, SstEnvironmentBuilder, sstResolvers };
|
|
108
|
-
//# sourceMappingURL=SstEnvironmentBuilder-
|
|
108
|
+
//# sourceMappingURL=SstEnvironmentBuilder-BEBFSUYr.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SstEnvironmentBuilder-BEBFSUYr.mjs","names":["name: string","value: SecretValue","key: string","value: PostgresValue","value: BucketValue","value: SnsTopicValue","sstResolvers: Resolvers","record: TRecord","additionalResolvers?: CustomResolvers<TRecord>","options?: EnvironmentBuilderOptions","mergedResolvers: Resolvers"],"sources":["../src/SstEnvironmentBuilder.ts"],"sourcesContent":["import {\n\tEnvironmentBuilder,\n\ttype EnvironmentBuilderOptions,\n\ttype EnvironmentResolver,\n\ttype EnvRecord,\n\ttype InputValue,\n\ttype Resolvers,\n} from './EnvironmentBuilder';\n\n/**\n * Enumeration of supported SST (Serverless Stack Toolkit) resource types.\n * Used to identify and process different AWS and SST resources.\n */\nexport enum ResourceType {\n\t// Legacy format (dot notation)\n\tApiGatewayV2 = 'sst.aws.ApiGatewayV2',\n\tPostgres = 'sst.aws.Postgres',\n\tFunction = 'sst.aws.Function',\n\tBucket = 'sst.aws.Bucket',\n\tVpc = 'sst.aws.Vpc',\n\tSecret = 'sst.sst.Secret',\n\n\t// Modern format (colon notation)\n\tSSTSecret = 'sst:sst:Secret',\n\tSSTFunction = 'sst:sst:Function',\n\tSSTApiGatewayV2 = 'sst:aws:ApiGatewayV2',\n\tSSTPostgres = 'sst:aws:Postgres',\n\tSSTBucket = 'sst:aws:Bucket',\n\tSnsTopic = 'sst:aws:SnsTopic',\n}\n\n/**\n * AWS API Gateway V2 resource type.\n * Represents an HTTP/WebSocket API.\n */\nexport type ApiGatewayV2 = {\n\ttype: ResourceType.ApiGatewayV2 | ResourceType.SSTApiGatewayV2;\n\turl: string;\n};\n\n/**\n * PostgreSQL database resource type.\n * Contains all connection details needed to connect to the database.\n */\nexport type Postgres = {\n\ttype: ResourceType.Postgres | ResourceType.SSTPostgres;\n\tdatabase: string;\n\thost: string;\n\tpassword: string;\n\tport: number;\n\tusername: string;\n};\n\n/**\n * AWS Lambda Function resource type.\n */\nexport type Function = {\n\ttype: ResourceType.Function | ResourceType.SSTFunction;\n\tname: string;\n};\n\n/**\n * AWS S3 Bucket resource type.\n */\nexport type Bucket = {\n\ttype: ResourceType.Bucket | ResourceType.SSTBucket;\n\tname: string;\n};\n\n/**\n * AWS VPC (Virtual Private Cloud) resource type.\n */\nexport type Vpc = {\n\ttype: ResourceType.Vpc;\n\tbastion: string;\n};\n\n/**\n * Secret resource type for storing sensitive values.\n */\nexport type Secret = {\n\ttype: ResourceType.Secret | ResourceType.SSTSecret;\n\tvalue: string;\n};\n\n/**\n * AWS SNS Topic resource type.\n */\nexport type SnsTopic = {\n\ttype: ResourceType.SnsTopic;\n\tarn: string;\n};\n\n/**\n * Union type of all supported SST resource types.\n */\nexport type SstResource =\n\t| ApiGatewayV2\n\t| Postgres\n\t| Function\n\t| Bucket\n\t| Vpc\n\t| Secret\n\t| SnsTopic;\n\n// Value types without the `type` key (for resolver parameters)\ntype SecretValue = Omit<Secret, 'type'>;\ntype PostgresValue = Omit<Postgres, 'type'>;\ntype BucketValue = Omit<Bucket, 'type'>;\ntype SnsTopicValue = Omit<SnsTopic, 'type'>;\n\n/**\n * Function type for processing a specific resource type into environment variables.\n *\n * @template K - The specific resource type (without `type` key)\n * @param name - The resource name\n * @param value - The resource value (without `type` key)\n * @returns Object mapping environment variable names to values\n */\nexport type ResourceProcessor<K> = (name: string, value: K) => EnvRecord;\n\n// SST Resource Resolvers (receive values without `type` key)\n\nconst secretResolver = (name: string, value: SecretValue) => ({\n\t[name]: value.value,\n});\n\nconst postgresResolver = (key: string, value: PostgresValue) => ({\n\t[`${key}Name`]: value.database,\n\t[`${key}Host`]: value.host,\n\t[`${key}Password`]: value.password,\n\t[`${key}Port`]: value.port,\n\t[`${key}Username`]: value.username,\n});\n\nconst bucketResolver = (name: string, value: BucketValue) => ({\n\t[`${name}Name`]: value.name,\n});\n\nconst topicResolver = (name: string, value: SnsTopicValue) => ({\n\t[`${name}Arn`]: value.arn,\n});\n\nconst noopResolver = () => ({});\n\n/**\n * Pre-configured resolvers for all SST resource types.\n */\nexport const sstResolvers: Resolvers = {\n\t// Legacy format\n\t[ResourceType.ApiGatewayV2]: noopResolver,\n\t[ResourceType.Function]: noopResolver,\n\t[ResourceType.Vpc]: noopResolver,\n\t[ResourceType.Secret]: secretResolver,\n\t[ResourceType.Postgres]: postgresResolver,\n\t[ResourceType.Bucket]: bucketResolver,\n\n\t// Modern format\n\t[ResourceType.SSTSecret]: secretResolver,\n\t[ResourceType.SSTBucket]: bucketResolver,\n\t[ResourceType.SSTFunction]: noopResolver,\n\t[ResourceType.SSTPostgres]: postgresResolver,\n\t[ResourceType.SSTApiGatewayV2]: noopResolver,\n\t[ResourceType.SnsTopic]: topicResolver,\n};\n\n/**\n * All known SST resource type strings.\n */\ntype SstResourceTypeString = `${ResourceType}`;\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\t[K in keyof TRecord]: ExtractType<TRecord[K]>;\n}[keyof TRecord];\n\n/**\n * Extracts only the custom (non-SST) type values from a record.\n */\ntype CustomTypeValues<TRecord extends Record<string, InputValue>> = Exclude<\n\tAllTypeValues<TRecord>,\n\tSstResourceTypeString\n>;\n\n/**\n * For a given type value, finds the corresponding value type (without `type` key).\n */\ntype ValueForType<\n\tTRecord extends Record<string, InputValue>,\n\tTType extends string,\n> = {\n\t[K in keyof TRecord]: TRecord[K] extends { type: TType }\n\t\t? OmitType<TRecord[K]>\n\t\t: never;\n}[keyof TRecord];\n\n/**\n * Generates typed resolvers for custom (non-SST) types in the input record.\n */\ntype CustomResolvers<TRecord extends Record<string, InputValue>> =\n\tCustomTypeValues<TRecord> extends never\n\t\t? Resolvers | undefined\n\t\t: {\n\t\t\t\t[TType in CustomTypeValues<TRecord>]: EnvironmentResolver<\n\t\t\t\t\tValueForType<TRecord, TType>\n\t\t\t\t>;\n\t\t\t};\n\n/**\n * SST-specific environment builder with built-in resolvers for all known\n * SST resource types.\n *\n * Wraps the generic EnvironmentBuilder with pre-configured SST resolvers.\n *\n * @template TRecord - The input record type for type inference\n *\n * @example\n * ```typescript\n * const env = new SstEnvironmentBuilder({\n * database: { type: 'sst:aws:Postgres', host: '...', ... },\n * apiKey: { type: 'sst:sst:Secret', value: 'secret' },\n * appName: 'my-app',\n * }).build();\n *\n * // With custom resolvers (typed based on input)\n * const env = new SstEnvironmentBuilder(\n * {\n * database: postgresResource,\n * custom: { type: 'my-custom' as const, data: 'foo' },\n * },\n * {\n * // TypeScript requires 'my-custom' resolver with typed value\n * 'my-custom': (key, value) => ({ [`${key}Data`]: value.data }),\n * }\n * ).build();\n * ```\n */\nexport class SstEnvironmentBuilder<\n\tTRecord extends Record<string, SstResource | InputValue | string>,\n> {\n\tprivate readonly builder: EnvironmentBuilder<\n\t\tRecord<string, InputValue>,\n\t\tResolvers\n\t>;\n\n\t/**\n\t * Create a new SST environment builder.\n\t *\n\t * @param record - Object containing SST resources, custom resources, and/or string values\n\t * @param additionalResolvers - Optional custom resolvers (typed based on custom types in record)\n\t * @param options - Optional configuration options\n\t */\n\tconstructor(\n\t\trecord: TRecord,\n\t\tadditionalResolvers?: CustomResolvers<TRecord>,\n\t\toptions?: EnvironmentBuilderOptions,\n\t) {\n\t\t// Merge resolvers with custom ones taking precedence\n\t\tconst mergedResolvers: Resolvers = additionalResolvers\n\t\t\t? { ...sstResolvers, ...additionalResolvers }\n\t\t\t: sstResolvers;\n\n\t\tthis.builder = new EnvironmentBuilder(\n\t\t\trecord as Record<string, InputValue>,\n\t\t\tmergedResolvers,\n\t\t\toptions,\n\t\t);\n\t}\n\n\t/**\n\t * Build environment variables from the input record.\n\t *\n\t * @returns A record of environment variables\n\t */\n\tbuild(): EnvRecord {\n\t\treturn this.builder.build();\n\t}\n}\n\nexport type {\n\tEnvironmentBuilderOptions,\n\tEnvRecord,\n\tEnvValue,\n} from './EnvironmentBuilder';\n// Re-export useful types\nexport { environmentCase } from './EnvironmentBuilder';\n"],"mappings":";;;;;;;AAaA,IAAY,wDAAL;AAEN;AACA;AACA;AACA;AACA;AACA;AAGA;AACA;AACA;AACA;AACA;AACA;;AACA;AA8FD,MAAM,iBAAiB,CAACA,MAAcC,WAAwB,GAC5D,OAAO,MAAM,MACd;AAED,MAAM,mBAAmB,CAACC,KAAaC,WAA0B;GAC9D,EAAE,IAAI,QAAQ,MAAM;GACpB,EAAE,IAAI,QAAQ,MAAM;GACpB,EAAE,IAAI,YAAY,MAAM;GACxB,EAAE,IAA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c;;;;;;;;;sBAepC,+BACc,gBAAgB,oBAC5B;;;;;;WAmBF"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const require_EnvironmentBuilder = require('./EnvironmentBuilder-
|
|
1
|
+
const require_EnvironmentBuilder = require('./EnvironmentBuilder-Djr1VsWM.cjs');
|
|
2
2
|
|
|
3
3
|
//#region src/SstEnvironmentBuilder.ts
|
|
4
4
|
/**
|
|
@@ -122,4 +122,4 @@ Object.defineProperty(exports, 'sstResolvers', {
|
|
|
122
122
|
return sstResolvers;
|
|
123
123
|
}
|
|
124
124
|
});
|
|
125
|
-
//# sourceMappingURL=SstEnvironmentBuilder-
|
|
125
|
+
//# sourceMappingURL=SstEnvironmentBuilder-wFnN2M5O.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SstEnvironmentBuilder-wFnN2M5O.cjs","names":["name: string","value: SecretValue","key: string","value: PostgresValue","value: BucketValue","value: SnsTopicValue","sstResolvers: Resolvers","record: TRecord","additionalResolvers?: CustomResolvers<TRecord>","options?: EnvironmentBuilderOptions","mergedResolvers: Resolvers","EnvironmentBuilder"],"sources":["../src/SstEnvironmentBuilder.ts"],"sourcesContent":["import {\n\tEnvironmentBuilder,\n\ttype EnvironmentBuilderOptions,\n\ttype EnvironmentResolver,\n\ttype EnvRecord,\n\ttype InputValue,\n\ttype Resolvers,\n} from './EnvironmentBuilder';\n\n/**\n * Enumeration of supported SST (Serverless Stack Toolkit) resource types.\n * Used to identify and process different AWS and SST resources.\n */\nexport enum ResourceType {\n\t// Legacy format (dot notation)\n\tApiGatewayV2 = 'sst.aws.ApiGatewayV2',\n\tPostgres = 'sst.aws.Postgres',\n\tFunction = 'sst.aws.Function',\n\tBucket = 'sst.aws.Bucket',\n\tVpc = 'sst.aws.Vpc',\n\tSecret = 'sst.sst.Secret',\n\n\t// Modern format (colon notation)\n\tSSTSecret = 'sst:sst:Secret',\n\tSSTFunction = 'sst:sst:Function',\n\tSSTApiGatewayV2 = 'sst:aws:ApiGatewayV2',\n\tSSTPostgres = 'sst:aws:Postgres',\n\tSSTBucket = 'sst:aws:Bucket',\n\tSnsTopic = 'sst:aws:SnsTopic',\n}\n\n/**\n * AWS API Gateway V2 resource type.\n * Represents an HTTP/WebSocket API.\n */\nexport type ApiGatewayV2 = {\n\ttype: ResourceType.ApiGatewayV2 | ResourceType.SSTApiGatewayV2;\n\turl: string;\n};\n\n/**\n * PostgreSQL database resource type.\n * Contains all connection details needed to connect to the database.\n */\nexport type Postgres = {\n\ttype: ResourceType.Postgres | ResourceType.SSTPostgres;\n\tdatabase: string;\n\thost: string;\n\tpassword: string;\n\tport: number;\n\tusername: string;\n};\n\n/**\n * AWS Lambda Function resource type.\n */\nexport type Function = {\n\ttype: ResourceType.Function | ResourceType.SSTFunction;\n\tname: string;\n};\n\n/**\n * AWS S3 Bucket resource type.\n */\nexport type Bucket = {\n\ttype: ResourceType.Bucket | ResourceType.SSTBucket;\n\tname: string;\n};\n\n/**\n * AWS VPC (Virtual Private Cloud) resource type.\n */\nexport type Vpc = {\n\ttype: ResourceType.Vpc;\n\tbastion: string;\n};\n\n/**\n * Secret resource type for storing sensitive values.\n */\nexport type Secret = {\n\ttype: ResourceType.Secret | ResourceType.SSTSecret;\n\tvalue: string;\n};\n\n/**\n * AWS SNS Topic resource type.\n */\nexport type SnsTopic = {\n\ttype: ResourceType.SnsTopic;\n\tarn: string;\n};\n\n/**\n * Union type of all supported SST resource types.\n */\nexport type SstResource =\n\t| ApiGatewayV2\n\t| Postgres\n\t| Function\n\t| Bucket\n\t| Vpc\n\t| Secret\n\t| SnsTopic;\n\n// Value types without the `type` key (for resolver parameters)\ntype SecretValue = Omit<Secret, 'type'>;\ntype PostgresValue = Omit<Postgres, 'type'>;\ntype BucketValue = Omit<Bucket, 'type'>;\ntype SnsTopicValue = Omit<SnsTopic, 'type'>;\n\n/**\n * Function type for processing a specific resource type into environment variables.\n *\n * @template K - The specific resource type (without `type` key)\n * @param name - The resource name\n * @param value - The resource value (without `type` key)\n * @returns Object mapping environment variable names to values\n */\nexport type ResourceProcessor<K> = (name: string, value: K) => EnvRecord;\n\n// SST Resource Resolvers (receive values without `type` key)\n\nconst secretResolver = (name: string, value: SecretValue) => ({\n\t[name]: value.value,\n});\n\nconst postgresResolver = (key: string, value: PostgresValue) => ({\n\t[`${key}Name`]: value.database,\n\t[`${key}Host`]: value.host,\n\t[`${key}Password`]: value.password,\n\t[`${key}Port`]: value.port,\n\t[`${key}Username`]: value.username,\n});\n\nconst bucketResolver = (name: string, value: BucketValue) => ({\n\t[`${name}Name`]: value.name,\n});\n\nconst topicResolver = (name: string, value: SnsTopicValue) => ({\n\t[`${name}Arn`]: value.arn,\n});\n\nconst noopResolver = () => ({});\n\n/**\n * Pre-configured resolvers for all SST resource types.\n */\nexport const sstResolvers: Resolvers = {\n\t// Legacy format\n\t[ResourceType.ApiGatewayV2]: noopResolver,\n\t[ResourceType.Function]: noopResolver,\n\t[ResourceType.Vpc]: noopResolver,\n\t[ResourceType.Secret]: secretResolver,\n\t[ResourceType.Postgres]: postgresResolver,\n\t[ResourceType.Bucket]: bucketResolver,\n\n\t// Modern format\n\t[ResourceType.SSTSecret]: secretResolver,\n\t[ResourceType.SSTBucket]: bucketResolver,\n\t[ResourceType.SSTFunction]: noopResolver,\n\t[ResourceType.SSTPostgres]: postgresResolver,\n\t[ResourceType.SSTApiGatewayV2]: noopResolver,\n\t[ResourceType.SnsTopic]: topicResolver,\n};\n\n/**\n * All known SST resource type strings.\n */\ntype SstResourceTypeString = `${ResourceType}`;\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\t[K in keyof TRecord]: ExtractType<TRecord[K]>;\n}[keyof TRecord];\n\n/**\n * Extracts only the custom (non-SST) type values from a record.\n */\ntype CustomTypeValues<TRecord extends Record<string, InputValue>> = Exclude<\n\tAllTypeValues<TRecord>,\n\tSstResourceTypeString\n>;\n\n/**\n * For a given type value, finds the corresponding value type (without `type` key).\n */\ntype ValueForType<\n\tTRecord extends Record<string, InputValue>,\n\tTType extends string,\n> = {\n\t[K in keyof TRecord]: TRecord[K] extends { type: TType }\n\t\t? OmitType<TRecord[K]>\n\t\t: never;\n}[keyof TRecord];\n\n/**\n * Generates typed resolvers for custom (non-SST) types in the input record.\n */\ntype CustomResolvers<TRecord extends Record<string, InputValue>> =\n\tCustomTypeValues<TRecord> extends never\n\t\t? Resolvers | undefined\n\t\t: {\n\t\t\t\t[TType in CustomTypeValues<TRecord>]: EnvironmentResolver<\n\t\t\t\t\tValueForType<TRecord, TType>\n\t\t\t\t>;\n\t\t\t};\n\n/**\n * SST-specific environment builder with built-in resolvers for all known\n * SST resource types.\n *\n * Wraps the generic EnvironmentBuilder with pre-configured SST resolvers.\n *\n * @template TRecord - The input record type for type inference\n *\n * @example\n * ```typescript\n * const env = new SstEnvironmentBuilder({\n * database: { type: 'sst:aws:Postgres', host: '...', ... },\n * apiKey: { type: 'sst:sst:Secret', value: 'secret' },\n * appName: 'my-app',\n * }).build();\n *\n * // With custom resolvers (typed based on input)\n * const env = new SstEnvironmentBuilder(\n * {\n * database: postgresResource,\n * custom: { type: 'my-custom' as const, data: 'foo' },\n * },\n * {\n * // TypeScript requires 'my-custom' resolver with typed value\n * 'my-custom': (key, value) => ({ [`${key}Data`]: value.data }),\n * }\n * ).build();\n * ```\n */\nexport class SstEnvironmentBuilder<\n\tTRecord extends Record<string, SstResource | InputValue | string>,\n> {\n\tprivate readonly builder: EnvironmentBuilder<\n\t\tRecord<string, InputValue>,\n\t\tResolvers\n\t>;\n\n\t/**\n\t * Create a new SST environment builder.\n\t *\n\t * @param record - Object containing SST resources, custom resources, and/or string values\n\t * @param additionalResolvers - Optional custom resolvers (typed based on custom types in record)\n\t * @param options - Optional configuration options\n\t */\n\tconstructor(\n\t\trecord: TRecord,\n\t\tadditionalResolvers?: CustomResolvers<TRecord>,\n\t\toptions?: EnvironmentBuilderOptions,\n\t) {\n\t\t// Merge resolvers with custom ones taking precedence\n\t\tconst mergedResolvers: Resolvers = additionalResolvers\n\t\t\t? { ...sstResolvers, ...additionalResolvers }\n\t\t\t: sstResolvers;\n\n\t\tthis.builder = new EnvironmentBuilder(\n\t\t\trecord as Record<string, InputValue>,\n\t\t\tmergedResolvers,\n\t\t\toptions,\n\t\t);\n\t}\n\n\t/**\n\t * Build environment variables from the input record.\n\t *\n\t * @returns A record of environment variables\n\t */\n\tbuild(): EnvRecord {\n\t\treturn this.builder.build();\n\t}\n}\n\nexport type {\n\tEnvironmentBuilderOptions,\n\tEnvRecord,\n\tEnvValue,\n} from './EnvironmentBuilder';\n// Re-export useful types\nexport { environmentCase } from './EnvironmentBuilder';\n"],"mappings":";;;;;;;AAaA,IAAY,wDAAL;AAEN;AACA;AACA;AACA;AACA;AACA;AAGA;AACA;AACA;AACA;AACA;AACA;;AACA;AA8FD,MAAM,iBAAiB,CAACA,MAAcC,WAAwB,GAC5D,OAAO,MAAM,MACd;AAED,MAAM,mBAAmB,CAACC,KAAaC,WAA0B;GAC9D,EAAE,IAAI,QAAQ,MAAM;GACpB,EAAE,IAAI,QAAQ,MAAM;GACpB,EAAE,IAAI,YAAY,MAAM;GACxB,EAAE,IAAI,QAAQ,MAAM;GACpB,EAAE,IAAI,YAAY,MAAM;AAC1B;AAED,MAAM,iBAAiB,CAACH,MAAcI,WAAwB,IAC3D,EAAE,KAAK,QAAQ,MAAM,KACvB;AAED,MAAM,gBAAgB,CAACJ,MAAcK,WAA0B,IAC5D,EAAE,KAAK,OAAO,MAAM,IACtB;AAED,MAAM,eAAe,OAAO,CAAE;;;;AAK9B,MAAaC,eAA0B;EAErC,aAAa,eAAe;EAC5B,aAAa,WAAW;EACxB,aAAa,MAAM;EACnB,aAAa,SAAS;EACtB,aAAa,WAAW;EACxB,aAAa,SAAS;EAGtB,aAAa,YAAY;EACzB,aAAa,YAAY;EACzB,aAAa,cAAc;EAC3B,aAAa,cAAc;EAC3B,aAAa,kBAAkB;EAC/B,aAAa,WAAW;AACzB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqFD,IAAa,wBAAb,MAEE;CACD,AAAiB;;;;;;;;CAYjB,YACCC,QACAC,qBACAC,SACC;EAED,MAAMC,kBAA6B,sBAChC;GAAE,GAAG;GAAc,GAAG;EAAqB,IAC3C;AAEH,OAAK,UAAU,IAAIC,8CAClB,QACA,iBACA;CAED;;;;;;CAOD,QAAmB;AAClB,SAAO,KAAK,QAAQ,OAAO;CAC3B;AACD"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const require_EnvironmentBuilder = require('./EnvironmentBuilder-
|
|
2
|
-
const require_SstEnvironmentBuilder = require('./SstEnvironmentBuilder-
|
|
1
|
+
const require_EnvironmentBuilder = require('./EnvironmentBuilder-Djr1VsWM.cjs');
|
|
2
|
+
const require_SstEnvironmentBuilder = require('./SstEnvironmentBuilder-wFnN2M5O.cjs');
|
|
3
3
|
|
|
4
4
|
exports.ResourceType = require_SstEnvironmentBuilder.ResourceType;
|
|
5
5
|
exports.SstEnvironmentBuilder = require_SstEnvironmentBuilder.SstEnvironmentBuilder;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { environmentCase } from "./EnvironmentBuilder-
|
|
2
|
-
import { ResourceType, SstEnvironmentBuilder, sstResolvers } from "./SstEnvironmentBuilder-
|
|
1
|
+
import { environmentCase } from "./EnvironmentBuilder-BSuHZm0y.mjs";
|
|
2
|
+
import { ResourceType, SstEnvironmentBuilder, sstResolvers } from "./SstEnvironmentBuilder-BEBFSUYr.mjs";
|
|
3
3
|
|
|
4
4
|
export { ResourceType, SstEnvironmentBuilder, environmentCase, sstResolvers };
|