@clipboard-health/config 0.9.0 → 0.10.1
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 +38 -7
- package/package.json +2 -2
- package/src/lib/createConfig.d.ts +13 -4
- package/src/lib/createConfig.js +13 -4
- package/src/lib/createConfig.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
# @clipboard-health/config <!-- omit from toc -->
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<embedex source="packages/config/examples/config.md">
|
|
4
|
+
|
|
5
|
+
Type-safe static configuration management: a pure function to resolve, validate against a Zod
|
|
6
|
+
schema, and freeze configuration values.
|
|
7
|
+
|
|
8
|
+
Configuration values resolve in order from highest precedence to lowest:
|
|
9
|
+
|
|
10
|
+
1. Environment variables
|
|
11
|
+
- Resolved converting configuration path from camelCase to UPPER_SNAKE. For example, the `{
|
|
12
|
+
myApi: { port: 3000 } }` configuration resolves to `MY_API_PORT`.
|
|
13
|
+
2. Environment-specific overrides, {@link ConfigValue.overrides}
|
|
14
|
+
3. Default values, {@link ConfigValue.defaultValue}
|
|
15
|
+
|
|
16
|
+
Supported configuration value types:
|
|
17
|
+
|
|
18
|
+
- bigint
|
|
19
|
+
- boolean
|
|
20
|
+
- date
|
|
21
|
+
- number
|
|
22
|
+
- string
|
|
23
|
+
- arrays and nested objects using the above types
|
|
24
|
+
|
|
25
|
+
To override arrays with environment variables, use stringified JSON arrays, e.g. `["a","b"]`.
|
|
26
|
+
|
|
27
|
+
**IMPORTANT**: To avoid runtime errors:
|
|
28
|
+
|
|
29
|
+
1. Environment variables are strings, so use `z.coerce` Zod types for those you plan to override.
|
|
30
|
+
Note that `z.coerce.boolean()` coerces any truthy value to `true`. To restrict to `"true" |
|
|
31
|
+
"false"`, use the `booleanString` schema from `@clipboard-health/contract-core`.
|
|
32
|
+
2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to
|
|
33
|
+
modify it. The actual return type is `ReadonlyDeep<SchemaT>`, but the library returns a
|
|
34
|
+
`Readonly<SchemaT>` because the former prevents clients from passing configuration values to
|
|
35
|
+
functions that don't explicitly accept `readonly` types.
|
|
36
|
+
|
|
37
|
+
</embedex>
|
|
4
38
|
|
|
5
39
|
## Table of contents <!-- omit from toc -->
|
|
6
40
|
|
|
@@ -19,13 +53,9 @@ npm install @clipboard-health/config
|
|
|
19
53
|
|
|
20
54
|
### Type-safe configuration
|
|
21
55
|
|
|
22
|
-
|
|
56
|
+
<embedex source="packages/config/examples/config.ts">
|
|
23
57
|
|
|
24
|
-
A usage example:
|
|
25
|
-
|
|
26
|
-
<!-- prettier-ignore -->
|
|
27
58
|
```ts
|
|
28
|
-
// packages/config/examples/config.ts
|
|
29
59
|
import { ok } from "node:assert/strict";
|
|
30
60
|
|
|
31
61
|
import { createConfig } from "@clipboard-health/config";
|
|
@@ -89,9 +119,10 @@ try {
|
|
|
89
119
|
} finally {
|
|
90
120
|
process.env = { ...original };
|
|
91
121
|
}
|
|
92
|
-
|
|
93
122
|
```
|
|
94
123
|
|
|
124
|
+
</embedex>
|
|
125
|
+
|
|
95
126
|
## Local development commands
|
|
96
127
|
|
|
97
128
|
See [`package.json`](./package.json) `scripts` for a list of commands.
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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.10.1",
|
|
5
5
|
"bugs": "https://github.com/ClipboardHealth/core-utils/issues",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@clipboard-health/util-ts": "2.
|
|
7
|
+
"@clipboard-health/util-ts": "2.14.1",
|
|
8
8
|
"decamelize": "5.0.1",
|
|
9
9
|
"dotenv": "16.4.5",
|
|
10
10
|
"tslib": "2.8.0",
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { type ConfigParams } from "./types";
|
|
2
2
|
/**
|
|
3
|
+
* <embedex source="packages/config/examples/config.md">
|
|
4
|
+
*
|
|
3
5
|
* Type-safe static configuration management: a pure function to resolve, validate against a Zod
|
|
4
6
|
* schema, and freeze configuration values.
|
|
5
7
|
*
|
|
6
8
|
* Configuration values resolve in order from highest precedence to lowest:
|
|
9
|
+
*
|
|
7
10
|
* 1. Environment variables
|
|
8
11
|
* - Resolved converting configuration path from camelCase to UPPER_SNAKE. For example, the `{
|
|
9
|
-
*
|
|
12
|
+
* myApi: { port: 3000 } }` configuration resolves to `MY_API_PORT`.
|
|
10
13
|
* 2. Environment-specific overrides, {@link ConfigValue.overrides}
|
|
11
14
|
* 3. Default values, {@link ConfigValue.defaultValue}
|
|
12
15
|
*
|
|
13
16
|
* Supported configuration value types:
|
|
17
|
+
*
|
|
14
18
|
* - bigint
|
|
15
19
|
* - boolean
|
|
16
20
|
* - date
|
|
@@ -21,17 +25,21 @@ import { type ConfigParams } from "./types";
|
|
|
21
25
|
* To override arrays with environment variables, use stringified JSON arrays, e.g. `["a","b"]`.
|
|
22
26
|
*
|
|
23
27
|
* **IMPORTANT**: To avoid runtime errors:
|
|
28
|
+
*
|
|
24
29
|
* 1. Environment variables are strings, so use `z.coerce` Zod types for those you plan to override.
|
|
25
30
|
* Note that `z.coerce.boolean()` coerces any truthy value to `true`. To restrict to `"true" |
|
|
26
|
-
*
|
|
31
|
+
* "false"`, use the `booleanString` schema from `@clipboard-health/contract-core`.
|
|
27
32
|
* 2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to
|
|
28
33
|
* modify it. The actual return type is `ReadonlyDeep<SchemaT>`, but the library returns a
|
|
29
34
|
* `Readonly<SchemaT>` because the former prevents clients from passing configuration values to
|
|
30
35
|
* functions that don't explicitly accept `readonly` types.
|
|
31
36
|
*
|
|
37
|
+
* </embedex>
|
|
38
|
+
*
|
|
32
39
|
* @example
|
|
40
|
+
* <embedex source="packages/config/examples/config.ts">
|
|
41
|
+
*
|
|
33
42
|
* ```ts
|
|
34
|
-
* // packages/config/examples/config.ts
|
|
35
43
|
* import { ok } from "node:assert/strict";
|
|
36
44
|
*
|
|
37
45
|
* import { createConfig } from "@clipboard-health/config";
|
|
@@ -95,9 +103,10 @@ import { type ConfigParams } from "./types";
|
|
|
95
103
|
* } finally {
|
|
96
104
|
* process.env = { ...original };
|
|
97
105
|
* }
|
|
98
|
-
*
|
|
99
106
|
* ```
|
|
100
107
|
*
|
|
108
|
+
* </embedex>
|
|
109
|
+
*
|
|
101
110
|
* @throws {Error} When configuration values fail schema validation
|
|
102
111
|
* @returns A deeply frozen configuration object matching the provided schema
|
|
103
112
|
*/
|
package/src/lib/createConfig.js
CHANGED
|
@@ -8,17 +8,21 @@ const zod_validation_error_1 = require("zod-validation-error");
|
|
|
8
8
|
const resolver_1 = require("./internal/resolver");
|
|
9
9
|
dotenv_1.default.config();
|
|
10
10
|
/**
|
|
11
|
+
* <embedex source="packages/config/examples/config.md">
|
|
12
|
+
*
|
|
11
13
|
* Type-safe static configuration management: a pure function to resolve, validate against a Zod
|
|
12
14
|
* schema, and freeze configuration values.
|
|
13
15
|
*
|
|
14
16
|
* Configuration values resolve in order from highest precedence to lowest:
|
|
17
|
+
*
|
|
15
18
|
* 1. Environment variables
|
|
16
19
|
* - Resolved converting configuration path from camelCase to UPPER_SNAKE. For example, the `{
|
|
17
|
-
*
|
|
20
|
+
* myApi: { port: 3000 } }` configuration resolves to `MY_API_PORT`.
|
|
18
21
|
* 2. Environment-specific overrides, {@link ConfigValue.overrides}
|
|
19
22
|
* 3. Default values, {@link ConfigValue.defaultValue}
|
|
20
23
|
*
|
|
21
24
|
* Supported configuration value types:
|
|
25
|
+
*
|
|
22
26
|
* - bigint
|
|
23
27
|
* - boolean
|
|
24
28
|
* - date
|
|
@@ -29,17 +33,21 @@ dotenv_1.default.config();
|
|
|
29
33
|
* To override arrays with environment variables, use stringified JSON arrays, e.g. `["a","b"]`.
|
|
30
34
|
*
|
|
31
35
|
* **IMPORTANT**: To avoid runtime errors:
|
|
36
|
+
*
|
|
32
37
|
* 1. Environment variables are strings, so use `z.coerce` Zod types for those you plan to override.
|
|
33
38
|
* Note that `z.coerce.boolean()` coerces any truthy value to `true`. To restrict to `"true" |
|
|
34
|
-
*
|
|
39
|
+
* "false"`, use the `booleanString` schema from `@clipboard-health/contract-core`.
|
|
35
40
|
* 2. The resulting configuration is deeply frozen and will throw a runtime error if you attempt to
|
|
36
41
|
* modify it. The actual return type is `ReadonlyDeep<SchemaT>`, but the library returns a
|
|
37
42
|
* `Readonly<SchemaT>` because the former prevents clients from passing configuration values to
|
|
38
43
|
* functions that don't explicitly accept `readonly` types.
|
|
39
44
|
*
|
|
45
|
+
* </embedex>
|
|
46
|
+
*
|
|
40
47
|
* @example
|
|
48
|
+
* <embedex source="packages/config/examples/config.ts">
|
|
49
|
+
*
|
|
41
50
|
* ```ts
|
|
42
|
-
* // packages/config/examples/config.ts
|
|
43
51
|
* import { ok } from "node:assert/strict";
|
|
44
52
|
*
|
|
45
53
|
* import { createConfig } from "@clipboard-health/config";
|
|
@@ -103,9 +111,10 @@ dotenv_1.default.config();
|
|
|
103
111
|
* } finally {
|
|
104
112
|
* process.env = { ...original };
|
|
105
113
|
* }
|
|
106
|
-
*
|
|
107
114
|
* ```
|
|
108
115
|
*
|
|
116
|
+
* </embedex>
|
|
117
|
+
*
|
|
109
118
|
* @throws {Error} When configuration values fail schema validation
|
|
110
119
|
* @returns A deeply frozen configuration object matching the provided schema
|
|
111
120
|
*/
|
|
@@ -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":";;AAwHA,oCAeC;;AAvID,uDAAuD;AACvD,4DAA4B;AAC5B,+DAAoD;AAEpD,kDAA8C;AAG9C,gBAAM,CAAC,MAAM,EAAE,CAAC;AAEhB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8GG;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,oBAAU,EAAC,MAAM,CAAC,IAAI,CAAC,CAAC;AACjC,CAAC"}
|