@geekmidas/envkit 10.0.0-alpha.99 → 10.0.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/CHANGELOG.md +117 -0
- package/README.md +2 -2
- package/dist/{EnvironmentBuilder-Rxq657gm.mjs → EnvironmentBuilder-CFen3oIg.mjs} +6 -5
- package/dist/EnvironmentBuilder-CFen3oIg.mjs.map +1 -0
- package/dist/{EnvironmentBuilder-DTZ8o3se.d.cts → EnvironmentBuilder-CNgcdzSR.d.cts} +6 -6
- package/dist/EnvironmentBuilder-CNgcdzSR.d.cts.map +1 -0
- package/dist/{EnvironmentBuilder-wJH3EBbJ.cjs → EnvironmentBuilder-DDgLJAAo.cjs} +17 -17
- package/dist/EnvironmentBuilder-DDgLJAAo.cjs.map +1 -0
- package/dist/{EnvironmentBuilder-DTZ8o3se.d.mts → EnvironmentBuilder-DHfDXJUm.d.mts} +6 -6
- package/dist/EnvironmentBuilder-DHfDXJUm.d.mts.map +1 -0
- package/dist/{EnvironmentParser-BmJLlm5_.d.cts → EnvironmentParser-BY1oCUsc.d.mts} +15 -3
- package/dist/EnvironmentParser-BY1oCUsc.d.mts.map +1 -0
- package/dist/{EnvironmentParser-CmhDxs77.cjs → EnvironmentParser-Bzg5yXG2.cjs} +34 -34
- package/dist/EnvironmentParser-Bzg5yXG2.cjs.map +1 -0
- package/dist/{EnvironmentParser-BmJLlm5_.d.mts → EnvironmentParser-DoooZm8n.d.cts} +15 -3
- package/dist/EnvironmentParser-DoooZm8n.d.cts.map +1 -0
- package/dist/{EnvironmentParser-X6Z-jryw.mjs → EnvironmentParser-taAKfzT7.mjs} +12 -10
- package/dist/EnvironmentParser-taAKfzT7.mjs.map +1 -0
- package/dist/SnifferEnvironmentParser.cjs +7 -5
- package/dist/SnifferEnvironmentParser.cjs.map +1 -1
- package/dist/SnifferEnvironmentParser.d.cts +9 -5
- package/dist/SnifferEnvironmentParser.d.cts.map +1 -1
- package/dist/SnifferEnvironmentParser.d.mts +9 -5
- package/dist/SnifferEnvironmentParser.d.mts.map +1 -1
- package/dist/SnifferEnvironmentParser.mjs +5 -3
- package/dist/SnifferEnvironmentParser.mjs.map +1 -1
- package/dist/{rolldown-runtime-VH7oDXx4.cjs → chunk-CUT6urMc.cjs} +10 -8
- package/dist/credentials.cjs +11 -9
- package/dist/credentials.cjs.map +1 -1
- package/dist/credentials.d.cts +3 -3
- package/dist/credentials.d.cts.map +1 -1
- package/dist/credentials.d.mts +3 -3
- package/dist/credentials.d.mts.map +1 -1
- package/dist/credentials.mjs +8 -4
- package/dist/credentials.mjs.map +1 -1
- package/dist/index.cjs +4 -4
- package/dist/index.d.cts +7 -5
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +7 -5
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +4 -3
- package/dist/sst.cjs +48 -59
- package/dist/sst.cjs.map +1 -1
- package/dist/sst.d.cts +26 -35
- package/dist/sst.d.cts.map +1 -1
- package/dist/sst.d.mts +26 -35
- package/dist/sst.d.mts.map +1 -1
- package/dist/sst.mjs +48 -58
- package/dist/sst.mjs.map +1 -1
- package/docs/api-reference.md +302 -0
- package/docs/async-secrets-design.md +355 -0
- package/examples/basic-usage.ts +386 -0
- package/package.json +2 -9
- package/src/EnvironmentBuilder.ts +192 -0
- package/src/EnvironmentParser.ts +330 -0
- package/src/SnifferEnvironmentParser.ts +334 -0
- package/src/SstEnvValidator.ts +369 -0
- package/src/SstEnvironmentBuilder.ts +343 -0
- package/src/__tests__/ConfigParser.spec.ts +394 -0
- package/src/__tests__/EnvironmentBuilder.spec.ts +254 -0
- package/src/__tests__/EnvironmentParser.spec.ts +839 -0
- package/src/__tests__/SnifferEnvironmentParser.spec.ts +644 -0
- package/src/__tests__/SstEnvValidator.spec.ts +236 -0
- package/src/__tests__/SstEnvironmentBuilder.spec.ts +397 -0
- package/src/__tests__/credentials.integration.spec.ts +239 -0
- package/src/__tests__/credentials.spec.ts +136 -0
- package/src/__tests__/formatter.spec.ts +268 -0
- package/src/__tests__/sst.spec.ts +437 -0
- package/src/credentials.ts +112 -0
- package/src/formatter.ts +146 -0
- package/src/index.ts +24 -0
- package/src/sst.ts +76 -0
- package/sst-env.d.ts +8 -0
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +18 -0
- package/dist/EnvironmentBuilder-DTZ8o3se.d.cts.map +0 -1
- package/dist/EnvironmentBuilder-DTZ8o3se.d.mts.map +0 -1
- package/dist/EnvironmentBuilder-Rxq657gm.mjs.map +0 -1
- package/dist/EnvironmentBuilder-wJH3EBbJ.cjs.map +0 -1
- package/dist/EnvironmentParser-BmJLlm5_.d.cts.map +0 -1
- package/dist/EnvironmentParser-BmJLlm5_.d.mts.map +0 -1
- package/dist/EnvironmentParser-CmhDxs77.cjs.map +0 -1
- package/dist/EnvironmentParser-X6Z-jryw.mjs.map +0 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# @geekmidas/envkit
|
|
2
|
+
|
|
3
|
+
## 10.0.0
|
|
4
|
+
|
|
5
|
+
## 9.0.2
|
|
6
|
+
|
|
7
|
+
### Patch Changes
|
|
8
|
+
|
|
9
|
+
- [#12](https://github.com/geekmidas/toolbox/pull/12) [`d53863a`](https://github.com/geekmidas/toolbox/commit/d53863a84db2e4ab5420e08f79128b637043fc42) Thanks [@geekmidas](https://github.com/geekmidas)! - Align every published package on a single version and keep them in step.
|
|
10
|
+
|
|
11
|
+
All packages now share one version, enforced by a changesets `fixed` group. The
|
|
12
|
+
baseline is 9.0.1 — @geekmidas/client's published version — so nothing moves
|
|
13
|
+
backwards; this release takes the whole set to 9.0.2 together.
|
|
14
|
+
|
|
15
|
+
Independent versions made "which version of the docs applies to me"
|
|
16
|
+
unanswerable: a reader on constructs@7 and cli@2 was on no version at all. One
|
|
17
|
+
number per release makes versioned documentation possible, and lets 9 freeze as
|
|
18
|
+
the current paradigm while the constructs rework is developed against it.
|
|
19
|
+
|
|
20
|
+
Every release now publishes every package, and a major anywhere is a major
|
|
21
|
+
everywhere. Peer ranges get simpler in return.
|
|
22
|
+
|
|
23
|
+
## 1.1.1
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- 🐛 [#11](https://github.com/geekmidas/toolbox/pull/11) [`40f4dc0`](https://github.com/geekmidas/toolbox/commit/40f4dc095911b2223a255029d8f776caf7781309) Thanks [@geekmidas](https://github.com/geekmidas)! - Patch release across all packages to realign published versions with the
|
|
28
|
+
registry. The previous release only published the four packages that had
|
|
29
|
+
version bumps; the remaining packages failed with "cannot publish over the
|
|
30
|
+
previously published versions" because their versions were unchanged.
|
|
31
|
+
|
|
32
|
+
## 1.1.0
|
|
33
|
+
|
|
34
|
+
### Minor Changes
|
|
35
|
+
|
|
36
|
+
- ✨ [#8](https://github.com/geekmidas/toolbox/pull/8) [`7323f34`](https://github.com/geekmidas/toolbox/commit/7323f34176d63170dd53450889ac0b5959420c3c) Thanks [@geekmidas](https://github.com/geekmidas)! - feat(envkit): add Queue resolver and publisher connection strings (sst)
|
|
37
|
+
|
|
38
|
+
`@geekmidas/envkit/sst` gains a `Queue` resource type (`ResourceType.Queue` /
|
|
39
|
+
`SSTQueue`) whose resolver emits `<NAME>_URL`, `<NAME>_ARN`, and a
|
|
40
|
+
`<NAME>_PUBLISHER_CONNECTION_STRING` (`sqs://?queueUrl=…`). The SNS topic
|
|
41
|
+
resolver now also emits `<NAME>_PUBLISHER_CONNECTION_STRING` (`sns://?topicArn=…`).
|
|
42
|
+
|
|
43
|
+
These name-namespaced connection strings are what `@geekmidas/events`'
|
|
44
|
+
`Publisher.fromConnectionString` consumes, so a linked queue/topic resolves to a
|
|
45
|
+
ready-to-use publisher (the protocol selects the transport — SQS/SNS deployed,
|
|
46
|
+
or a local backend in dev).
|
|
47
|
+
|
|
48
|
+
- ✨ [#8](https://github.com/geekmidas/toolbox/pull/8) [`79e2929`](https://github.com/geekmidas/toolbox/commit/79e292978d3dbc8927e25814bdb051d1c380600a) Thanks [@geekmidas](https://github.com/geekmidas)! - feat(envkit): add an SST env-var validator to `@geekmidas/envkit/sst`
|
|
49
|
+
|
|
50
|
+
Adds `EnvValidator`, `resolveEnvKeys`, and `EnvValidationError` alongside
|
|
51
|
+
`SstEnvironmentBuilder`, so a deployable unit's required environment variables
|
|
52
|
+
can be validated **before** deploy — at `sst.config.ts` synth time.
|
|
53
|
+
- `resolveEnvKeys` derives the env-var keys a set of linked resources will
|
|
54
|
+
produce by replaying the **same** `sstResolvers` used at runtime (reduced to
|
|
55
|
+
the resource `type`, so no SST `Output` value is ever read). The infra-time
|
|
56
|
+
validator and the runtime resolution share a single source of truth — they
|
|
57
|
+
cannot drift, and there is no parallel suffix table to maintain.
|
|
58
|
+
- `EnvValidationError` is a structured, catchable error carrying `.missing`,
|
|
59
|
+
`.available`, `.suggestions`, and `.context`. Its message names the failing
|
|
60
|
+
unit and gives a nearest-match "did you mean DB_URL?" hint per missing
|
|
61
|
+
variable (edit-distance + token-overlap).
|
|
62
|
+
- Platform whitelists are **exported, never assumed** — SST deploys to AWS, GCP,
|
|
63
|
+
and Cloudflare. `AWS_RUNTIME_ENV_VARS`, `GCP_RUNTIME_ENV_VARS`,
|
|
64
|
+
`CLOUDFLARE_RUNTIME_ENV_VARS`, the `PLATFORM_ENV_VARS` registry, and
|
|
65
|
+
`platformEnvVars(platform)` are exported; the caller opts in via
|
|
66
|
+
`new EnvValidator(links, { platform })`. Optional vars are marked with a
|
|
67
|
+
trailing `?`.
|
|
68
|
+
- `getProvidersForEnvVars(requested)` returns the link names that provide a
|
|
69
|
+
requested var, for least-privilege linking (attach only the links a unit needs).
|
|
70
|
+
|
|
71
|
+
## 1.0.7
|
|
72
|
+
|
|
73
|
+
### Patch Changes
|
|
74
|
+
|
|
75
|
+
- 🐛 [`d70c6c0`](https://github.com/geekmidas/toolbox/commit/d70c6c0aeb8a79da2473ac77dbd8255a4a2f5651) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix `package.json` exports so TypeScript declarations resolve correctly under NodeNext/Bundler module resolution. Each subpath export now nests `types` inside its `import`/`require` condition, pointing at the `.d.mts` and `.d.cts` files that `tsdown` actually emits (previously the exports referenced non-existent `.d.ts` files, causing type-resolution failures for consumers). Both ESM (`.mjs`) and CJS (`.cjs`) runtime entry points are preserved. Additionally, `@geekmidas/ui` had `import` paths pointing at `.js` files that were never emitted — those are corrected to `.mjs`.
|
|
76
|
+
|
|
77
|
+
## 1.0.6
|
|
78
|
+
|
|
79
|
+
### Patch Changes
|
|
80
|
+
|
|
81
|
+
- ✨ [`e90d1fa`](https://github.com/geekmidas/toolbox/commit/e90d1fa0838769b346a98f0762c77295ef4fd09b) Thanks [@geekmidas](https://github.com/geekmidas)! - Add url support for database
|
|
82
|
+
|
|
83
|
+
## 1.0.5
|
|
84
|
+
|
|
85
|
+
### Patch Changes
|
|
86
|
+
|
|
87
|
+
- ✨ [`56e71bc`](https://github.com/geekmidas/toolbox/commit/56e71bcb57a5305270909f695a4539fa504a463b) Thanks [@geekmidas](https://github.com/geekmidas)! - Add optional params support and open api on build
|
|
88
|
+
|
|
89
|
+
## 1.0.4
|
|
90
|
+
|
|
91
|
+
### Patch Changes
|
|
92
|
+
|
|
93
|
+
- 🐛 [`a483d4c`](https://github.com/geekmidas/toolbox/commit/a483d4c193d27673ccad2aeed6f56b1c5708b5b4) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix credentials injection to work with esm/cjs
|
|
94
|
+
|
|
95
|
+
## 1.0.3
|
|
96
|
+
|
|
97
|
+
### Patch Changes
|
|
98
|
+
|
|
99
|
+
- ✨ [`bf094cd`](https://github.com/geekmidas/toolbox/commit/bf094cd29df2d18e75213f0976c0c9ff8047d14c) Thanks [@geekmidas](https://github.com/geekmidas)! - Add legacy dynamo resolution
|
|
100
|
+
|
|
101
|
+
## 1.0.2
|
|
102
|
+
|
|
103
|
+
### Patch Changes
|
|
104
|
+
|
|
105
|
+
- ✨ [`bfa41ad`](https://github.com/geekmidas/toolbox/commit/bfa41ad86c74c73a98e3922a536805fdf65d7607) Thanks [@geekmidas](https://github.com/geekmidas)! - Add support for dynamo links
|
|
106
|
+
|
|
107
|
+
## 1.0.1
|
|
108
|
+
|
|
109
|
+
### Patch Changes
|
|
110
|
+
|
|
111
|
+
- 🐛 [`8bdda11`](https://github.com/geekmidas/toolbox/commit/8bdda11f5c0f7c2eaea605befb0eca38ecc56e44) Thanks [@geekmidas](https://github.com/geekmidas)! - Fix iam resolution for authorizers and fixed exported types for envkit
|
|
112
|
+
|
|
113
|
+
## 1.0.0
|
|
114
|
+
|
|
115
|
+
### Major Changes
|
|
116
|
+
|
|
117
|
+
- [`ff7b115`](https://github.com/geekmidas/toolbox/commit/ff7b11599f60f84ac6cdc73714c853ecf786b2e8) Thanks [@geekmidas](https://github.com/geekmidas)! - Version 1 Stable release
|
package/README.md
CHANGED
|
@@ -249,12 +249,12 @@ import { ResourceType } from '@geekmidas/envkit/sst';
|
|
|
249
249
|
// Legacy format (dot notation)
|
|
250
250
|
ResourceType.Postgres // 'sst.aws.Postgres'
|
|
251
251
|
ResourceType.Secret // 'sst.sst.Secret'
|
|
252
|
-
ResourceType.
|
|
252
|
+
ResourceType.Bucket // 'sst.aws.Bucket'
|
|
253
253
|
|
|
254
254
|
// Modern format (colon notation)
|
|
255
255
|
ResourceType.SSTPostgres // 'sst:aws:Postgres'
|
|
256
256
|
ResourceType.SSTSecret // 'sst:sst:Secret'
|
|
257
|
-
ResourceType.
|
|
257
|
+
ResourceType.SSTBucket // 'sst:aws:Bucket'
|
|
258
258
|
ResourceType.SnsTopic // 'sst:aws:SnsTopic'
|
|
259
259
|
```
|
|
260
260
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import snakecase from "lodash.snakecase";
|
|
2
|
+
|
|
2
3
|
//#region src/EnvironmentBuilder.ts
|
|
3
4
|
/**
|
|
4
5
|
* Converts a string to environment variable case format (UPPER_SNAKE_CASE).
|
|
@@ -9,7 +10,7 @@ import snakecase from "lodash.snakecase";
|
|
|
9
10
|
*
|
|
10
11
|
* @example
|
|
11
12
|
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
12
|
-
* environmentCase('
|
|
13
|
+
* environmentCase('apiV2') // 'APIV2'
|
|
13
14
|
*/
|
|
14
15
|
function environmentCase(name) {
|
|
15
16
|
return snakecase(name).toUpperCase().replace(/_\d+/g, (r) => {
|
|
@@ -64,7 +65,7 @@ var EnvironmentBuilder = class {
|
|
|
64
65
|
env[environmentCase(key)] = value;
|
|
65
66
|
continue;
|
|
66
67
|
}
|
|
67
|
-
const { type
|
|
68
|
+
const { type,...rest } = value;
|
|
68
69
|
const resolver = this.resolvers[type];
|
|
69
70
|
if (resolver) {
|
|
70
71
|
const resolved = resolver(key, rest);
|
|
@@ -74,7 +75,7 @@ var EnvironmentBuilder = class {
|
|
|
74
75
|
return env;
|
|
75
76
|
}
|
|
76
77
|
};
|
|
77
|
-
//#endregion
|
|
78
|
-
export { environmentCase as n, EnvironmentBuilder as t };
|
|
79
78
|
|
|
80
|
-
//#
|
|
79
|
+
//#endregion
|
|
80
|
+
export { EnvironmentBuilder, environmentCase };
|
|
81
|
+
//# sourceMappingURL=EnvironmentBuilder-CFen3oIg.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-CFen3oIg.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,KAAI,gBAAgB,YAAY,IAAI;GAErC,MACA,MAAK,QAAQ,iBAAiB,KAAK,MAAM;EAE1C;AAED,SAAO;CACP;AACD"}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
11
|
-
* environmentCase('
|
|
11
|
+
* environmentCase('apiV2') // 'APIV2'
|
|
12
12
|
*/
|
|
13
13
|
declare function environmentCase(name: string): string;
|
|
14
14
|
/**
|
|
@@ -75,18 +75,18 @@ type OmitType<T> = T extends {
|
|
|
75
75
|
/**
|
|
76
76
|
* Extracts all unique `type` values from a record (excluding plain strings).
|
|
77
77
|
*/
|
|
78
|
-
type AllTypeValues<TRecord extends Record<string, InputValue>> = { [K in keyof TRecord]: ExtractType<TRecord[K]
|
|
78
|
+
type AllTypeValues<TRecord extends Record<string, InputValue>> = { [K in keyof TRecord]: ExtractType<TRecord[K]> }[keyof TRecord];
|
|
79
79
|
/**
|
|
80
80
|
* For a given type value, finds the corresponding value type (without `type` key).
|
|
81
81
|
*/
|
|
82
82
|
type ValueForType<TRecord extends Record<string, InputValue>, TType extends string> = { [K in keyof TRecord]: TRecord[K] extends {
|
|
83
83
|
type: TType;
|
|
84
|
-
} ? OmitType<TRecord[K]> : never
|
|
84
|
+
} ? OmitType<TRecord[K]> : never }[keyof TRecord];
|
|
85
85
|
/**
|
|
86
86
|
* Generates typed resolvers based on the input record.
|
|
87
87
|
* Keys are the `type` values, values are resolver functions receiving the value without `type`.
|
|
88
88
|
*/
|
|
89
|
-
type TypedResolvers<TRecord extends Record<string, InputValue>> = { [TType in AllTypeValues<TRecord>]: EnvironmentResolver<ValueForType<TRecord, TType
|
|
89
|
+
type TypedResolvers<TRecord extends Record<string, InputValue>> = { [TType in AllTypeValues<TRecord>]: EnvironmentResolver<ValueForType<TRecord, TType>> };
|
|
90
90
|
/**
|
|
91
91
|
* A generic, extensible class for building environment variables from
|
|
92
92
|
* objects with type-discriminated values.
|
|
@@ -127,5 +127,5 @@ declare class EnvironmentBuilder<TRecord extends Record<string, InputValue> = Re
|
|
|
127
127
|
build(): EnvRecord;
|
|
128
128
|
}
|
|
129
129
|
//#endregion
|
|
130
|
-
export {
|
|
131
|
-
//# sourceMappingURL=EnvironmentBuilder-
|
|
130
|
+
export { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase };
|
|
131
|
+
//# sourceMappingURL=EnvironmentBuilder-CNgcdzSR.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-CNgcdzSR.d.cts","names":[],"sources":["../src/EnvironmentBuilder.ts"],"sourcesContent":[],"mappings":";;AAaA;AAYA;AAQA;AAUA;;;;AAA+E;AAK/E;;AAAuC,iBAnCvB,eAAA,CAmCuB,IAAA,EAAA,MAAA,CAAA,EAAA,MAAA;;AAAT;AAK9B;AAWA;AAKY,UA5CK,SAAA,CA4CU;EAQtB,CAAA,GAAA,EAAA,MAAA,CAAA,EAnDW,QAmDA;;;;AAAoD;AAAA;AAKvD,KAjDD,QAAA,GAiDC,MAAA,GAAA,MAAA,GAAA,OAAA,GAjDsC,SAiDtC;;;;AAAuC;AAAA;;;;AAMvC,KA7CD,mBA6CC,CAAA,IAAA,GAAA,CAAA,GAAA,CAAA,GAAA,EAAA,MAAA,EAAA,KAAA,EA7CmD,CA6CnD,EAAA,GA7CyD,SA6CzD;;;;AACL,KAzCI,SAAA,GAAY,MAyChB,CAAA,MAAA,EAzC+B,mBAyC/B,CAAA,GAAA,CAAA,CAAA;AAAO;AAAA;;AAMiB,UA1Cf,yBAAA,CA0Ce;EAAU;;;;EAGV,gBAAkB,CAAA,EAAA,CAAA,GAAA,EAAA,MAAA,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,6 +1,6 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const lodash_snakecase = require_chunk.__toESM(require("lodash.snakecase"));
|
|
3
|
+
|
|
4
4
|
//#region src/EnvironmentBuilder.ts
|
|
5
5
|
/**
|
|
6
6
|
* Converts a string to environment variable case format (UPPER_SNAKE_CASE).
|
|
@@ -11,7 +11,7 @@ lodash_snakecase = require_rolldown_runtime.__toESM(lodash_snakecase, 1);
|
|
|
11
11
|
*
|
|
12
12
|
* @example
|
|
13
13
|
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
14
|
-
* environmentCase('
|
|
14
|
+
* environmentCase('apiV2') // 'APIV2'
|
|
15
15
|
*/
|
|
16
16
|
function environmentCase(name) {
|
|
17
17
|
return (0, lodash_snakecase.default)(name).toUpperCase().replace(/_\d+/g, (r) => {
|
|
@@ -66,7 +66,7 @@ var EnvironmentBuilder = class {
|
|
|
66
66
|
env[environmentCase(key)] = value;
|
|
67
67
|
continue;
|
|
68
68
|
}
|
|
69
|
-
const { type
|
|
69
|
+
const { type,...rest } = value;
|
|
70
70
|
const resolver = this.resolvers[type];
|
|
71
71
|
if (resolver) {
|
|
72
72
|
const resolved = resolver(key, rest);
|
|
@@ -76,18 +76,18 @@ var EnvironmentBuilder = class {
|
|
|
76
76
|
return env;
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
|
+
|
|
79
80
|
//#endregion
|
|
80
|
-
Object.defineProperty(exports,
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
Object.defineProperty(exports, 'EnvironmentBuilder', {
|
|
82
|
+
enumerable: true,
|
|
83
|
+
get: function () {
|
|
84
|
+
return EnvironmentBuilder;
|
|
85
|
+
}
|
|
85
86
|
});
|
|
86
|
-
Object.defineProperty(exports,
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
87
|
+
Object.defineProperty(exports, 'environmentCase', {
|
|
88
|
+
enumerable: true,
|
|
89
|
+
get: function () {
|
|
90
|
+
return environmentCase;
|
|
91
|
+
}
|
|
91
92
|
});
|
|
92
|
-
|
|
93
|
-
//# sourceMappingURL=EnvironmentBuilder-wJH3EBbJ.cjs.map
|
|
93
|
+
//# sourceMappingURL=EnvironmentBuilder-DDgLJAAo.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-DDgLJAAo.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,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,KAAI,gBAAgB,YAAY,IAAI;GAErC,MACA,MAAK,QAAQ,iBAAiB,KAAK,MAAM;EAE1C;AAED,SAAO;CACP;AACD"}
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* environmentCase('myVariable') // 'MY_VARIABLE'
|
|
11
|
-
* environmentCase('
|
|
11
|
+
* environmentCase('apiV2') // 'APIV2'
|
|
12
12
|
*/
|
|
13
13
|
declare function environmentCase(name: string): string;
|
|
14
14
|
/**
|
|
@@ -75,18 +75,18 @@ type OmitType<T> = T extends {
|
|
|
75
75
|
/**
|
|
76
76
|
* Extracts all unique `type` values from a record (excluding plain strings).
|
|
77
77
|
*/
|
|
78
|
-
type AllTypeValues<TRecord extends Record<string, InputValue>> = { [K in keyof TRecord]: ExtractType<TRecord[K]
|
|
78
|
+
type AllTypeValues<TRecord extends Record<string, InputValue>> = { [K in keyof TRecord]: ExtractType<TRecord[K]> }[keyof TRecord];
|
|
79
79
|
/**
|
|
80
80
|
* For a given type value, finds the corresponding value type (without `type` key).
|
|
81
81
|
*/
|
|
82
82
|
type ValueForType<TRecord extends Record<string, InputValue>, TType extends string> = { [K in keyof TRecord]: TRecord[K] extends {
|
|
83
83
|
type: TType;
|
|
84
|
-
} ? OmitType<TRecord[K]> : never
|
|
84
|
+
} ? OmitType<TRecord[K]> : never }[keyof TRecord];
|
|
85
85
|
/**
|
|
86
86
|
* Generates typed resolvers based on the input record.
|
|
87
87
|
* Keys are the `type` values, values are resolver functions receiving the value without `type`.
|
|
88
88
|
*/
|
|
89
|
-
type TypedResolvers<TRecord extends Record<string, InputValue>> = { [TType in AllTypeValues<TRecord>]: EnvironmentResolver<ValueForType<TRecord, TType
|
|
89
|
+
type TypedResolvers<TRecord extends Record<string, InputValue>> = { [TType in AllTypeValues<TRecord>]: EnvironmentResolver<ValueForType<TRecord, TType>> };
|
|
90
90
|
/**
|
|
91
91
|
* A generic, extensible class for building environment variables from
|
|
92
92
|
* objects with type-discriminated values.
|
|
@@ -127,5 +127,5 @@ declare class EnvironmentBuilder<TRecord extends Record<string, InputValue> = Re
|
|
|
127
127
|
build(): EnvRecord;
|
|
128
128
|
}
|
|
129
129
|
//#endregion
|
|
130
|
-
export {
|
|
131
|
-
//# sourceMappingURL=EnvironmentBuilder-
|
|
130
|
+
export { EnvRecord, EnvValue, EnvironmentBuilder, EnvironmentBuilderOptions, EnvironmentResolver, InputValue, Resolvers, TypedInputValue, TypedResolvers, environmentCase };
|
|
131
|
+
//# sourceMappingURL=EnvironmentBuilder-DHfDXJUm.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentBuilder-DHfDXJUm.d.mts","names":[],"sources":["../src/EnvironmentBuilder.ts"],"sourcesContent":[],"mappings":";;AAaA;AAYA;AAQA;AAUA;;;;AAA+E;AAK/E;;AAAuC,iBAnCvB,eAAA,CAmCuB,IAAA,EAAA,MAAA,CAAA,EAAA,MAAA;;AAAT;AAK9B;AAWA;AAKY,UA5CK,SAAA,CA4CU;EAQtB,CAAA,GAAA,EAAA,MAAA,CAAA,EAnDW,QAmDA;;;;AAAoD;AAAA;AAKvD,KAjDD,QAAA,GAiDC,MAAA,GAAA,MAAA,GAAA,OAAA,GAjDsC,SAiDtC;;;;AAAuC;AAAA;;;;AAMvC,KA7CD,mBA6CC,CAAA,IAAA,GAAA,CAAA,GAAA,CAAA,GAAA,EAAA,MAAA,EAAA,KAAA,EA7CmD,CA6CnD,EAAA,GA7Cy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nvironmentParser.d.ts
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Parses and validates configuration objects against Zod schemas.
|
|
5
7
|
* Handles nested configurations and aggregates validation errors.
|
|
@@ -115,7 +117,7 @@ declare class EnvironmentParser<T extends EmptyObject> {
|
|
|
115
117
|
*
|
|
116
118
|
* @template T - The configuration object type
|
|
117
119
|
*/
|
|
118
|
-
type InferConfig<T extends EmptyObject> = { [K in keyof T]: T[K] extends z.ZodSchema ? z.infer<T[K]> : T[K] extends Record<string, unknown> ? InferConfig<T[K]> : T[K]
|
|
120
|
+
type InferConfig<T extends EmptyObject> = { [K in keyof T]: T[K] extends z.ZodSchema ? z.infer<T[K]> : T[K] extends Record<string, unknown> ? InferConfig<T[K]> : T[K] };
|
|
119
121
|
/**
|
|
120
122
|
* Function type for fetching environment variables with Zod validation.
|
|
121
123
|
* Returns a Zod object scoped to a specific environment variable.
|
|
@@ -125,11 +127,21 @@ type InferConfig<T extends EmptyObject> = { [K in keyof T]: T[K] extends z.ZodSc
|
|
|
125
127
|
* @returns A Zod object for defining the schema
|
|
126
128
|
*/
|
|
127
129
|
type EnvFetcher<TPath extends string = string> = (name: TPath) => typeof z;
|
|
130
|
+
/**
|
|
131
|
+
* Function type for building environment configuration objects.
|
|
132
|
+
* Takes an EnvFetcher and returns a configuration object with Zod schemas.
|
|
133
|
+
*
|
|
134
|
+
* @template TResponse - The response configuration object type
|
|
135
|
+
* @param get - The environment variable fetcher function
|
|
136
|
+
* @returns The configuration object with Zod schemas
|
|
137
|
+
*/
|
|
138
|
+
|
|
128
139
|
/**
|
|
129
140
|
* Type alias for a generic object with unknown values.
|
|
130
141
|
* Used as a constraint for configuration objects.
|
|
131
142
|
*/
|
|
132
143
|
type EmptyObject = Record<string | number | symbol, unknown>;
|
|
144
|
+
//# sourceMappingURL=EnvironmentParser.d.ts.map
|
|
133
145
|
//#endregion
|
|
134
|
-
export {
|
|
135
|
-
//# sourceMappingURL=EnvironmentParser-
|
|
146
|
+
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentParser, InferConfig };
|
|
147
|
+
//# sourceMappingURL=EnvironmentParser-BY1oCUsc.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-BY1oCUsc.d.mts","names":[],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AAWA;;;;AAS4B,cATf,YASe,CAAA,kBATgB,WAShB,CAAA,CAAA;EAAG,iBAMT,MAAA;EAAS,iBAArB,OAAA;EAAW;AA2FrB;;;;;EAiJmC,WAClB,CAAA,MAAA,EApPU,SAoPV,EAAA,OAAA,CAAA,EAnPW,GAmPX,CAAA,MAAA,CAAA;EAAU;;;AACX;EA6BJ,KAAA,CAAA,CAAA,EA3QF,WA2Qa,CA3QD,SA2QC,CAAA;EAAA;;;;;;;;;;;;;;;;EAKjB,uBAAC,CAAA,CAAA,EAAA,MAAA,EAAA;AAAC;AAWR;;;;AAEa;AAkBb;;;;;;;;;;;;;;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;;;;;;;;;;;;;;KAkBA,WAAA,GAAc"}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
lodash_get =
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
let zod_v4 = require("zod/v4");
|
|
1
|
+
const require_chunk = require('./chunk-CUT6urMc.cjs');
|
|
2
|
+
const zod_v4 = require_chunk.__toESM(require("zod/v4"));
|
|
3
|
+
const lodash_get = require_chunk.__toESM(require("lodash.get"));
|
|
4
|
+
const lodash_set = require_chunk.__toESM(require("lodash.set"));
|
|
5
|
+
|
|
7
6
|
//#region src/formatter.ts
|
|
8
7
|
/**
|
|
9
8
|
* ANSI color codes for terminal output.
|
|
@@ -48,7 +47,8 @@ const colors = {
|
|
|
48
47
|
* ```
|
|
49
48
|
*/
|
|
50
49
|
function formatParseError(error, options = {}) {
|
|
51
|
-
const
|
|
50
|
+
const useColors = options.colors ?? (process.stdout?.isTTY && process.env.NO_COLOR == null);
|
|
51
|
+
const c = useColors ? colors : {
|
|
52
52
|
reset: "",
|
|
53
53
|
red: "",
|
|
54
54
|
yellow: "",
|
|
@@ -66,7 +66,8 @@ function formatParseError(error, options = {}) {
|
|
|
66
66
|
if (match?.[1]) envName = match[1];
|
|
67
67
|
}
|
|
68
68
|
const received = "received" in issue ? issue.received : void 0;
|
|
69
|
-
|
|
69
|
+
const isMissing = issue.code === "invalid_type" && (received === "undefined" || received === "null");
|
|
70
|
+
if (isMissing) missingVars.push({
|
|
70
71
|
name: envName || "Unknown",
|
|
71
72
|
message: cleanMessage(issue.message)
|
|
72
73
|
});
|
|
@@ -109,6 +110,7 @@ function isDevelopment() {
|
|
|
109
110
|
const nodeEnv = process.env.NODE_ENV?.toLowerCase();
|
|
110
111
|
return nodeEnv == null || nodeEnv === "development" || nodeEnv === "dev";
|
|
111
112
|
}
|
|
113
|
+
|
|
112
114
|
//#endregion
|
|
113
115
|
//#region src/EnvironmentParser.ts
|
|
114
116
|
/**
|
|
@@ -118,8 +120,6 @@ function isDevelopment() {
|
|
|
118
120
|
* @template TResponse - The shape of the configuration object
|
|
119
121
|
*/
|
|
120
122
|
var ConfigParser = class {
|
|
121
|
-
config;
|
|
122
|
-
envVars;
|
|
123
123
|
/**
|
|
124
124
|
* Creates a new ConfigParser instance.
|
|
125
125
|
*
|
|
@@ -201,7 +201,6 @@ var ConfigParser = class {
|
|
|
201
201
|
* ```
|
|
202
202
|
*/
|
|
203
203
|
var EnvironmentParser = class {
|
|
204
|
-
config;
|
|
205
204
|
/**
|
|
206
205
|
* Set to track which environment variable names have been accessed
|
|
207
206
|
*/
|
|
@@ -298,7 +297,8 @@ var EnvironmentParser = class {
|
|
|
298
297
|
* @returns A ConfigParser object that can be used to parse the config object
|
|
299
298
|
*/
|
|
300
299
|
create(builder) {
|
|
301
|
-
|
|
300
|
+
const config = builder(this.getZodGetter);
|
|
301
|
+
return new ConfigParser(config, this.accessedVars);
|
|
302
302
|
}
|
|
303
303
|
/**
|
|
304
304
|
* Returns an array of environment variable names that were accessed via the getter.
|
|
@@ -317,30 +317,30 @@ var EnvironmentParser = class {
|
|
|
317
317
|
return Array.from(this.accessedVars).sort();
|
|
318
318
|
}
|
|
319
319
|
};
|
|
320
|
+
|
|
320
321
|
//#endregion
|
|
321
|
-
Object.defineProperty(exports,
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
322
|
+
Object.defineProperty(exports, 'ConfigParser', {
|
|
323
|
+
enumerable: true,
|
|
324
|
+
get: function () {
|
|
325
|
+
return ConfigParser;
|
|
326
|
+
}
|
|
326
327
|
});
|
|
327
|
-
Object.defineProperty(exports,
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
328
|
+
Object.defineProperty(exports, 'EnvironmentParser', {
|
|
329
|
+
enumerable: true,
|
|
330
|
+
get: function () {
|
|
331
|
+
return EnvironmentParser;
|
|
332
|
+
}
|
|
332
333
|
});
|
|
333
|
-
Object.defineProperty(exports,
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
334
|
+
Object.defineProperty(exports, 'formatParseError', {
|
|
335
|
+
enumerable: true,
|
|
336
|
+
get: function () {
|
|
337
|
+
return formatParseError;
|
|
338
|
+
}
|
|
338
339
|
});
|
|
339
|
-
Object.defineProperty(exports,
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
340
|
+
Object.defineProperty(exports, 'isDevelopment', {
|
|
341
|
+
enumerable: true,
|
|
342
|
+
get: function () {
|
|
343
|
+
return isDevelopment;
|
|
344
|
+
}
|
|
344
345
|
});
|
|
345
|
-
|
|
346
|
-
//# sourceMappingURL=EnvironmentParser-CmhDxs77.cjs.map
|
|
346
|
+
//# sourceMappingURL=EnvironmentParser-Bzg5yXG2.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-Bzg5yXG2.cjs","names":["error: z.ZodError","options: FormatOptions","missingVars: Array<{ name: string; message: string }>","invalidVars: Array<{ name: string; value: unknown; message: string }>","lines: string[]","message: string","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/formatter.ts","../src/EnvironmentParser.ts"],"sourcesContent":["import type { z } from 'zod/v4';\n\n/**\n * Options for formatting parse errors.\n */\nexport interface FormatOptions {\n\t/** Whether to use colors in output. Defaults to auto-detect TTY. */\n\tcolors?: boolean;\n}\n\n/**\n * ANSI color codes for terminal output.\n */\nconst colors = {\n\treset: '\\x1b[0m',\n\tred: '\\x1b[31m',\n\tyellow: '\\x1b[33m',\n\tcyan: '\\x1b[36m',\n\tdim: '\\x1b[2m',\n\tbold: '\\x1b[1m',\n};\n\n/**\n * Formats a ZodError into a user-friendly string for development.\n *\n * @param error - The ZodError to format\n * @param options - Formatting options\n * @returns Formatted error message\n *\n * @example\n * ```typescript\n * try {\n * config.parse();\n * } catch (error) {\n * if (error instanceof ZodError) {\n * console.error(formatParseError(error));\n * }\n * }\n * ```\n *\n * Output:\n * ```\n * Environment Configuration Failed\n *\n * Missing Variables:\n * DATABASE_URL - Required\n * JWT_SECRET - Required\n *\n * Invalid Values:\n * NODE_ENV = \"invalid\"\n * Expected: \"development\" | \"staging\" | \"production\"\n * ```\n */\nexport function formatParseError(\n\terror: z.ZodError,\n\toptions: FormatOptions = {},\n): string {\n\tconst useColors =\n\t\toptions.colors ?? (process.stdout?.isTTY && process.env.NO_COLOR == null);\n\n\tconst c = useColors\n\t\t? colors\n\t\t: { reset: '', red: '', yellow: '', cyan: '', dim: '', bold: '' };\n\n\tconst missingVars: Array<{ name: string; message: string }> = [];\n\tconst invalidVars: Array<{ name: string; value: unknown; message: string }> =\n\t\t[];\n\n\tfor (const issue of error.issues) {\n\t\t// Extract environment variable name from path or message\n\t\tlet envName = '';\n\t\tif (issue.path.length > 0) {\n\t\t\t// Join the full path with '.' to show nested config keys and env var name\n\t\t\tenvName = issue.path.map(String).join('.');\n\t\t} else {\n\t\t\t// Try to extract from message like 'Environment variable \"NAME\": ...'\n\t\t\tconst match = issue.message.match(/Environment variable \"([^\"]+)\"/);\n\t\t\tif (match?.[1]) {\n\t\t\t\tenvName = match[1];\n\t\t\t}\n\t\t}\n\n\t\t// Determine if this is a missing or invalid value\n\t\t// Use type guard for received property\n\t\tconst received = 'received' in issue ? issue.received : undefined;\n\t\tconst isMissing =\n\t\t\tissue.code === 'invalid_type' &&\n\t\t\t(received === 'undefined' || received === 'null');\n\n\t\tif (isMissing) {\n\t\t\tmissingVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t} else {\n\t\t\tinvalidVars.push({\n\t\t\t\tname: envName || 'Unknown',\n\t\t\t\tvalue: received,\n\t\t\t\tmessage: cleanMessage(issue.message),\n\t\t\t});\n\t\t}\n\t}\n\n\tconst lines: string[] = [];\n\n\tlines.push('');\n\tlines.push(`${c.red}${c.bold}Environment Configuration Failed${c.reset}`);\n\tlines.push('');\n\n\tif (missingVars.length > 0) {\n\t\tlines.push(`${c.yellow}Missing Variables:${c.reset}`);\n\t\tfor (const v of missingVars) {\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset} ${c.dim}- Required${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\tif (invalidVars.length > 0) {\n\t\tlines.push(`${c.yellow}Invalid Values:${c.reset}`);\n\t\tfor (const v of invalidVars) {\n\t\t\tconst valueStr =\n\t\t\t\tv.value !== undefined ? ` = ${JSON.stringify(v.value)}` : '';\n\t\t\tlines.push(` ${c.cyan}${v.name}${c.reset}${valueStr}`);\n\t\t\tlines.push(` ${c.dim}${v.message}${c.reset}`);\n\t\t}\n\t\tlines.push('');\n\t}\n\n\treturn lines.join('\\n');\n}\n\n/**\n * Cleans up a Zod error message by removing redundant prefixes.\n */\nfunction cleanMessage(message: string): string {\n\t// Remove \"Environment variable \"NAME\": \" prefix if present\n\treturn message.replace(/^Environment variable \"[^\"]+\": /, '');\n}\n\n/**\n * Checks if the current environment is development.\n */\nexport function isDevelopment(): boolean {\n\tconst nodeEnv = process.env.NODE_ENV?.toLowerCase();\n\treturn nodeEnv == null || nodeEnv === 'development' || nodeEnv === 'dev';\n}\n","import get from 'lodash.get';\nimport set from 'lodash.set';\nimport { z } from 'zod/v4';\nimport { formatParseError, isDevelopment } from './formatter.js';\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\tconst zodError = new z.ZodError(errors);\n\t\t\t// In development, log a formatted error message before throwing\n\t\t\tif (isDevelopment()) {\n\t\t\t\tconsole.error(formatParseError(zodError));\n\t\t\t}\n\t\t\tthrow zodError;\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":";;;;;;;;;AAaA,MAAM,SAAS;CACd,OAAO;CACP,KAAK;CACL,QAAQ;CACR,MAAM;CACN,KAAK;CACL,MAAM;AACN;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCD,SAAgB,iBACfA,OACAC,UAAyB,CAAE,GAClB;CACT,MAAM,YACL,QAAQ,WAAW,QAAQ,QAAQ,SAAS,QAAQ,IAAI,YAAY;CAErE,MAAM,IAAI,YACP,SACA;EAAE,OAAO;E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AAED,WAAO;GACP;GAKF,MAAM,eAAe,OAAO;AAC5B,cAAW,iBAAiB,WAC3B,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,aAAa,MAAM,QAAQ,KAAK;AAE/C,QAAI,iBAAiB,WAAW,YAAY,WAAW,OACtD,QAAO,KAAK,WAAW,QAAQ,KAAK;AAErC,WAAO;GACP;AAGF,UAAO;EACP,EACD;CACD;;;;;;;;CASD,AAAQ,eAAe,CAACE,SAAiB;AAExC,OAAK,aAAa,IAAI,KAAK;AAG3B,SAAO,IAAI,MACV,EAAE,GAAGF,SAAG,GACR,EACC,KAAK,CAAC,QAAQ,SAAS;GAGtB,MAAM,QAAQ,OAAO;AAErB,cAAW,UAAU,WAEpB,QAAO,CAAC,GAAG,SAAgB;IAC1B,MAAM,SAAS,MAAM,GAAG,KAAK;AAC7B,WAAO,KAAK,WAAW,QAAQ,KAAK;GACpC;AAIF,OAAI,gBAAgB,UAAU,SAC7B,QAAO,IAAI,MAAM,OAAO,EACvB,KAAK,CAAC,cAAc,eAAe;IAClC,MAAM,cACL,aAAa;AACd,eAAW,gBAAgB,WAC1B,QAAO,CAAC,GAAG,SAAgB;KAC1B,MAAM,SAAS,YAAY,GAAG,KAAK;AACnC,YAAO,KAAK,WAAW,QAAQ,KAAK;IACpC;AAEF,WAAO;GACP,EACD;AAGF,UAAO;EACP,EACD;CAEF;;;;;;;CAQD,OACCI,SACwB;EACxB,MAAM,SAAS,QAAQ,KAAK,aAAa;AACzC,SAAO,IAAI,aAAa,QAAQ,KAAK;CACrC;;;;;;;;;;;;;;CAeD,0BAAoC;AACnC,SAAO,MAAM,KAAK,KAAK,aAAa,CAAC,MAAM;CAC3C;AACD"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { z } from "zod/v4";
|
|
2
|
+
|
|
2
3
|
//#region src/EnvironmentParser.d.ts
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
6
|
* Parses and validates configuration objects against Zod schemas.
|
|
5
7
|
* Handles nested configurations and aggregates validation errors.
|
|
@@ -115,7 +117,7 @@ declare class EnvironmentParser<T extends EmptyObject> {
|
|
|
115
117
|
*
|
|
116
118
|
* @template T - The configuration object type
|
|
117
119
|
*/
|
|
118
|
-
type InferConfig<T extends EmptyObject> = { [K in keyof T]: T[K] extends z.ZodSchema ? z.infer<T[K]> : T[K] extends Record<string, unknown> ? InferConfig<T[K]> : T[K]
|
|
120
|
+
type InferConfig<T extends EmptyObject> = { [K in keyof T]: T[K] extends z.ZodSchema ? z.infer<T[K]> : T[K] extends Record<string, unknown> ? InferConfig<T[K]> : T[K] };
|
|
119
121
|
/**
|
|
120
122
|
* Function type for fetching environment variables with Zod validation.
|
|
121
123
|
* Returns a Zod object scoped to a specific environment variable.
|
|
@@ -125,11 +127,21 @@ type InferConfig<T extends EmptyObject> = { [K in keyof T]: T[K] extends z.ZodSc
|
|
|
125
127
|
* @returns A Zod object for defining the schema
|
|
126
128
|
*/
|
|
127
129
|
type EnvFetcher<TPath extends string = string> = (name: TPath) => typeof z;
|
|
130
|
+
/**
|
|
131
|
+
* Function type for building environment configuration objects.
|
|
132
|
+
* Takes an EnvFetcher and returns a configuration object with Zod schemas.
|
|
133
|
+
*
|
|
134
|
+
* @template TResponse - The response configuration object type
|
|
135
|
+
* @param get - The environment variable fetcher function
|
|
136
|
+
* @returns The configuration object with Zod schemas
|
|
137
|
+
*/
|
|
138
|
+
|
|
128
139
|
/**
|
|
129
140
|
* Type alias for a generic object with unknown values.
|
|
130
141
|
* Used as a constraint for configuration objects.
|
|
131
142
|
*/
|
|
132
143
|
type EmptyObject = Record<string | number | symbol, unknown>;
|
|
144
|
+
//# sourceMappingURL=EnvironmentParser.d.ts.map
|
|
133
145
|
//#endregion
|
|
134
|
-
export {
|
|
135
|
-
//# sourceMappingURL=EnvironmentParser-
|
|
146
|
+
export { ConfigParser, EmptyObject, EnvFetcher, EnvironmentParser, InferConfig };
|
|
147
|
+
//# sourceMappingURL=EnvironmentParser-DoooZm8n.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"EnvironmentParser-DoooZm8n.d.cts","names":[],"sources":["../src/EnvironmentParser.ts"],"sourcesContent":[],"mappings":";;;;;;AAWA;;;;AAS4B,cATf,YASe,CAAA,kBATgB,WAShB,CAAA,CAAA;EAAG,iBAMT,MAAA;EAAS,iBAArB,OAAA;EAAW;AA2FrB;;;;;EAiJmC,WAClB,CAAA,MAAA,EApPU,SAoPV,EAAA,OAAA,CAAA,EAnPW,GAmPX,CAAA,MAAA,CAAA;EAAU;;;AACX;EA6BJ,KAAA,CAAA,CAAA,EA3QF,WA2Qa,CA3QD,SA2QC,CAAA;EAAA;;;;;;;;;;;;;;;;EAKjB,uBAAC,CAAA,CAAA,EAAA,MAAA,EAAA;AAAC;AAWR;;;;AAEa;AAkBb;;;;;;;;;;;;;;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;;;;;;;;;;;;;;KAkBA,WAAA,GAAc"}
|