@jsnw/srv-utils 1.0.10 → 1.1.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
CHANGED
|
@@ -32,16 +32,27 @@ import {getRootPackageDirnameSync} from '@jsnw/srv-utils';
|
|
|
32
32
|
const root = getRootPackageDirnameSync();
|
|
33
33
|
```
|
|
34
34
|
|
|
35
|
-
### ConfigLoader.
|
|
36
|
-
Loads a YAML file and validates it with a Zod schema. It also injects `isDev` based on `APP_CONTEXT`, `APPLICATION_CONTEXT`, or `NODE_ENV`.
|
|
35
|
+
### ConfigLoader.loadConfig(path, schema, addProps?)
|
|
36
|
+
Loads a YAML file and validates it with a Zod schema. It also injects `isDev` based on `APP_CONTEXT`, `APPLICATION_CONTEXT`, or `NODE_ENV`.
|
|
37
|
+
|
|
38
|
+
**Path resolution prefixes:**
|
|
39
|
+
- `%pkgroot/` - Resolves to the project root directory
|
|
40
|
+
- `%env:VAR_NAME/` - Resolves based on environment variable value
|
|
37
41
|
|
|
38
42
|
```ts
|
|
39
43
|
import {ConfigLoader, configSchemas} from '@jsnw/srv-utils';
|
|
40
44
|
|
|
41
|
-
|
|
45
|
+
// Using project root
|
|
46
|
+
const config = ConfigLoader.loadConfig(
|
|
42
47
|
'%pkgroot/config.yml',
|
|
43
48
|
configSchemas.mongodbConnection
|
|
44
49
|
);
|
|
50
|
+
|
|
51
|
+
// Using environment variable
|
|
52
|
+
const config = ConfigLoader.loadConfig(
|
|
53
|
+
'%env:CONFIG_PATH/config.yml',
|
|
54
|
+
configSchemas.mongodbConnection
|
|
55
|
+
);
|
|
45
56
|
```
|
|
46
57
|
|
|
47
58
|
### configSchemas
|
|
@@ -8,6 +8,7 @@ const yaml_1 = require("yaml");
|
|
|
8
8
|
const file_exists_1 = require("../file-exists");
|
|
9
9
|
const getRootPackageDirname_1 = require("../getRootPackageDirname");
|
|
10
10
|
const PKG_ROOT_REGEX = /^%pkgroot(?:[\/\\]|$)/i;
|
|
11
|
+
const ENV_VAR_REGEX = /^%env:(?<var_name>[A-Za-z0-9_]+)(?:[\/\\]|$)/i;
|
|
11
12
|
class ConfigLoader {
|
|
12
13
|
static _instance;
|
|
13
14
|
/**
|
|
@@ -22,7 +23,9 @@ class ConfigLoader {
|
|
|
22
23
|
* @template {z.ZodObject} S
|
|
23
24
|
* @template {object} P
|
|
24
25
|
* @param {string} path You can use %pkgroot prefix for automatic project root resolution by AppConfigLoader.
|
|
25
|
-
*
|
|
26
|
+
* Also, you can use %env:ENV_VARIABLE to resolve config path based on environment variable
|
|
27
|
+
* Example 1: %pkgroot/config.yml
|
|
28
|
+
* Example 2: %env:CONFIG_PATH/config.yml
|
|
26
29
|
* @param {S} schema
|
|
27
30
|
* @param {P} [addProps]
|
|
28
31
|
* @returns {ResolvedConfig<S, P>}
|
|
@@ -62,7 +65,7 @@ class ConfigLoader {
|
|
|
62
65
|
* @protected
|
|
63
66
|
*/
|
|
64
67
|
loadYamlFile(path) {
|
|
65
|
-
path = this.
|
|
68
|
+
path = this.resolveFilePath(path);
|
|
66
69
|
if (!(0, file_exists_1.fileExistsSync)(path))
|
|
67
70
|
return [null, new Error(`YAML file does not exists at path: ${path}`)];
|
|
68
71
|
let data = undefined, parsedYml = undefined;
|
|
@@ -91,7 +94,7 @@ class ConfigLoader {
|
|
|
91
94
|
const { data: parsed, error, success } = schema.safeParse(data);
|
|
92
95
|
if (!success) {
|
|
93
96
|
if (error && error instanceof zod_1.ZodError)
|
|
94
|
-
return [null, new Error(`Failed to validate yaml (#1)`)];
|
|
97
|
+
return [null, new Error(`Failed to validate yaml (#1). Error message: ${zod_1.z.prettifyError(error)}`)];
|
|
95
98
|
return [null, new Error(`Failed to validate yaml (#2)`)];
|
|
96
99
|
}
|
|
97
100
|
return [parsed, null];
|
|
@@ -141,9 +144,17 @@ class ConfigLoader {
|
|
|
141
144
|
return [yaml, errors];
|
|
142
145
|
}
|
|
143
146
|
//region Utils
|
|
144
|
-
|
|
147
|
+
resolveFilePath(path) {
|
|
145
148
|
if (PKG_ROOT_REGEX.test(path))
|
|
146
149
|
path = path.replace(PKG_ROOT_REGEX, (0, getRootPackageDirname_1.getRootPackageDirnameSync)() + node_path_1.sep);
|
|
150
|
+
if (ENV_VAR_REGEX.test(path)) {
|
|
151
|
+
const m = path.match(ENV_VAR_REGEX);
|
|
152
|
+
if (m.groups?.['var_name']) {
|
|
153
|
+
if (process.env?.[m.groups['var_name']]) {
|
|
154
|
+
path = path.replace(ENV_VAR_REGEX, process.env[m.groups['var_name']].replace(/[\/\\]+$/, '') + node_path_1.sep);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
147
158
|
return (0, node_path_1.resolve)(path);
|
|
148
159
|
}
|
|
149
160
|
/**
|
|
@@ -153,7 +164,7 @@ class ConfigLoader {
|
|
|
153
164
|
* @private
|
|
154
165
|
*/
|
|
155
166
|
resolveIncludePath(fromDirname, toPath) {
|
|
156
|
-
return (0, node_path_1.resolve)(this.
|
|
167
|
+
return (0, node_path_1.resolve)(this.resolveFilePath(fromDirname), toPath);
|
|
157
168
|
}
|
|
158
169
|
}
|
|
159
170
|
exports.ConfigLoader = ConfigLoader;
|
|
@@ -11,7 +11,9 @@ export declare class ConfigLoader {
|
|
|
11
11
|
* @template {z.ZodObject} S
|
|
12
12
|
* @template {object} P
|
|
13
13
|
* @param {string} path You can use %pkgroot prefix for automatic project root resolution by AppConfigLoader.
|
|
14
|
-
*
|
|
14
|
+
* Also, you can use %env:ENV_VARIABLE to resolve config path based on environment variable
|
|
15
|
+
* Example 1: %pkgroot/config.yml
|
|
16
|
+
* Example 2: %env:CONFIG_PATH/config.yml
|
|
15
17
|
* @param {S} schema
|
|
16
18
|
* @param {P} [addProps]
|
|
17
19
|
* @returns {ResolvedConfig<S, P>}
|
|
@@ -39,7 +41,7 @@ export declare class ConfigLoader {
|
|
|
39
41
|
* @protected
|
|
40
42
|
*/
|
|
41
43
|
protected processIncludes(mainYamlDirname: string, yaml: any): ErrorResult<any, Error[]>;
|
|
42
|
-
private
|
|
44
|
+
private resolveFilePath;
|
|
43
45
|
/**
|
|
44
46
|
* @param {string} fromDirname
|
|
45
47
|
* @param {string} toPath
|
package/package.json
CHANGED