@devopsplaybook.io/common-utils 1.2.0-beta.7.3382b46 → 1.2.0-beta.7.84f3f67
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/dist/src/ConfigBase.d.ts +0 -13
- package/dist/src/ConfigBase.js +11 -32
- package/package.json +1 -1
- package/src/ConfigBase.ts +11 -51
package/dist/src/ConfigBase.d.ts
CHANGED
|
@@ -7,19 +7,6 @@ export interface ConfigFieldDef {
|
|
|
7
7
|
field: string;
|
|
8
8
|
/** When `true` the value is masked in log output. */
|
|
9
9
|
sensitive?: boolean;
|
|
10
|
-
/**
|
|
11
|
-
* Alternative environment variable names to check when the primary field
|
|
12
|
-
* name is not found in `process.env`. Aliases are tried in order and the
|
|
13
|
-
* first match wins. Only checked in the environment layer, never in the
|
|
14
|
-
* config-file layer.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* // Look for DATABASE_POSTGRES_HOST first, then fall back to POSTGRES_HOST
|
|
19
|
-
* { field: "DATABASE_POSTGRES_HOST", envAliases: ["POSTGRES_HOST"] }
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
envAliases?: string[];
|
|
23
10
|
}
|
|
24
11
|
/**
|
|
25
12
|
* Database-specific configuration fields shared by every project that
|
package/dist/src/ConfigBase.js
CHANGED
|
@@ -117,11 +117,11 @@ class ConfigBase {
|
|
|
117
117
|
{ field: "JWT_KEY", sensitive: true },
|
|
118
118
|
{ field: "LOG_LEVEL" },
|
|
119
119
|
{ field: "DATABASE_TYPE" },
|
|
120
|
-
{ field: "DATABASE_POSTGRES_HOST"
|
|
121
|
-
{ field: "DATABASE_POSTGRES_PORT"
|
|
122
|
-
{ field: "DATABASE_POSTGRES_USER"
|
|
123
|
-
{ field: "DATABASE_POSTGRES_PASSWORD", sensitive: true
|
|
124
|
-
{ field: "DATABASE_POSTGRES_DATABASE"
|
|
120
|
+
{ field: "DATABASE_POSTGRES_HOST" },
|
|
121
|
+
{ field: "DATABASE_POSTGRES_PORT" },
|
|
122
|
+
{ field: "DATABASE_POSTGRES_USER" },
|
|
123
|
+
{ field: "DATABASE_POSTGRES_PASSWORD", sensitive: true },
|
|
124
|
+
{ field: "DATABASE_POSTGRES_DATABASE" },
|
|
125
125
|
{ field: "OPENTELEMETRY_COLLECTOR_HTTP_TRACES" },
|
|
126
126
|
{ field: "OPENTELEMETRY_COLLECTOR_HTTP_METRICS" },
|
|
127
127
|
{ field: "OPENTELEMETRY_COLLECTOR_HTTP_LOGS" },
|
|
@@ -146,11 +146,10 @@ class ConfigBase {
|
|
|
146
146
|
* Call this in your subclass constructor for every project-specific field.
|
|
147
147
|
*/
|
|
148
148
|
addConfigField(def) {
|
|
149
|
-
var _a
|
|
149
|
+
var _a;
|
|
150
150
|
this._fields.push({
|
|
151
151
|
field: def.field,
|
|
152
152
|
sensitive: (_a = def.sensitive) !== null && _a !== void 0 ? _a : false,
|
|
153
|
-
envAliases: (_b = def.envAliases) !== null && _b !== void 0 ? _b : [],
|
|
154
153
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
155
154
|
defaultValue: this[def.field],
|
|
156
155
|
});
|
|
@@ -176,34 +175,14 @@ class ConfigBase {
|
|
|
176
175
|
log(`Configuration Value: CONFIG_FILE: ${this.CONFIG_FILE}`);
|
|
177
176
|
log(`Configuration Value: SERVICE_ID: ${this.SERVICE_ID}`);
|
|
178
177
|
log(`Configuration Value: VERSION: ${this.VERSION}`);
|
|
179
|
-
for (const { field, sensitive
|
|
178
|
+
for (const { field, sensitive } of this._fields) {
|
|
180
179
|
let from = "defaults";
|
|
181
|
-
let foundValue;
|
|
182
|
-
let usedAlias = false;
|
|
183
|
-
// 1. Primary environment variable name
|
|
184
180
|
if (process.env[field] !== undefined) {
|
|
185
|
-
foundValue = process.env[field];
|
|
186
|
-
from = "environment";
|
|
187
|
-
}
|
|
188
|
-
// 2. Check aliases if primary env var was not set
|
|
189
|
-
if (foundValue === undefined && envAliases.length > 0) {
|
|
190
|
-
for (const alias of envAliases) {
|
|
191
|
-
if (process.env[alias] !== undefined) {
|
|
192
|
-
foundValue = process.env[alias];
|
|
193
|
-
from = "environment";
|
|
194
|
-
usedAlias = true;
|
|
195
|
-
break;
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
// 3. Apply environment value (full name or alias) if found
|
|
200
|
-
if (foundValue !== undefined) {
|
|
201
181
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
202
|
-
this[field] =
|
|
182
|
+
this[field] = process.env[field];
|
|
183
|
+
from = "environment";
|
|
203
184
|
}
|
|
204
|
-
|
|
205
|
-
// environment nor alias matched, check config file)
|
|
206
|
-
if (foundValue === undefined && content[field] !== undefined) {
|
|
185
|
+
else if (content[field] !== undefined) {
|
|
207
186
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
208
187
|
this[field] = content[field];
|
|
209
188
|
from = "config";
|
|
@@ -214,7 +193,7 @@ class ConfigBase {
|
|
|
214
193
|
else {
|
|
215
194
|
log(
|
|
216
195
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
217
|
-
`Configuration Value: ${field}: ${this[field]} (from ${from}
|
|
196
|
+
`Configuration Value: ${field}: ${this[field]} (from ${from})`);
|
|
218
197
|
}
|
|
219
198
|
}
|
|
220
199
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devopsplaybook.io/common-utils",
|
|
3
|
-
"version": "1.2.0-beta.7.
|
|
3
|
+
"version": "1.2.0-beta.7.84f3f67",
|
|
4
4
|
"description": "Shared utility modules for devopsplaybook.io projects (DB, Config, OTel context, system helpers)",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Open Telemetry",
|
package/src/ConfigBase.ts
CHANGED
|
@@ -11,19 +11,6 @@ export interface ConfigFieldDef {
|
|
|
11
11
|
field: string;
|
|
12
12
|
/** When `true` the value is masked in log output. */
|
|
13
13
|
sensitive?: boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Alternative environment variable names to check when the primary field
|
|
16
|
-
* name is not found in `process.env`. Aliases are tried in order and the
|
|
17
|
-
* first match wins. Only checked in the environment layer, never in the
|
|
18
|
-
* config-file layer.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* // Look for DATABASE_POSTGRES_HOST first, then fall back to POSTGRES_HOST
|
|
23
|
-
* { field: "DATABASE_POSTGRES_HOST", envAliases: ["POSTGRES_HOST"] }
|
|
24
|
-
* ```
|
|
25
|
-
*/
|
|
26
|
-
envAliases?: string[];
|
|
27
14
|
}
|
|
28
15
|
|
|
29
16
|
/**
|
|
@@ -110,7 +97,7 @@ export abstract class ConfigBase implements ConfigCommonInterface {
|
|
|
110
97
|
* should process. Common / DB / OTel fields are pre-registered.
|
|
111
98
|
*/
|
|
112
99
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
113
|
-
private _fields: { field: string; sensitive: boolean;
|
|
100
|
+
private _fields: { field: string; sensitive: boolean; defaultValue: any }[] =
|
|
114
101
|
[];
|
|
115
102
|
|
|
116
103
|
/**
|
|
@@ -140,11 +127,11 @@ export abstract class ConfigBase implements ConfigCommonInterface {
|
|
|
140
127
|
{ field: "JWT_KEY", sensitive: true },
|
|
141
128
|
{ field: "LOG_LEVEL" },
|
|
142
129
|
{ field: "DATABASE_TYPE" },
|
|
143
|
-
{ field: "DATABASE_POSTGRES_HOST"
|
|
144
|
-
{ field: "DATABASE_POSTGRES_PORT"
|
|
145
|
-
{ field: "DATABASE_POSTGRES_USER"
|
|
146
|
-
{ field: "DATABASE_POSTGRES_PASSWORD", sensitive: true
|
|
147
|
-
{ field: "DATABASE_POSTGRES_DATABASE"
|
|
130
|
+
{ field: "DATABASE_POSTGRES_HOST" },
|
|
131
|
+
{ field: "DATABASE_POSTGRES_PORT" },
|
|
132
|
+
{ field: "DATABASE_POSTGRES_USER" },
|
|
133
|
+
{ field: "DATABASE_POSTGRES_PASSWORD", sensitive: true },
|
|
134
|
+
{ field: "DATABASE_POSTGRES_DATABASE" },
|
|
148
135
|
{ field: "OPENTELEMETRY_COLLECTOR_HTTP_TRACES" },
|
|
149
136
|
{ field: "OPENTELEMETRY_COLLECTOR_HTTP_METRICS" },
|
|
150
137
|
{ field: "OPENTELEMETRY_COLLECTOR_HTTP_LOGS" },
|
|
@@ -173,7 +160,6 @@ export abstract class ConfigBase implements ConfigCommonInterface {
|
|
|
173
160
|
this._fields.push({
|
|
174
161
|
field: def.field,
|
|
175
162
|
sensitive: def.sensitive ?? false,
|
|
176
|
-
envAliases: def.envAliases ?? [],
|
|
177
163
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
178
164
|
defaultValue: (this as any)[def.field],
|
|
179
165
|
});
|
|
@@ -201,43 +187,17 @@ export abstract class ConfigBase implements ConfigCommonInterface {
|
|
|
201
187
|
log(`Configuration Value: SERVICE_ID: ${this.SERVICE_ID}`);
|
|
202
188
|
log(`Configuration Value: VERSION: ${this.VERSION}`);
|
|
203
189
|
|
|
204
|
-
for (const { field, sensitive
|
|
190
|
+
for (const { field, sensitive } of this._fields) {
|
|
205
191
|
let from = "defaults";
|
|
206
|
-
let foundValue: string | undefined;
|
|
207
|
-
let usedAlias = false;
|
|
208
|
-
|
|
209
|
-
// 1. Primary environment variable name
|
|
210
192
|
if (process.env[field] !== undefined) {
|
|
211
|
-
foundValue = process.env[field];
|
|
212
|
-
from = "environment";
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// 2. Check aliases if primary env var was not set
|
|
216
|
-
if (foundValue === undefined && envAliases.length > 0) {
|
|
217
|
-
for (const alias of envAliases) {
|
|
218
|
-
if (process.env[alias] !== undefined) {
|
|
219
|
-
foundValue = process.env[alias];
|
|
220
|
-
from = "environment";
|
|
221
|
-
usedAlias = true;
|
|
222
|
-
break;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// 3. Apply environment value (full name or alias) if found
|
|
228
|
-
if (foundValue !== undefined) {
|
|
229
193
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
230
|
-
(this as any)[field] =
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
// 4. Config file override (environment always wins, but if neither
|
|
234
|
-
// environment nor alias matched, check config file)
|
|
235
|
-
if (foundValue === undefined && content[field] !== undefined) {
|
|
194
|
+
(this as any)[field] = process.env[field];
|
|
195
|
+
from = "environment";
|
|
196
|
+
} else if (content[field] !== undefined) {
|
|
236
197
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
237
198
|
(this as any)[field] = content[field];
|
|
238
199
|
from = "config";
|
|
239
200
|
}
|
|
240
|
-
|
|
241
201
|
if (sensitive) {
|
|
242
202
|
log(
|
|
243
203
|
`Configuration Value: ${field}: ******************** (from ${from})`,
|
|
@@ -245,7 +205,7 @@ export abstract class ConfigBase implements ConfigCommonInterface {
|
|
|
245
205
|
} else {
|
|
246
206
|
log(
|
|
247
207
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
248
|
-
`Configuration Value: ${field}: ${(this as any)[field]} (from ${from}
|
|
208
|
+
`Configuration Value: ${field}: ${(this as any)[field]} (from ${from})`,
|
|
249
209
|
);
|
|
250
210
|
}
|
|
251
211
|
}
|