@clipboard-health/config 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/package.json +2 -1
- package/src/lib/createConfig.d.ts +1 -3
- package/src/lib/createConfig.js +3 -5
- package/src/lib/createConfig.js.map +1 -1
- package/src/lib/internal/resolver.js +4 -4
- package/src/lib/internal/resolver.js.map +1 -1
- package/src/lib/internal/deepFreeze.d.ts +0 -9
- package/src/lib/internal/deepFreeze.js +0 -27
- package/src/lib/internal/deepFreeze.js.map +0 -1
- package/src/lib/internal/isDefined.d.ts +0 -4
- package/src/lib/internal/isDefined.js +0 -10
- package/src/lib/internal/isDefined.js.map +0 -1
package/README.md
CHANGED
|
@@ -25,10 +25,10 @@ The TypeDoc comment for the `createConfig` function:
|
|
|
25
25
|
```ts
|
|
26
26
|
// ./src/lib/createConfig.ts
|
|
27
27
|
|
|
28
|
+
import { deepFreeze } from "@clipboard-health/util-typescript";
|
|
28
29
|
import dotenv from "dotenv";
|
|
29
30
|
import { fromZodError } from "zod-validation-error";
|
|
30
31
|
|
|
31
|
-
import { deepFreeze } from "./internal/deepFreeze";
|
|
32
32
|
import { resolve } from "./internal/resolver";
|
|
33
33
|
import { type ConfigParams } from "./types";
|
|
34
34
|
|
|
@@ -58,9 +58,7 @@ dotenv.config();
|
|
|
58
58
|
* **IMPORTANT**: To avoid runtime errors:
|
|
59
59
|
* 1. Environment variables are strings, so use `z.coerce` Zod types for those you plan to override.
|
|
60
60
|
* Note that `z.coerce.boolean()` coerces any truthy value to `true`. To restrict to `"true" |
|
|
61
|
-
* "false"`, use the
|
|
62
|
-
* {@link https://github.com/ClipboardHealth/core-utils/blob/main/packages/contract-core/src/lib/schemas/booleanString.ts
|
|
63
|
-
* `booleanString` schema} from `@clipboard-health/contract-core`.
|
|
61
|
+
* "false"`, use the `booleanString` schema from `@clipboard-health/contract-core`.
|
|
64
62
|
* 2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to
|
|
65
63
|
* modify it. The actual return type is `ReadonlyDeep<SchemaT>`, but the library returns a
|
|
66
64
|
* `Readonly<SchemaT>` because the former prevents clients from passing configuration values to
|
package/package.json
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clipboard-health/config",
|
|
3
3
|
"description": "Type-safe static configuration management: a pure function to resolve, validate against a Zod schema, and freeze configuration values.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"dependencies": {
|
|
6
|
+
"@clipboard-health/util-typescript": "0.3.0",
|
|
6
7
|
"decamelize": "5.0.1",
|
|
7
8
|
"dotenv": "16.4.5",
|
|
8
9
|
"tslib": "2.8.0",
|
|
@@ -23,9 +23,7 @@ import { type ConfigParams } from "./types";
|
|
|
23
23
|
* **IMPORTANT**: To avoid runtime errors:
|
|
24
24
|
* 1. Environment variables are strings, so use `z.coerce` Zod types for those you plan to override.
|
|
25
25
|
* Note that `z.coerce.boolean()` coerces any truthy value to `true`. To restrict to `"true" |
|
|
26
|
-
* "false"`, use the
|
|
27
|
-
* {@link https://github.com/ClipboardHealth/core-utils/blob/main/packages/contract-core/src/lib/schemas/booleanString.ts
|
|
28
|
-
* `booleanString` schema} from `@clipboard-health/contract-core`.
|
|
26
|
+
* "false"`, use the `booleanString` schema from `@clipboard-health/contract-core`.
|
|
29
27
|
* 2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to
|
|
30
28
|
* modify it. The actual return type is `ReadonlyDeep<SchemaT>`, but the library returns a
|
|
31
29
|
* `Readonly<SchemaT>` because the former prevents clients from passing configuration values to
|
package/src/lib/createConfig.js
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createConfig = createConfig;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const util_typescript_1 = require("@clipboard-health/util-typescript");
|
|
5
6
|
const dotenv_1 = tslib_1.__importDefault(require("dotenv"));
|
|
6
7
|
const zod_validation_error_1 = require("zod-validation-error");
|
|
7
|
-
const deepFreeze_1 = require("./internal/deepFreeze");
|
|
8
8
|
const resolver_1 = require("./internal/resolver");
|
|
9
9
|
dotenv_1.default.config();
|
|
10
10
|
/**
|
|
@@ -31,9 +31,7 @@ dotenv_1.default.config();
|
|
|
31
31
|
* **IMPORTANT**: To avoid runtime errors:
|
|
32
32
|
* 1. Environment variables are strings, so use `z.coerce` Zod types for those you plan to override.
|
|
33
33
|
* Note that `z.coerce.boolean()` coerces any truthy value to `true`. To restrict to `"true" |
|
|
34
|
-
* "false"`, use the
|
|
35
|
-
* {@link https://github.com/ClipboardHealth/core-utils/blob/main/packages/contract-core/src/lib/schemas/booleanString.ts
|
|
36
|
-
* `booleanString` schema} from `@clipboard-health/contract-core`.
|
|
34
|
+
* "false"`, use the `booleanString` schema from `@clipboard-health/contract-core`.
|
|
37
35
|
* 2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to
|
|
38
36
|
* modify it. The actual return type is `ReadonlyDeep<SchemaT>`, but the library returns a
|
|
39
37
|
* `Readonly<SchemaT>` because the former prevents clients from passing configuration values to
|
|
@@ -54,6 +52,6 @@ function createConfig(params) {
|
|
|
54
52
|
cause: result.error,
|
|
55
53
|
});
|
|
56
54
|
}
|
|
57
|
-
return (0,
|
|
55
|
+
return (0, util_typescript_1.deepFreeze)(result.data);
|
|
58
56
|
}
|
|
59
57
|
//# sourceMappingURL=createConfig.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createConfig.js","sourceRoot":"","sources":["../../../../../packages/config/src/lib/createConfig.ts"],"names":[],"mappings":";;
|
|
1
|
+
{"version":3,"file":"createConfig.js","sourceRoot":"","sources":["../../../../../packages/config/src/lib/createConfig.ts"],"names":[],"mappings":";;AA6CA,oCAeC;;AA5DD,uEAA+D;AAC/D,4DAA4B;AAC5B,+DAAoD;AAEpD,kDAA8C;AAG9C,gBAAM,CAAC,MAAM,EAAE,CAAC;AAEhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,SAAgB,YAAY,CAG1B,MAAqD;IACrD,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;IAC/C,MAAM,EAAE,OAAO,EAAE,GAAG,WAAW,CAAC;IAEhC,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,IAAA,kBAAO,EAAC,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC;IAC7F,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,MAAM,IAAI,KAAK,CAAC,oCAAoC,IAAA,mCAAY,EAAC,MAAM,CAAC,KAAK,CAAC,CAAC,QAAQ,EAAE,EAAE,EAAE;YAC3F,KAAK,EAAE,MAAM,CAAC,KAAK;SACpB,CAAC,CAAC;IACL,CAAC;IAED,OAAO,IAAA,4BAAU,EAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC"}
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.resolve = resolve;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const util_typescript_1 = require("@clipboard-health/util-typescript");
|
|
5
6
|
const decamelize_1 = tslib_1.__importDefault(require("decamelize"));
|
|
6
7
|
const zod_1 = require("zod");
|
|
7
|
-
const isDefined_1 = require("./isDefined");
|
|
8
8
|
function resolve(params) {
|
|
9
9
|
const { config, path, ...rest } = params;
|
|
10
10
|
return Object.fromEntries(Object.entries(config).map(([key, value]) => [
|
|
@@ -16,18 +16,18 @@ function resolve(params) {
|
|
|
16
16
|
}
|
|
17
17
|
function isConfigValue(value) {
|
|
18
18
|
return (typeof value === "object" &&
|
|
19
|
-
(0,
|
|
19
|
+
(0, util_typescript_1.isDefined)(value) &&
|
|
20
20
|
"description" in value &&
|
|
21
21
|
"defaultValue" in value);
|
|
22
22
|
}
|
|
23
23
|
function resolveConfigValue(params) {
|
|
24
24
|
const { environment, path, schema, value } = params;
|
|
25
25
|
const variable = process.env[(0, decamelize_1.default)(path.join("_")).toUpperCase()];
|
|
26
|
-
if ((0,
|
|
26
|
+
if ((0, util_typescript_1.isDefined)(variable)) {
|
|
27
27
|
return parseEnvironmentVariable(variable, getSchema(path, schema));
|
|
28
28
|
}
|
|
29
29
|
const override = value.overrides?.[environment];
|
|
30
|
-
if ((0,
|
|
30
|
+
if ((0, util_typescript_1.isDefined)(override)) {
|
|
31
31
|
return override;
|
|
32
32
|
}
|
|
33
33
|
return value.defaultValue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../../../../../../packages/config/src/lib/internal/resolver.ts"],"names":[],"mappings":";;AAiBA,0BAeC;;AAhCD,oEAAoC;AACpC,6BAAwB;
|
|
1
|
+
{"version":3,"file":"resolver.js","sourceRoot":"","sources":["../../../../../../packages/config/src/lib/internal/resolver.ts"],"names":[],"mappings":";;AAiBA,0BAeC;;AAhCD,uEAA8D;AAC9D,oEAAoC;AACpC,6BAAwB;AAexB,SAAgB,OAAO,CACrB,MAAwC;IAExC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,GAAG,MAAM,CAAC;IAEzC,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,CACxB,CAAC,CAAC,GAAG,EAAE,KAAK,CAAuD,EAAE,EAAE,CAAC;QACtE,GAAG;QACH,aAAa,CAAC,KAAK,CAAC;YAClB,CAAC,CAAC,kBAAkB,CAAC,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,CAAC;YAC9D,CAAC,CAAC,OAAO,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H,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,SAAS,CAAC,IAAuB,EAAE,MAA0B;IACpE,OAAO,IAAI,CAAC,MAAM,CAChB,CAAC,MAAM,EAAE,GAAG,EAAE,EAAE;IACd,+DAA+D;IAC/D,MAAM,YAAY,OAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,0BAA0B,CAAC,MAAM,EACvF,MAAM,CACP,CAAC;AACJ,CAAC"}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Recursively freezes an object and all its nested properties.
|
|
3
|
-
*
|
|
4
|
-
* @template T - Type of the object to freeze
|
|
5
|
-
* @param value - The object to freeze
|
|
6
|
-
* @param seen - Internal parameter to track circular references
|
|
7
|
-
* @returns A deeply frozen version of the input object.
|
|
8
|
-
*/
|
|
9
|
-
export declare function deepFreeze<T extends object>(value: T, seen?: WeakSet<WeakKey>): Readonly<T>;
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.deepFreeze = deepFreeze;
|
|
4
|
-
/**
|
|
5
|
-
* Recursively freezes an object and all its nested properties.
|
|
6
|
-
*
|
|
7
|
-
* @template T - Type of the object to freeze
|
|
8
|
-
* @param value - The object to freeze
|
|
9
|
-
* @param seen - Internal parameter to track circular references
|
|
10
|
-
* @returns A deeply frozen version of the input object.
|
|
11
|
-
*/
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
13
|
-
function deepFreeze(value, seen = new WeakSet()) {
|
|
14
|
-
if (!value || typeof value !== "object" || seen.has(value)) {
|
|
15
|
-
return value;
|
|
16
|
-
}
|
|
17
|
-
seen.add(value);
|
|
18
|
-
Reflect.ownKeys(value).forEach((key) => {
|
|
19
|
-
const property = value[key];
|
|
20
|
-
if (property && typeof property === "object" && !Object.isFrozen(property)) {
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
22
|
-
deepFreeze(property, seen);
|
|
23
|
-
}
|
|
24
|
-
});
|
|
25
|
-
return Object.freeze(value);
|
|
26
|
-
}
|
|
27
|
-
//# sourceMappingURL=deepFreeze.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"deepFreeze.js","sourceRoot":"","sources":["../../../../../../packages/config/src/lib/internal/deepFreeze.ts"],"names":[],"mappings":";;AASA,gCAeC;AAxBD;;;;;;;GAOG;AACH,wDAAwD;AACxD,SAAgB,UAAU,CAAmB,KAAQ,EAAE,IAAI,GAAG,IAAI,OAAO,EAAE;IACzE,IAAI,CAAC,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3D,OAAO,KAAK,CAAC;IACf,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACf,OAAO,CAAC,OAAO,CAAC,KAAK,CAAoB,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;QACzD,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,QAAQ,IAAI,OAAO,QAAQ,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3E,wDAAwD;YACxD,UAAU,CAA2B,QAAQ,EAAE,IAAI,CAAC,CAAC;QACvD,CAAC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAC9B,CAAC"}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isDefined = isDefined;
|
|
4
|
-
/**
|
|
5
|
-
* Type guard that checks if a value is neither null nor undefined.
|
|
6
|
-
*/
|
|
7
|
-
function isDefined(value) {
|
|
8
|
-
return value !== null && value !== undefined;
|
|
9
|
-
}
|
|
10
|
-
//# sourceMappingURL=isDefined.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"isDefined.js","sourceRoot":"","sources":["../../../../../../packages/config/src/lib/internal/isDefined.ts"],"names":[],"mappings":";;AAGA,8BAEC;AALD;;GAEG;AACH,SAAgB,SAAS,CAAI,KAAQ;IACnC,OAAO,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,CAAC;AAC/C,CAAC"}
|