@rhombus-std/config.env 0.0.0-alpha.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/LICENSE +21 -0
- package/README.md +44 -0
- package/dist/index.d.ts +61 -0
- package/dist/index.js +61 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thomas Butler
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# @rhombus-std/config.env
|
|
2
|
+
|
|
3
|
+
Environment-variable configuration provider for `@rhombus-std/config` —
|
|
4
|
+
`EnvironmentVariablesConfigurationSource`/`EnvironmentVariablesConfigurationProvider`
|
|
5
|
+
plus the `addEnvironmentVariables` sugar bolted onto `ConfigurationBuilder`.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install @rhombus-std/config @rhombus-std/config.env
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
`@rhombus-std/config` is a peer dependency — install it alongside this package.
|
|
14
|
+
|
|
15
|
+
## Basic usage
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import "@rhombus-std/config.env"; // unlocks .addEnvironmentVariables() on ConfigurationBuilder
|
|
19
|
+
import { ConfigurationBuilder } from "@rhombus-std/config";
|
|
20
|
+
|
|
21
|
+
const config = new ConfigurationBuilder()
|
|
22
|
+
.addEnvironmentVariables({ prefix: "APP_" })
|
|
23
|
+
.build();
|
|
24
|
+
|
|
25
|
+
// APP_SERVER__PORT=8080 in the environment resolves as:
|
|
26
|
+
config.get("Server:Port"); // "8080"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Variable names are normalized (`__` → `:`) before prefix matching, and the
|
|
30
|
+
prefix match itself is case-insensitive — `app_`, `APP_`, and `App_` all match
|
|
31
|
+
a `prefix: "APP_"` source.
|
|
32
|
+
|
|
33
|
+
## The side-effect import requirement
|
|
34
|
+
|
|
35
|
+
`addEnvironmentVariables` isn't a method `ConfigurationBuilder` ships with —
|
|
36
|
+
this package bolts it on via TypeScript declaration merging plus a runtime
|
|
37
|
+
prototype patch. If your code calls `.addEnvironmentVariables()` but never
|
|
38
|
+
names any other symbol from `@rhombus-std/config.env`, a bundler or tree-shaker has
|
|
39
|
+
nothing forcing it to load this package's module — you must import it for
|
|
40
|
+
its side effect explicitly:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
import "@rhombus-std/config.env"; // unlocks .addEnvironmentVariables() on ConfigurationBuilder
|
|
44
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { IConfigurationSource, IConfigurationBuilder, IConfigurationProvider, IndexedSection } from '@rhombus-std/config.core';
|
|
2
|
+
import { ConfigurationProvider } from '@rhombus-std/config';
|
|
3
|
+
|
|
4
|
+
/** Options accepted by {@link EnvironmentVariablesConfigurationSource}. */
|
|
5
|
+
interface EnvironmentVariablesConfigurationSourceOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Only variables whose TRANSFORMED name starts with `prefix` (case-insensitive)
|
|
8
|
+
* are kept; the prefix is stripped from the resulting key.
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Transforms a raw environment variable name before prefix matching.
|
|
13
|
+
* Defaults to replacing every `__` with `:`, the conventional way to spell
|
|
14
|
+
* a section-delimited key in an environment variable name.
|
|
15
|
+
*/
|
|
16
|
+
variableNameTransformation?: (name: string) => string;
|
|
17
|
+
/**
|
|
18
|
+
* The environment map to read. Defaults to `process.env`. Injectable so
|
|
19
|
+
* `load()` is pure with respect to an explicit map -- tests (and any caller
|
|
20
|
+
* wanting a hermetic source) pass their own instead of mutating the ambient
|
|
21
|
+
* `process.env`.
|
|
22
|
+
*/
|
|
23
|
+
env?: Record<string, string | undefined>;
|
|
24
|
+
}
|
|
25
|
+
/** Default {@link EnvironmentVariablesConfigurationSourceOptions.variableNameTransformation}: `__` -> `:`. */
|
|
26
|
+
declare function defaultVariableNameTransformation(name: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* A configuration source backed by `process.env`, flattened into the
|
|
29
|
+
* colon-delimited key/value store every provider produces, per an optional
|
|
30
|
+
* name prefix and a variable-name transformation.
|
|
31
|
+
*/
|
|
32
|
+
declare class EnvironmentVariablesConfigurationSource implements IConfigurationSource {
|
|
33
|
+
/** Only variables whose transformed name starts with this prefix (case-insensitive) are kept. */
|
|
34
|
+
prefix?: string;
|
|
35
|
+
/** Applied to each raw variable name before prefix matching. */
|
|
36
|
+
variableNameTransformation: (name: string) => string;
|
|
37
|
+
/** The environment map read at load time (defaults to `process.env`). */
|
|
38
|
+
env: Record<string, string | undefined>;
|
|
39
|
+
constructor(options?: EnvironmentVariablesConfigurationSourceOptions);
|
|
40
|
+
build(_builder: IConfigurationBuilder): IConfigurationProvider;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
declare class EnvironmentVariablesConfigurationProvider extends ConfigurationProvider {
|
|
44
|
+
#private;
|
|
45
|
+
constructor(source: EnvironmentVariablesConfigurationSource);
|
|
46
|
+
load(): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare module "@rhombus-std/config/configuration-builder" {
|
|
50
|
+
interface ConfigurationBuilder<T = IndexedSection> {
|
|
51
|
+
/**
|
|
52
|
+
* Registers an {@link EnvironmentVariablesConfigurationSource} seeded from
|
|
53
|
+
* `process.env`, per an optional `options.prefix` and
|
|
54
|
+
* `options.variableNameTransformation`.
|
|
55
|
+
*/
|
|
56
|
+
addEnvironmentVariables(options?: EnvironmentVariablesConfigurationSourceOptions): this;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { EnvironmentVariablesConfigurationProvider, EnvironmentVariablesConfigurationSource, defaultVariableNameTransformation };
|
|
61
|
+
export type { EnvironmentVariablesConfigurationSourceOptions };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { ConfigurationBuilder } from "@rhombus-std/config";
|
|
3
|
+
|
|
4
|
+
// src/environment-variables-configuration-provider.ts
|
|
5
|
+
import { ConfigurationProvider } from "@rhombus-std/config";
|
|
6
|
+
|
|
7
|
+
class EnvironmentVariablesConfigurationProvider extends ConfigurationProvider {
|
|
8
|
+
#source;
|
|
9
|
+
constructor(source) {
|
|
10
|
+
super();
|
|
11
|
+
this.#source = source;
|
|
12
|
+
}
|
|
13
|
+
load() {
|
|
14
|
+
this.data.clear();
|
|
15
|
+
const { prefix, variableNameTransformation, env } = this.#source;
|
|
16
|
+
for (const [rawName, value] of Object.entries(env)) {
|
|
17
|
+
if (value === undefined) {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const transformedName = variableNameTransformation(rawName);
|
|
21
|
+
if (prefix === undefined) {
|
|
22
|
+
this.set(transformedName, value);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (!transformedName.toLowerCase().startsWith(prefix.toLowerCase())) {
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
this.set(transformedName.slice(prefix.length), value);
|
|
29
|
+
}
|
|
30
|
+
this.onReload();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// src/environment-variables-configuration-source.ts
|
|
35
|
+
function defaultVariableNameTransformation(name) {
|
|
36
|
+
return name.replaceAll("__", ":");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class EnvironmentVariablesConfigurationSource {
|
|
40
|
+
prefix;
|
|
41
|
+
variableNameTransformation;
|
|
42
|
+
env;
|
|
43
|
+
constructor(options) {
|
|
44
|
+
this.prefix = options?.prefix;
|
|
45
|
+
this.variableNameTransformation = options?.variableNameTransformation ?? defaultVariableNameTransformation;
|
|
46
|
+
this.env = options?.env ?? process.env;
|
|
47
|
+
}
|
|
48
|
+
build(_builder) {
|
|
49
|
+
return new EnvironmentVariablesConfigurationProvider(this);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/index.ts
|
|
54
|
+
ConfigurationBuilder.prototype.addEnvironmentVariables = function(options) {
|
|
55
|
+
return this.add(new EnvironmentVariablesConfigurationSource(options));
|
|
56
|
+
};
|
|
57
|
+
export {
|
|
58
|
+
defaultVariableNameTransformation,
|
|
59
|
+
EnvironmentVariablesConfigurationSource,
|
|
60
|
+
EnvironmentVariablesConfigurationProvider
|
|
61
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rhombus-std/config.env",
|
|
3
|
+
"version": "0.0.0-alpha.0",
|
|
4
|
+
"description": "process.env configuration provider for @rhombus-std/config: EnvironmentVariablesConfigurationSource/Provider + the addEnvironmentVariables augmentation.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"configuration",
|
|
7
|
+
"config",
|
|
8
|
+
"env",
|
|
9
|
+
"environment-variables",
|
|
10
|
+
"options",
|
|
11
|
+
"ioc",
|
|
12
|
+
"typescript"
|
|
13
|
+
],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/fnioc/std.git",
|
|
18
|
+
"directory": "libraries/config.env"
|
|
19
|
+
},
|
|
20
|
+
"type": "module",
|
|
21
|
+
"main": "./dist/index.js",
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"sideEffects": true,
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@rhombus-std/config.core": "0.0.0-alpha.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@rhombus-std/config": "^0.0.0-alpha.0"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rhombus-std/config": "0.0.0-alpha.0"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public",
|
|
45
|
+
"provenance": true
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "bun x tsc --noEmit -p tsconfig.json && bun build.ts",
|
|
49
|
+
"lint": "bun x tsc --noEmit -p tsconfig.lint.json"
|
|
50
|
+
}
|
|
51
|
+
}
|