@hkdigital/lib-core 0.4.31 → 0.4.33
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/logging/internal/adapters/console.js +5 -5
- package/dist/logging/internal/adapters/pino.js +2 -1
- package/dist/logging/internal/factories/client.js +2 -2
- package/dist/logging/internal/factories/server.js +2 -2
- package/dist/services/service-manager/ServiceManager.d.ts +16 -0
- package/dist/services/service-manager/ServiceManager.js +48 -9
- package/dist/services/service-manager/constants.d.ts +6 -2
- package/dist/services/service-manager/constants.js +10 -3
- package/dist/util/sveltekit/env/README.md +1 -2
- package/dist/util/sveltekit/env/all.d.ts +4 -15
- package/dist/util/sveltekit/env/all.js +2 -20
- package/dist/util/sveltekit/env/parsers.d.ts +17 -31
- package/dist/util/sveltekit/env/parsers.js +15 -15
- package/dist/util/sveltekit/env/private.d.ts +6 -8
- package/dist/util/sveltekit/env/private.js +4 -4
- package/dist/util/sveltekit/env/public.d.ts +6 -8
- package/dist/util/sveltekit/env/public.js +3 -3
- package/dist/util/sveltekit/env-all.d.ts +1 -1
- package/dist/util/sveltekit/env-all.js +2 -4
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { dev } from '$app/environment';
|
|
2
|
-
import { LEVELS } from '../../levels.js';
|
|
2
|
+
import { LEVELS, DEBUG, INFO, WARN, ERROR } from '../../levels.js';
|
|
3
3
|
import {
|
|
4
4
|
findRelevantFrameIndex,
|
|
5
5
|
detectErrorMeta,
|
|
@@ -164,13 +164,13 @@ export class ConsoleAdapter {
|
|
|
164
164
|
*/
|
|
165
165
|
#getConsoleMethod(level) {
|
|
166
166
|
switch (level) {
|
|
167
|
-
case
|
|
167
|
+
case DEBUG:
|
|
168
168
|
return 'debug';
|
|
169
|
-
case
|
|
169
|
+
case INFO:
|
|
170
170
|
return 'info';
|
|
171
|
-
case
|
|
171
|
+
case WARN:
|
|
172
172
|
return 'warn';
|
|
173
|
-
case
|
|
173
|
+
case ERROR:
|
|
174
174
|
case 'fatal':
|
|
175
175
|
return 'error';
|
|
176
176
|
default:
|
|
@@ -5,6 +5,7 @@ import pino from 'pino';
|
|
|
5
5
|
import { dev } from '$app/environment';
|
|
6
6
|
|
|
7
7
|
import { HkPromise } from '../../../generic/promises.js';
|
|
8
|
+
import { ERROR } from '../../levels.js';
|
|
8
9
|
|
|
9
10
|
import {
|
|
10
11
|
detectErrorMeta,
|
|
@@ -240,7 +241,7 @@ export class PinoAdapter {
|
|
|
240
241
|
|
|
241
242
|
while (this.#messageQueue.length > 0) {
|
|
242
243
|
const queuedLog = this.#messageQueue.shift();
|
|
243
|
-
console[queuedLog.level ===
|
|
244
|
+
console[queuedLog.level === ERROR ? 'error' : 'log'](
|
|
244
245
|
`[${queuedLog.level.toUpperCase()}] ${queuedLog.message}`,
|
|
245
246
|
queuedLog.data
|
|
246
247
|
);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger } from '../logger/index.js';
|
|
2
2
|
import { ConsoleAdapter } from '../adapters/console.js';
|
|
3
|
-
import { INFO } from '../../levels.js';
|
|
3
|
+
import { INFO, LOG } from '../../levels.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a client-side logger with console adapter
|
|
@@ -15,7 +15,7 @@ export function createClientLogger(name, level = INFO, consoleOptions = {}) {
|
|
|
15
15
|
const adapter = new ConsoleAdapter({ ...consoleOptions, level });
|
|
16
16
|
|
|
17
17
|
// Connect adapter to logger events
|
|
18
|
-
logger.on(
|
|
18
|
+
logger.on(LOG, (logEvent) => adapter.handleLog(logEvent));
|
|
19
19
|
|
|
20
20
|
return logger;
|
|
21
21
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Logger } from '../logger/index.js';
|
|
2
2
|
import { PinoAdapter } from '../adapters/pino.js';
|
|
3
|
-
import { INFO } from '../../levels.js';
|
|
3
|
+
import { INFO, LOG } from '../../levels.js';
|
|
4
4
|
// import { expectNoSSRContext } from '../../../util/ssr/index.js';
|
|
5
5
|
|
|
6
6
|
/**
|
|
@@ -26,7 +26,7 @@ export function createServerLogger(name, level = INFO, pinoOptions = {}) {
|
|
|
26
26
|
// determination to fail
|
|
27
27
|
// -> Stop and start the dev server
|
|
28
28
|
//
|
|
29
|
-
logger.on(
|
|
29
|
+
logger.on(LOG, (logEvent) => adapter.handleLog(logEvent));
|
|
30
30
|
|
|
31
31
|
return logger;
|
|
32
32
|
}
|
|
@@ -140,6 +140,22 @@ export class ServiceManager extends EventEmitter {
|
|
|
140
140
|
* @returns {Function} Unsubscribe function
|
|
141
141
|
*/
|
|
142
142
|
onServiceLogEvent(listener: (logEvent: LogEvent) => void): Function;
|
|
143
|
+
/**
|
|
144
|
+
* Listen to log messages emitted by the ServiceManager itself
|
|
145
|
+
*
|
|
146
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
147
|
+
*
|
|
148
|
+
* @returns {Function} Unsubscribe function
|
|
149
|
+
*/
|
|
150
|
+
onManagerLogEvent(listener: (logEvent: LogEvent) => void): Function;
|
|
151
|
+
/**
|
|
152
|
+
* Listen to all log messages (both manager and services)
|
|
153
|
+
*
|
|
154
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
155
|
+
*
|
|
156
|
+
* @returns {Function} Unsubscribe function
|
|
157
|
+
*/
|
|
158
|
+
onLogEvent(listener: (logEvent: LogEvent) => void): Function;
|
|
143
159
|
/**
|
|
144
160
|
* Set log level for the ServiceManager itself
|
|
145
161
|
*
|
|
@@ -66,7 +66,16 @@
|
|
|
66
66
|
import { EventEmitter } from '../../generic/events.js';
|
|
67
67
|
import { Logger, DEBUG, INFO } from '../../logging/index.js';
|
|
68
68
|
|
|
69
|
-
import {
|
|
69
|
+
import {
|
|
70
|
+
SERVICE_LOG,
|
|
71
|
+
STATE_CHANGED,
|
|
72
|
+
HEALTH_CHANGED,
|
|
73
|
+
ERROR,
|
|
74
|
+
LOG,
|
|
75
|
+
SERVICE_STATE_CHANGED,
|
|
76
|
+
SERVICE_HEALTH_CHANGED,
|
|
77
|
+
SERVICE_ERROR
|
|
78
|
+
} from './constants.js';
|
|
70
79
|
import { parseServiceLogLevels } from './util.js';
|
|
71
80
|
|
|
72
81
|
import {
|
|
@@ -503,6 +512,36 @@ export class ServiceManager extends EventEmitter {
|
|
|
503
512
|
return this.on(SERVICE_LOG, listener);
|
|
504
513
|
}
|
|
505
514
|
|
|
515
|
+
/**
|
|
516
|
+
* Listen to log messages emitted by the ServiceManager itself
|
|
517
|
+
*
|
|
518
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
519
|
+
*
|
|
520
|
+
* @returns {Function} Unsubscribe function
|
|
521
|
+
*/
|
|
522
|
+
onManagerLogEvent(listener) {
|
|
523
|
+
return this.logger.on(LOG, listener);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Listen to all log messages (both manager and services)
|
|
528
|
+
*
|
|
529
|
+
* @param {(logEvent: LogEvent) => void} listener - Log event handler
|
|
530
|
+
*
|
|
531
|
+
* @returns {Function} Unsubscribe function
|
|
532
|
+
*/
|
|
533
|
+
onLogEvent(listener) {
|
|
534
|
+
// Listen to both service and manager logs
|
|
535
|
+
const unsubscribeService = this.onServiceLogEvent(listener);
|
|
536
|
+
const unsubscribeManager = this.onManagerLogEvent(listener);
|
|
537
|
+
|
|
538
|
+
// Return combined unsubscribe function
|
|
539
|
+
return () => {
|
|
540
|
+
unsubscribeService();
|
|
541
|
+
unsubscribeManager();
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
|
|
506
545
|
/**
|
|
507
546
|
* Set log level for the ServiceManager itself
|
|
508
547
|
*
|
|
@@ -587,21 +626,21 @@ export class ServiceManager extends EventEmitter {
|
|
|
587
626
|
*/
|
|
588
627
|
_attachServiceEvents(name, instance) {
|
|
589
628
|
// Forward service events
|
|
590
|
-
instance.on(
|
|
591
|
-
this.emit(
|
|
629
|
+
instance.on(STATE_CHANGED, (/** @type {StateChangeEvent} */ data) => {
|
|
630
|
+
this.emit(SERVICE_STATE_CHANGED, { service: name, data });
|
|
592
631
|
});
|
|
593
632
|
|
|
594
|
-
instance.on(
|
|
595
|
-
this.emit(
|
|
633
|
+
instance.on(HEALTH_CHANGED, (/** @type {HealthChangeEvent} */ data) => {
|
|
634
|
+
this.emit(SERVICE_HEALTH_CHANGED, { service: name, data });
|
|
596
635
|
});
|
|
597
636
|
|
|
598
|
-
instance.on(
|
|
599
|
-
this.emit(
|
|
637
|
+
instance.on(ERROR, (/** @type {ServiceErrorEvent} */ data) => {
|
|
638
|
+
this.emit(SERVICE_ERROR, { service: name, data });
|
|
600
639
|
});
|
|
601
640
|
|
|
602
641
|
// Forward log events
|
|
603
|
-
instance.logger.on(
|
|
604
|
-
this.emit(
|
|
642
|
+
instance.logger.on(LOG, (/** @type {LogEvent} */ logEvent) => {
|
|
643
|
+
this.emit(SERVICE_LOG, logEvent);
|
|
605
644
|
});
|
|
606
645
|
}
|
|
607
646
|
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
-
export const
|
|
2
|
-
export const
|
|
1
|
+
export const STATE_CHANGED: "stateChanged";
|
|
2
|
+
export const HEALTH_CHANGED: "healthChanged";
|
|
3
|
+
export const ERROR: "error";
|
|
4
|
+
export const LOG: "log";
|
|
5
|
+
export const SERVICE_STATE_CHANGED: "service:stateChanged";
|
|
6
|
+
export const SERVICE_HEALTH_CHANGED: "service:healthChanged";
|
|
3
7
|
export const SERVICE_ERROR: "service:error";
|
|
4
8
|
export const SERVICE_LOG: "service:log";
|
|
5
9
|
export const ANY_LOG_LEVEL: "*";
|
|
@@ -1,8 +1,15 @@
|
|
|
1
|
-
//
|
|
2
|
-
export const
|
|
3
|
-
export const
|
|
1
|
+
// Service event names (what services emit)
|
|
2
|
+
export const STATE_CHANGED = 'stateChanged';
|
|
3
|
+
export const HEALTH_CHANGED = 'healthChanged';
|
|
4
|
+
export const ERROR = 'error';
|
|
5
|
+
export const LOG = 'log';
|
|
6
|
+
|
|
7
|
+
// Manager event names (what ServiceManager emits)
|
|
8
|
+
export const SERVICE_STATE_CHANGED = 'service:stateChanged';
|
|
9
|
+
export const SERVICE_HEALTH_CHANGED = 'service:healthChanged';
|
|
4
10
|
export const SERVICE_ERROR = 'service:error';
|
|
5
11
|
export const SERVICE_LOG = 'service:log';
|
|
6
12
|
|
|
13
|
+
|
|
7
14
|
export const ANY_LOG_LEVEL = '*';
|
|
8
15
|
export const ANY_SERVICE_NAME = '*';
|
|
@@ -339,7 +339,6 @@ console.log(features);
|
|
|
339
339
|
- `getAllEnv(options)` - Get combined environment variables (server only)
|
|
340
340
|
- `getPublicEnvByPrefix(prefix, options)` - Get public variables by prefix
|
|
341
341
|
- `getPrivateEnvByPrefix(prefix, options)` - Get private variables by prefix
|
|
342
|
-
- `getAllEnvByPrefix(prefix, options)` - Get combined variables by prefix
|
|
343
342
|
|
|
344
343
|
### Raw Access
|
|
345
344
|
|
|
@@ -421,4 +420,4 @@ interface DatabaseConfig {
|
|
|
421
420
|
}
|
|
422
421
|
|
|
423
422
|
const { database }: { database: DatabaseConfig } = getPrivateEnv();
|
|
424
|
-
```
|
|
423
|
+
```
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* @param {boolean} [options.autoGroup=true]
|
|
14
14
|
* Enable automatic prefix grouping
|
|
15
15
|
*
|
|
16
|
-
* @returns {
|
|
16
|
+
* @returns {Record<string, any>} Grouped and parsed combined environment variables
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* // Environment variables:
|
|
@@ -34,21 +34,10 @@ export function getAllEnv(options?: {
|
|
|
34
34
|
camelCase?: boolean | undefined;
|
|
35
35
|
parseValues?: boolean | undefined;
|
|
36
36
|
autoGroup?: boolean | undefined;
|
|
37
|
-
}):
|
|
38
|
-
/**
|
|
39
|
-
* Get combined environment variables by prefix
|
|
40
|
-
*
|
|
41
|
-
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
42
|
-
* @param {Object} [options={}] - Parsing options
|
|
43
|
-
*
|
|
44
|
-
* @returns {Object} Parsed configuration object
|
|
45
|
-
*/
|
|
46
|
-
export function getAllEnvByPrefix(prefix: string, options?: Object): Object;
|
|
37
|
+
}): Record<string, any>;
|
|
47
38
|
/**
|
|
48
39
|
* Get raw combined environment variables (no parsing)
|
|
49
40
|
*
|
|
50
|
-
* @returns {
|
|
41
|
+
* @returns {Record<string, string|undefined>} Raw combined environment variables
|
|
51
42
|
*/
|
|
52
|
-
export function getRawAllEnv():
|
|
53
|
-
[x: string]: string;
|
|
54
|
-
};
|
|
43
|
+
export function getRawAllEnv(): Record<string, string | undefined>;
|
|
@@ -31,7 +31,7 @@ import { autoGroupEnvByPrefix } from './parsers.js';
|
|
|
31
31
|
* @param {boolean} [options.autoGroup=true]
|
|
32
32
|
* Enable automatic prefix grouping
|
|
33
33
|
*
|
|
34
|
-
* @returns {
|
|
34
|
+
* @returns {Record<string, any>} Grouped and parsed combined environment variables
|
|
35
35
|
*
|
|
36
36
|
* @example
|
|
37
37
|
* // Environment variables:
|
|
@@ -65,28 +65,10 @@ export function getAllEnv(options = {}) {
|
|
|
65
65
|
return { ...publicVars, ...privateVars };
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
/**
|
|
69
|
-
* Get combined environment variables by prefix
|
|
70
|
-
*
|
|
71
|
-
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
72
|
-
* @param {Object} [options={}] - Parsing options
|
|
73
|
-
*
|
|
74
|
-
* @returns {Object} Parsed configuration object
|
|
75
|
-
*/
|
|
76
|
-
export function getAllEnvByPrefix(prefix, options = {}) {
|
|
77
|
-
const prefixWithUnderscore = prefix.endsWith('_') ? prefix : `${prefix}_`;
|
|
78
|
-
|
|
79
|
-
return getAllEnv({
|
|
80
|
-
...options,
|
|
81
|
-
prefix: prefixWithUnderscore,
|
|
82
|
-
removePrefix: true
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
|
|
86
68
|
/**
|
|
87
69
|
* Get raw combined environment variables (no parsing)
|
|
88
70
|
*
|
|
89
|
-
* @returns {
|
|
71
|
+
* @returns {Record<string, string|undefined>} Raw combined environment variables
|
|
90
72
|
*/
|
|
91
73
|
export function getRawAllEnv() {
|
|
92
74
|
const publicVars = getRawPublicEnv();
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
/**
|
|
18
18
|
* Parse environment object with type conversion and key transformation
|
|
19
19
|
*
|
|
20
|
-
* @param {
|
|
20
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
21
21
|
* @param {Object} [options={}] - Parsing options
|
|
22
22
|
* @param {boolean} [options.camelCase=true]
|
|
23
23
|
* Convert env var names to camelCase object keys
|
|
@@ -27,28 +27,24 @@
|
|
|
27
27
|
* @param {boolean} [options.removePrefix=true]
|
|
28
28
|
* Remove prefix from resulting keys
|
|
29
29
|
*
|
|
30
|
-
* @returns {
|
|
30
|
+
* @returns {Record<string, any>} Parsed environment object
|
|
31
31
|
*/
|
|
32
|
-
export function parseEnv(env: {
|
|
33
|
-
[x: string]: string;
|
|
34
|
-
}, options?: {
|
|
32
|
+
export function parseEnv(env: Record<string, string | undefined>, options?: {
|
|
35
33
|
camelCase?: boolean | undefined;
|
|
36
34
|
parseValues?: boolean | undefined;
|
|
37
35
|
prefix?: string | undefined;
|
|
38
36
|
removePrefix?: boolean | undefined;
|
|
39
|
-
}):
|
|
37
|
+
}): Record<string, any>;
|
|
40
38
|
/**
|
|
41
39
|
* Parse environment variables by prefix
|
|
42
40
|
*
|
|
43
|
-
* @param {
|
|
41
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
44
42
|
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
45
43
|
* @param {Object} [options={}] - Parsing options
|
|
46
44
|
*
|
|
47
|
-
* @returns {
|
|
45
|
+
* @returns {Record<string, any>} Parsed configuration object
|
|
48
46
|
*/
|
|
49
|
-
export function parseEnvByPrefix(env:
|
|
50
|
-
[x: string]: string;
|
|
51
|
-
}, prefix: string, options?: Object): Object;
|
|
47
|
+
export function parseEnvByPrefix(env: Record<string, string | undefined>, prefix: string, options?: Object): Record<string, any>;
|
|
52
48
|
/**
|
|
53
49
|
* Convert SCREAMING_SNAKE_CASE to camelCase
|
|
54
50
|
*
|
|
@@ -72,14 +68,14 @@ export function parseValue(value: string): any;
|
|
|
72
68
|
* them into configuration objects. All variables with underscores are
|
|
73
69
|
* grouped by their prefix (the part before the first underscore).
|
|
74
70
|
*
|
|
75
|
-
* @param {
|
|
71
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
76
72
|
* @param {Object} [options={}] - Parsing options
|
|
77
73
|
* @param {boolean} [options.camelCase=true]
|
|
78
74
|
* Convert env var names to camelCase object keys
|
|
79
75
|
* @param {boolean} [options.parseValues=true]
|
|
80
76
|
* Parse string values to numbers/booleans when possible
|
|
81
77
|
*
|
|
82
|
-
* @returns {
|
|
78
|
+
* @returns {Record<string, Record<string, any> | any>} Grouped environment variables
|
|
83
79
|
*
|
|
84
80
|
* @example
|
|
85
81
|
* // Input env vars:
|
|
@@ -95,41 +91,31 @@ export function parseValue(value: string): any;
|
|
|
95
91
|
* // single: 'value' // No underscore, stays top-level
|
|
96
92
|
* // }
|
|
97
93
|
*/
|
|
98
|
-
export function autoGroupEnvByPrefix(env: {
|
|
99
|
-
[x: string]: string;
|
|
100
|
-
}, options?: {
|
|
94
|
+
export function autoGroupEnvByPrefix(env: Record<string, string | undefined>, options?: {
|
|
101
95
|
camelCase?: boolean | undefined;
|
|
102
96
|
parseValues?: boolean | undefined;
|
|
103
|
-
}):
|
|
104
|
-
[x: string]: Object;
|
|
105
|
-
};
|
|
97
|
+
}): Record<string, Record<string, any> | any>;
|
|
106
98
|
/**
|
|
107
99
|
* Group environment variables by specific prefixes
|
|
108
100
|
*
|
|
109
|
-
* @param {
|
|
101
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
110
102
|
* @param {string[]} prefixes - Array of prefixes to group by
|
|
111
103
|
* @param {Object} [options={}] - Parsing options
|
|
112
104
|
*
|
|
113
|
-
* @returns {
|
|
105
|
+
* @returns {Record<string, Record<string, any>>} Grouped environment variables
|
|
114
106
|
*
|
|
115
107
|
* @example
|
|
116
108
|
* const grouped = groupEnvByPrefixes(env, ['DATABASE', 'REDIS', 'JWT']);
|
|
117
109
|
* // Returns: { database: {...}, redis: {...}, jwt: {...} }
|
|
118
110
|
*/
|
|
119
|
-
export function groupEnvByPrefixes(env:
|
|
120
|
-
[x: string]: string;
|
|
121
|
-
}, prefixes: string[], options?: Object): {
|
|
122
|
-
[x: string]: Object;
|
|
123
|
-
};
|
|
111
|
+
export function groupEnvByPrefixes(env: Record<string, string | undefined>, prefixes: string[], options?: Object): Record<string, Record<string, any>>;
|
|
124
112
|
/**
|
|
125
113
|
* Filter environment variables by pattern
|
|
126
114
|
*
|
|
127
|
-
* @param {
|
|
115
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
128
116
|
* @param {RegExp|string} pattern - Pattern to match against keys
|
|
129
117
|
* @param {Object} [options={}] - Parsing options
|
|
130
118
|
*
|
|
131
|
-
* @returns {
|
|
119
|
+
* @returns {Record<string, any>} Filtered and parsed environment variables
|
|
132
120
|
*/
|
|
133
|
-
export function filterEnvByPattern(env:
|
|
134
|
-
[x: string]: string;
|
|
135
|
-
}, pattern: RegExp | string, options?: Object): Object;
|
|
121
|
+
export function filterEnvByPattern(env: Record<string, string | undefined>, pattern: RegExp | string, options?: Object): Record<string, any>;
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
/**
|
|
19
19
|
* Parse environment object with type conversion and key transformation
|
|
20
20
|
*
|
|
21
|
-
* @param {
|
|
21
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
22
22
|
* @param {Object} [options={}] - Parsing options
|
|
23
23
|
* @param {boolean} [options.camelCase=true]
|
|
24
24
|
* Convert env var names to camelCase object keys
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* @param {boolean} [options.removePrefix=true]
|
|
29
29
|
* Remove prefix from resulting keys
|
|
30
30
|
*
|
|
31
|
-
* @returns {
|
|
31
|
+
* @returns {Record<string, any>} Parsed environment object
|
|
32
32
|
*/
|
|
33
33
|
export function parseEnv(env, options = {}) {
|
|
34
34
|
const {
|
|
@@ -41,8 +41,8 @@ export function parseEnv(env, options = {}) {
|
|
|
41
41
|
const result = {};
|
|
42
42
|
|
|
43
43
|
for (const [key, value] of Object.entries(env || {})) {
|
|
44
|
-
// Skip if prefix specified and key doesn't match
|
|
45
|
-
if (prefix && !key.startsWith(prefix)) {
|
|
44
|
+
// Skip if value is undefined or if prefix specified and key doesn't match
|
|
45
|
+
if (value === undefined || (prefix && !key.startsWith(prefix))) {
|
|
46
46
|
continue;
|
|
47
47
|
}
|
|
48
48
|
|
|
@@ -73,11 +73,11 @@ export function parseEnv(env, options = {}) {
|
|
|
73
73
|
/**
|
|
74
74
|
* Parse environment variables by prefix
|
|
75
75
|
*
|
|
76
|
-
* @param {
|
|
76
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
77
77
|
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
78
78
|
* @param {Object} [options={}] - Parsing options
|
|
79
79
|
*
|
|
80
|
-
* @returns {
|
|
80
|
+
* @returns {Record<string, any>} Parsed configuration object
|
|
81
81
|
*/
|
|
82
82
|
export function parseEnvByPrefix(env, prefix, options = {}) {
|
|
83
83
|
const prefixWithUnderscore = prefix.endsWith('_') ? prefix : `${prefix}_`;
|
|
@@ -135,14 +135,14 @@ export function parseValue(value) {
|
|
|
135
135
|
* them into configuration objects. All variables with underscores are
|
|
136
136
|
* grouped by their prefix (the part before the first underscore).
|
|
137
137
|
*
|
|
138
|
-
* @param {
|
|
138
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
139
139
|
* @param {Object} [options={}] - Parsing options
|
|
140
140
|
* @param {boolean} [options.camelCase=true]
|
|
141
141
|
* Convert env var names to camelCase object keys
|
|
142
142
|
* @param {boolean} [options.parseValues=true]
|
|
143
143
|
* Parse string values to numbers/booleans when possible
|
|
144
144
|
*
|
|
145
|
-
* @returns {
|
|
145
|
+
* @returns {Record<string, Record<string, any> | any>} Grouped environment variables
|
|
146
146
|
*
|
|
147
147
|
* @example
|
|
148
148
|
* // Input env vars:
|
|
@@ -197,7 +197,7 @@ export function autoGroupEnvByPrefix(env, options = {}) {
|
|
|
197
197
|
|
|
198
198
|
// Add remaining variables (no underscore) as top-level properties
|
|
199
199
|
for (const [key, value] of Object.entries(env)) {
|
|
200
|
-
if (!usedKeys.has(key)) {
|
|
200
|
+
if (!usedKeys.has(key) && value !== undefined) {
|
|
201
201
|
const finalKey = camelCase ? toCamelCase(key) : key.toLowerCase();
|
|
202
202
|
result[finalKey] = parseValues ? parseValue(value) : value;
|
|
203
203
|
}
|
|
@@ -209,11 +209,11 @@ export function autoGroupEnvByPrefix(env, options = {}) {
|
|
|
209
209
|
/**
|
|
210
210
|
* Group environment variables by specific prefixes
|
|
211
211
|
*
|
|
212
|
-
* @param {
|
|
212
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
213
213
|
* @param {string[]} prefixes - Array of prefixes to group by
|
|
214
214
|
* @param {Object} [options={}] - Parsing options
|
|
215
215
|
*
|
|
216
|
-
* @returns {
|
|
216
|
+
* @returns {Record<string, Record<string, any>>} Grouped environment variables
|
|
217
217
|
*
|
|
218
218
|
* @example
|
|
219
219
|
* const grouped = groupEnvByPrefixes(env, ['DATABASE', 'REDIS', 'JWT']);
|
|
@@ -236,19 +236,19 @@ export function groupEnvByPrefixes(env, prefixes, options = {}) {
|
|
|
236
236
|
/**
|
|
237
237
|
* Filter environment variables by pattern
|
|
238
238
|
*
|
|
239
|
-
* @param {
|
|
239
|
+
* @param {Record<string, string | undefined>} env - Raw environment variables
|
|
240
240
|
* @param {RegExp|string} pattern - Pattern to match against keys
|
|
241
241
|
* @param {Object} [options={}] - Parsing options
|
|
242
242
|
*
|
|
243
|
-
* @returns {
|
|
243
|
+
* @returns {Record<string, any>} Filtered and parsed environment variables
|
|
244
244
|
*/
|
|
245
245
|
export function filterEnvByPattern(env, pattern, options = {}) {
|
|
246
246
|
const regex = typeof pattern === 'string' ? new RegExp(pattern) : pattern;
|
|
247
|
-
/** @type {
|
|
247
|
+
/** @type {Record<string, string>} */
|
|
248
248
|
const filtered = {};
|
|
249
249
|
|
|
250
250
|
for (const [key, value] of Object.entries(env || {})) {
|
|
251
|
-
if (regex.test(key)) {
|
|
251
|
+
if (regex.test(key) && value !== undefined) {
|
|
252
252
|
filtered[key] = value;
|
|
253
253
|
}
|
|
254
254
|
}
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* @param {boolean} [options.autoGroup=true]
|
|
14
14
|
* Enable automatic prefix grouping
|
|
15
15
|
*
|
|
16
|
-
* @returns {
|
|
16
|
+
* @returns {Record<string, any>} Grouped and parsed private environment variables
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* // Environment variables:
|
|
@@ -36,21 +36,19 @@ export function getPrivateEnv(options?: {
|
|
|
36
36
|
camelCase?: boolean | undefined;
|
|
37
37
|
parseValues?: boolean | undefined;
|
|
38
38
|
autoGroup?: boolean | undefined;
|
|
39
|
-
}):
|
|
39
|
+
}): Record<string, any>;
|
|
40
40
|
/**
|
|
41
41
|
* Get private environment variables by prefix
|
|
42
42
|
*
|
|
43
43
|
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
44
44
|
* @param {Object} [options={}] - Parsing options
|
|
45
45
|
*
|
|
46
|
-
* @returns {
|
|
46
|
+
* @returns {Record<string, any>} Parsed configuration object
|
|
47
47
|
*/
|
|
48
|
-
export function getPrivateEnvByPrefix(prefix: string, options?: Object):
|
|
48
|
+
export function getPrivateEnvByPrefix(prefix: string, options?: Object): Record<string, any>;
|
|
49
49
|
/**
|
|
50
50
|
* Get raw private environment variables (no parsing)
|
|
51
51
|
*
|
|
52
|
-
* @returns {
|
|
52
|
+
* @returns {Record<string, string|undefined>} Raw private environment variables
|
|
53
53
|
*/
|
|
54
|
-
export function getRawPrivateEnv():
|
|
55
|
-
[x: string]: string;
|
|
56
|
-
};
|
|
54
|
+
export function getRawPrivateEnv(): Record<string, string | undefined>;
|
|
@@ -30,7 +30,7 @@ import { autoGroupEnvByPrefix, parseEnv } from './parsers.js';
|
|
|
30
30
|
* @param {boolean} [options.autoGroup=true]
|
|
31
31
|
* Enable automatic prefix grouping
|
|
32
32
|
*
|
|
33
|
-
* @returns {
|
|
33
|
+
* @returns {Record<string, any>} Grouped and parsed private environment variables
|
|
34
34
|
*
|
|
35
35
|
* @example
|
|
36
36
|
* // Environment variables:
|
|
@@ -65,7 +65,7 @@ export function getPrivateEnv(options = {}) {
|
|
|
65
65
|
* @param {string} prefix - Environment variable prefix (e.g., 'DATABASE')
|
|
66
66
|
* @param {Object} [options={}] - Parsing options
|
|
67
67
|
*
|
|
68
|
-
* @returns {
|
|
68
|
+
* @returns {Record<string, any>} Parsed configuration object
|
|
69
69
|
*/
|
|
70
70
|
export function getPrivateEnvByPrefix(prefix, options = {}) {
|
|
71
71
|
const prefixWithUnderscore = prefix.endsWith('_') ? prefix : `${prefix}_`;
|
|
@@ -80,8 +80,8 @@ export function getPrivateEnvByPrefix(prefix, options = {}) {
|
|
|
80
80
|
/**
|
|
81
81
|
* Get raw private environment variables (no parsing)
|
|
82
82
|
*
|
|
83
|
-
* @returns {
|
|
83
|
+
* @returns {Record<string, string|undefined>} Raw private environment variables
|
|
84
84
|
*/
|
|
85
85
|
export function getRawPrivateEnv() {
|
|
86
86
|
return { ...env };
|
|
87
|
-
}
|
|
87
|
+
}
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
* @param {boolean} [options.autoGroup=true]
|
|
14
14
|
* Enable automatic prefix grouping
|
|
15
15
|
*
|
|
16
|
-
* @returns {
|
|
16
|
+
* @returns {Record<string, any>} Grouped and parsed public environment variables
|
|
17
17
|
*
|
|
18
18
|
* @example
|
|
19
19
|
* // Environment variables:
|
|
@@ -32,21 +32,19 @@ export function getPublicEnv(options?: {
|
|
|
32
32
|
camelCase?: boolean | undefined;
|
|
33
33
|
parseValues?: boolean | undefined;
|
|
34
34
|
autoGroup?: boolean | undefined;
|
|
35
|
-
}):
|
|
35
|
+
}): Record<string, any>;
|
|
36
36
|
/**
|
|
37
37
|
* Get public environment variables by prefix
|
|
38
38
|
*
|
|
39
39
|
* @param {string} prefix - Environment variable prefix (e.g., 'PUBLIC_API')
|
|
40
40
|
* @param {Object} [options={}] - Parsing options
|
|
41
41
|
*
|
|
42
|
-
* @returns {
|
|
42
|
+
* @returns {Record<string, any>} Parsed configuration object
|
|
43
43
|
*/
|
|
44
|
-
export function getPublicEnvByPrefix(prefix: string, options?: Object):
|
|
44
|
+
export function getPublicEnvByPrefix(prefix: string, options?: Object): Record<string, any>;
|
|
45
45
|
/**
|
|
46
46
|
* Get raw public environment variables (no parsing)
|
|
47
47
|
*
|
|
48
|
-
* @returns {
|
|
48
|
+
* @returns {Record<string, string>} Raw public environment variables
|
|
49
49
|
*/
|
|
50
|
-
export function getRawPublicEnv():
|
|
51
|
-
[x: string]: string;
|
|
52
|
-
};
|
|
50
|
+
export function getRawPublicEnv(): Record<string, string>;
|
|
@@ -29,7 +29,7 @@ import { autoGroupEnvByPrefix, parseEnv } from './parsers.js';
|
|
|
29
29
|
* @param {boolean} [options.autoGroup=true]
|
|
30
30
|
* Enable automatic prefix grouping
|
|
31
31
|
*
|
|
32
|
-
* @returns {
|
|
32
|
+
* @returns {Record<string, any>} Grouped and parsed public environment variables
|
|
33
33
|
*
|
|
34
34
|
* @example
|
|
35
35
|
* // Environment variables:
|
|
@@ -60,7 +60,7 @@ export function getPublicEnv(options = {}) {
|
|
|
60
60
|
* @param {string} prefix - Environment variable prefix (e.g., 'PUBLIC_API')
|
|
61
61
|
* @param {Object} [options={}] - Parsing options
|
|
62
62
|
*
|
|
63
|
-
* @returns {
|
|
63
|
+
* @returns {Record<string, any>} Parsed configuration object
|
|
64
64
|
*/
|
|
65
65
|
export function getPublicEnvByPrefix(prefix, options = {}) {
|
|
66
66
|
const prefixWithUnderscore = prefix.endsWith('_') ? prefix : `${prefix}_`;
|
|
@@ -75,7 +75,7 @@ export function getPublicEnvByPrefix(prefix, options = {}) {
|
|
|
75
75
|
/**
|
|
76
76
|
* Get raw public environment variables (no parsing)
|
|
77
77
|
*
|
|
78
|
-
* @returns {
|
|
78
|
+
* @returns {Record<string, string>} Raw public environment variables
|
|
79
79
|
*/
|
|
80
80
|
export function getRawPublicEnv() {
|
|
81
81
|
return { ...env };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { getAllEnv,
|
|
1
|
+
export { getAllEnv, getRawAllEnv } from "./env/all.js";
|
|
@@ -6,14 +6,12 @@
|
|
|
6
6
|
* imports private environment variables.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
|
-
* import { getAllEnv
|
|
9
|
+
* import { getAllEnv } from './env-all.js';
|
|
10
10
|
*
|
|
11
11
|
* const allVars = getAllEnv();
|
|
12
|
-
* const dbConfig = getAllEnvByPrefix('DATABASE');
|
|
13
12
|
*/
|
|
14
13
|
|
|
15
14
|
export {
|
|
16
15
|
getAllEnv,
|
|
17
|
-
getAllEnvByPrefix,
|
|
18
16
|
getRawAllEnv
|
|
19
|
-
} from './env/all.js';
|
|
17
|
+
} from './env/all.js';
|