@nirelc/microconf 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/dist/sources/env.d.mts +5 -1
- package/dist/sources/env.mjs +9 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -87,13 +87,20 @@ env({
|
|
|
87
87
|
prefix: "APP_", // optional, default: undefined
|
|
88
88
|
delimiter: "__", // optional, default: "__"
|
|
89
89
|
forceCoerce: true, // optional, default: true
|
|
90
|
+
renames: {
|
|
91
|
+
APP_NESTED__KEY: "RANDOM_KEY", // optional
|
|
92
|
+
},
|
|
90
93
|
});
|
|
91
94
|
```
|
|
92
95
|
|
|
93
96
|
`prefix` - prefix for env variable names. e.g., with prefix `APP_`, `token` becomes `APP_TOKEN`
|
|
97
|
+
|
|
94
98
|
`delimiter` - delimiter for nested keys. default: `__`
|
|
99
|
+
|
|
95
100
|
`forceCoerce` - boolean to force type coercion. default: `true`. e.g. if set to `false`, `t.boolean()` willn't coerce `"true"` to `true` and will return a parse error instead
|
|
96
101
|
|
|
102
|
+
`renames` - optional map of env variable names to schema paths. e.g., with `renames: { APP_NESTED__KEY: "RANDOM_KEY" }`, the env variable `RANDOM_KEY` instead of `APP_NESTED__KEY`
|
|
103
|
+
|
|
97
104
|
## Errors
|
|
98
105
|
|
|
99
106
|
With `APP_PORT=abc`, `prefix: "APP_"` and a numeric `port` schema:
|
package/dist/sources/env.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { SchemaMeta, Source } from "../types.mjs";
|
|
2
2
|
import { ParseResult } from "@nirelc/microtype";
|
|
3
3
|
//#region src/sources/env.d.ts
|
|
4
|
+
export type RenameMap = Record<string, string>;
|
|
4
5
|
export type EnvSourceOptions = {
|
|
5
6
|
prefix?: string;
|
|
6
7
|
delimiter?: string;
|
|
@@ -9,14 +10,17 @@ export type EnvSourceOptions = {
|
|
|
9
10
|
* useful, because env variables are always strings, and we want to coerce them to the correct type
|
|
10
11
|
*/
|
|
11
12
|
forceCoerce?: boolean;
|
|
13
|
+
renames?: RenameMap;
|
|
12
14
|
};
|
|
13
15
|
export declare class EnvSource implements Source {
|
|
14
16
|
prefix: string;
|
|
15
17
|
delimiter: string;
|
|
16
18
|
forceCoerce: boolean;
|
|
19
|
+
renames: RenameMap;
|
|
17
20
|
name: string;
|
|
18
|
-
constructor({ prefix, delimiter, forceCoerce }?: EnvSourceOptions);
|
|
21
|
+
constructor({ prefix, delimiter, forceCoerce, renames }?: EnvSourceOptions);
|
|
19
22
|
getKeyByPath(path: PropertyKey[]): string;
|
|
23
|
+
getKeyWithRenames(path: PropertyKey[]): string;
|
|
20
24
|
getValueByPath(path: PropertyKey[]): string | undefined;
|
|
21
25
|
load(meta: SchemaMeta): ParseResult<unknown> | undefined;
|
|
22
26
|
}
|
package/dist/sources/env.mjs
CHANGED
|
@@ -10,11 +10,13 @@ var EnvSource = class {
|
|
|
10
10
|
prefix;
|
|
11
11
|
delimiter;
|
|
12
12
|
forceCoerce;
|
|
13
|
+
renames;
|
|
13
14
|
name = "env";
|
|
14
|
-
constructor({ prefix = "", delimiter = "__", forceCoerce = true } = {}) {
|
|
15
|
+
constructor({ prefix = "", delimiter = "__", forceCoerce = true, renames = {} } = {}) {
|
|
15
16
|
this.prefix = prefix.trim();
|
|
16
17
|
this.delimiter = delimiter;
|
|
17
18
|
this.forceCoerce = forceCoerce;
|
|
19
|
+
this.renames = renames;
|
|
18
20
|
}
|
|
19
21
|
getKeyByPath(path) {
|
|
20
22
|
return `${this.prefix}${path.map((p) => {
|
|
@@ -22,15 +24,19 @@ var EnvSource = class {
|
|
|
22
24
|
return p;
|
|
23
25
|
}).join(this.delimiter)}`.toUpperCase();
|
|
24
26
|
}
|
|
27
|
+
getKeyWithRenames(path) {
|
|
28
|
+
const key = this.getKeyByPath(path);
|
|
29
|
+
return this.renames[key] ?? key;
|
|
30
|
+
}
|
|
25
31
|
getValueByPath(path) {
|
|
26
|
-
return process.env[this.
|
|
32
|
+
return process.env[this.getKeyWithRenames(path)];
|
|
27
33
|
}
|
|
28
34
|
load(meta) {
|
|
29
35
|
const value = this.getValueByPath(meta.path);
|
|
30
36
|
if (value === void 0) return;
|
|
31
37
|
const result = (this.forceCoerce ? enableCoercion(meta.schema) : meta.schema)._parse(value);
|
|
32
38
|
if (result.success) return result;
|
|
33
|
-
const key = this.
|
|
39
|
+
const key = this.getKeyWithRenames(meta.path);
|
|
34
40
|
return {
|
|
35
41
|
success: false,
|
|
36
42
|
issues: result.issues.map((issue) => ({
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "@nirelc/microconf",
|
|
3
3
|
"description": "Lightweight and simple type-safety config library over microtype",
|
|
4
4
|
"author": "Toil",
|
|
5
|
-
"version": "0.0.
|
|
5
|
+
"version": "0.0.3",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"type": "module",
|
|
8
8
|
"homepage": "https://github.com/nirelc/microconf#readme",
|
|
@@ -66,6 +66,6 @@
|
|
|
66
66
|
],
|
|
67
67
|
"sideEffects": false,
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@nirelc/microtype": "^0.0.
|
|
69
|
+
"@nirelc/microtype": "^0.0.3"
|
|
70
70
|
}
|
|
71
71
|
}
|