@haskou/ddd-kernel 1.0.3 → 1.2.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/dist/{RetryPredicate-U7dYnQ4N.d.ts → RetryPredicate-Bl3SMnbL.d.ts} +1 -1
- package/dist/{RetryPredicate-yT_z9zk1.d.cts → RetryPredicate-ZjioTYXn.d.cts} +1 -1
- package/dist/{ShutdownHook-CMWLsfu-.d.ts → ShutdownHook-B03Mcke-.d.ts} +54 -7
- package/dist/{ShutdownHook-BjbnCKzr.d.cts → ShutdownHook-BsEGvnEf.d.cts} +54 -7
- package/dist/adapters/index.cjs +101 -0
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +3 -2
- package/dist/adapters/index.d.ts +3 -2
- package/dist/adapters/index.js +101 -0
- package/dist/adapters/index.js.map +1 -1
- package/dist/adapters/pubsub/amqp/index.cjs +101 -0
- package/dist/adapters/pubsub/amqp/index.cjs.map +1 -1
- package/dist/adapters/pubsub/amqp/index.js +101 -0
- package/dist/adapters/pubsub/amqp/index.js.map +1 -1
- package/dist/adapters/pubsub/index.cjs +101 -0
- package/dist/adapters/pubsub/index.cjs.map +1 -1
- package/dist/adapters/pubsub/index.d.cts +4 -3
- package/dist/adapters/pubsub/index.d.ts +4 -3
- package/dist/adapters/pubsub/index.js +101 -0
- package/dist/adapters/pubsub/index.js.map +1 -1
- package/dist/adapters/ui/express/index.d.cts +3 -2
- package/dist/adapters/ui/express/index.d.ts +3 -2
- package/dist/adapters/ui/index.cjs +101 -0
- package/dist/adapters/ui/index.cjs.map +1 -1
- package/dist/adapters/ui/index.d.cts +2 -1
- package/dist/adapters/ui/index.d.ts +2 -1
- package/dist/adapters/ui/index.js +101 -0
- package/dist/adapters/ui/index.js.map +1 -1
- package/dist/adapters/ui/routes/index.cjs +101 -0
- package/dist/adapters/ui/routes/index.cjs.map +1 -1
- package/dist/adapters/ui/routes/index.js +101 -0
- package/dist/adapters/ui/routes/index.js.map +1 -1
- package/dist/contracts/index.d.cts +3 -2
- package/dist/contracts/index.d.ts +3 -2
- package/dist/contracts/kernel/index.d.cts +3 -2
- package/dist/contracts/kernel/index.d.ts +3 -2
- package/dist/index.cjs +103 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -2
- package/dist/index.d.ts +9 -2
- package/dist/index.js +102 -0
- package/dist/index.js.map +1 -1
- package/dist/infrastructure/express/index.d.cts +2 -1
- package/dist/infrastructure/express/index.d.ts +2 -1
- package/dist/infrastructure/scheduler/index.cjs +101 -0
- package/dist/infrastructure/scheduler/index.cjs.map +1 -1
- package/dist/infrastructure/scheduler/index.js +101 -0
- package/dist/infrastructure/scheduler/index.js.map +1 -1
- package/package.json +2 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { D as DomainEvent } from './DomainEvent-mXWEtr_J.js';
|
|
2
|
+
import { DotenvConfigOutput } from 'dotenv';
|
|
2
3
|
import { D as DomainEventConsumer } from './DomainEventConsumer-BroJmVty.js';
|
|
3
4
|
import { Route } from './adapters/ui/routes/index.js';
|
|
4
5
|
import { b as DependencyOverride, D as DependencyInjection, S as ServiceClass } from './ServiceClass-Bq_fBC5R.js';
|
|
@@ -26,14 +27,49 @@ interface KernelDependencyInjectionOptions {
|
|
|
26
27
|
readonly sourceDirectory?: string;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
type KernelDefaultEnvironment = NodeJS.ProcessEnv;
|
|
31
|
+
|
|
32
|
+
type KernelEnvironmentVariableType = 'boolean' | 'number' | 'string';
|
|
33
|
+
|
|
34
|
+
type KernelEnvironmentVariablePrimitive<TType extends KernelEnvironmentVariableType> = TType extends 'boolean' ? boolean : TType extends 'number' ? number : string;
|
|
35
|
+
|
|
36
|
+
interface KernelEnvironmentVariableDefinition<TType extends KernelEnvironmentVariableType = KernelEnvironmentVariableType, TRequired extends boolean = boolean> {
|
|
37
|
+
readonly defaultValue?: KernelEnvironmentVariablePrimitive<TType>;
|
|
38
|
+
readonly required?: TRequired;
|
|
39
|
+
readonly type: TType;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type KernelEnvironmentSchema = Readonly<Record<string, KernelEnvironmentVariableDefinition>>;
|
|
43
|
+
|
|
44
|
+
type KernelEnvironmentValue = boolean | number | string;
|
|
45
|
+
|
|
46
|
+
type KernelEnvironmentVariable<TDefinition extends KernelEnvironmentVariableDefinition> = TDefinition extends KernelEnvironmentVariableDefinition<infer TType extends KernelEnvironmentVariableType, infer TRequired extends boolean> ? TDefinition extends {
|
|
47
|
+
readonly defaultValue: unknown;
|
|
48
|
+
} ? KernelEnvironmentVariablePrimitive<TType> : TRequired extends true ? KernelEnvironmentVariablePrimitive<TType> : KernelEnvironmentVariablePrimitive<TType> | undefined : never;
|
|
49
|
+
|
|
50
|
+
type KernelEnvironment<TSchema extends KernelEnvironmentSchema> = {
|
|
51
|
+
readonly [key: string]: KernelEnvironmentValue | undefined;
|
|
52
|
+
} & {
|
|
53
|
+
readonly [TKey in keyof TSchema]: KernelEnvironmentVariable<TSchema[TKey]>;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
type KernelEnvironmentForSchema<TSchema extends KernelEnvironmentSchema | undefined> = TSchema extends KernelEnvironmentSchema ? KernelEnvironment<TSchema> : KernelDefaultEnvironment;
|
|
57
|
+
|
|
58
|
+
interface KernelEnvironmentVariablesOptions<TSchema extends KernelEnvironmentSchema | undefined = undefined> {
|
|
59
|
+
readonly override?: boolean;
|
|
60
|
+
readonly path?: string;
|
|
61
|
+
readonly schema?: TSchema;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface KernelOptions<TEnvironmentSchema extends KernelEnvironmentSchema | undefined = undefined> {
|
|
30
65
|
readonly di?: DependencyInjection;
|
|
66
|
+
readonly environmentSchema?: TEnvironmentSchema;
|
|
31
67
|
readonly logger?: KernelLogger;
|
|
32
68
|
readonly servicesYamlPath?: string;
|
|
33
69
|
readonly sourceDirectory?: string;
|
|
34
70
|
}
|
|
35
71
|
|
|
36
|
-
declare class Kernel {
|
|
72
|
+
declare class Kernel<TEnvironmentSchema extends KernelEnvironmentSchema | undefined = undefined> {
|
|
37
73
|
private readonly options;
|
|
38
74
|
private static readonly stateKey;
|
|
39
75
|
private readonly consumerMiddlewares;
|
|
@@ -43,19 +79,28 @@ declare class Kernel {
|
|
|
43
79
|
private readonly schedulersList;
|
|
44
80
|
private readonly shutdownHooks;
|
|
45
81
|
private dependencyInjectionInstance;
|
|
82
|
+
private environmentVariables;
|
|
46
83
|
private static get state();
|
|
47
84
|
static get configDirectory(): string;
|
|
48
85
|
static get consumers(): Consumer[];
|
|
49
86
|
static get consumerMiddleware(): ConsumerMiddleware[];
|
|
50
87
|
static get di(): DependencyInjection;
|
|
88
|
+
static get environment(): NodeJS.ProcessEnv;
|
|
51
89
|
static get logger(): KernelLogger;
|
|
52
|
-
static get active(): Kernel
|
|
90
|
+
static get active(): Kernel<KernelEnvironmentSchema | undefined>;
|
|
53
91
|
static get rootDirectory(): string;
|
|
54
92
|
static get routes(): ServiceClass<Route>[];
|
|
55
93
|
static get schedulers(): Scheduler[];
|
|
56
94
|
static get sourceDirectory(): string;
|
|
57
95
|
private static getActiveKernel;
|
|
58
|
-
|
|
96
|
+
private static assertRequiredEnvironmentVariable;
|
|
97
|
+
private static getEnvironmentVariablesPath;
|
|
98
|
+
private static parseBooleanEnvironmentVariable;
|
|
99
|
+
private static parseNumberEnvironmentVariable;
|
|
100
|
+
private static parseEnvironmentVariable;
|
|
101
|
+
private static validateEnvironmentVariables;
|
|
102
|
+
static loadEnvironmentVariables(environment?: string, options?: KernelEnvironmentVariablesOptions<KernelEnvironmentSchema | undefined>): DotenvConfigOutput;
|
|
103
|
+
constructor(options?: KernelOptions<TEnvironmentSchema>);
|
|
59
104
|
private closeCandidate;
|
|
60
105
|
private getConsumerFromClass;
|
|
61
106
|
private getInitializerFromClass;
|
|
@@ -64,11 +109,13 @@ declare class Kernel {
|
|
|
64
109
|
get consumers(): Consumer[];
|
|
65
110
|
get consumerMiddleware(): ConsumerMiddleware[];
|
|
66
111
|
get di(): DependencyInjection;
|
|
112
|
+
get environment(): KernelEnvironmentForSchema<TEnvironmentSchema>;
|
|
67
113
|
get logger(): KernelLogger;
|
|
68
114
|
get routes(): ServiceClass<Route>[];
|
|
69
115
|
get schedulers(): Scheduler[];
|
|
70
116
|
private getDependencyInjectionOptions;
|
|
71
117
|
dependencyInjection(options?: KernelDependencyInjectionOptions): Promise<void>;
|
|
118
|
+
loadEnvironmentVariables(environment?: string, options?: KernelEnvironmentVariablesOptions<KernelEnvironmentSchema | undefined>): DotenvConfigOutput;
|
|
72
119
|
getRoutes(): ServiceClass<Route>[];
|
|
73
120
|
registerConsumerMiddleware(...middlewares: ConsumerMiddleware[]): void;
|
|
74
121
|
registerConsumers(...ClassDefinitions: ServiceClass<Consumer>[]): void;
|
|
@@ -87,7 +134,7 @@ declare class Kernel {
|
|
|
87
134
|
runSchedulers(): Promise<void>;
|
|
88
135
|
shutdown(): Promise<void>;
|
|
89
136
|
}
|
|
90
|
-
declare function createKernel(options?: KernelOptions): Kernel
|
|
137
|
+
declare function createKernel<TEnvironmentSchema extends KernelEnvironmentSchema | undefined = undefined>(options?: KernelOptions<TEnvironmentSchema>): Kernel<TEnvironmentSchema>;
|
|
91
138
|
|
|
92
139
|
interface ConsumerExecutionContext {
|
|
93
140
|
readonly causationId?: string;
|
|
@@ -95,7 +142,7 @@ interface ConsumerExecutionContext {
|
|
|
95
142
|
readonly eventId: string;
|
|
96
143
|
readonly eventName: string;
|
|
97
144
|
readonly exchange: string;
|
|
98
|
-
readonly kernel: Kernel
|
|
145
|
+
readonly kernel: Kernel<KernelEnvironmentSchema | undefined>;
|
|
99
146
|
readonly metadata: Readonly<Record<string, unknown>>;
|
|
100
147
|
readonly rawMessage?: unknown;
|
|
101
148
|
readonly queueName: string;
|
|
@@ -109,4 +156,4 @@ interface ConsumerMiddleware {
|
|
|
109
156
|
|
|
110
157
|
type ShutdownHook = () => Promise<void> | void;
|
|
111
158
|
|
|
112
|
-
export { Consumer as C, Kernel as K, type ShutdownHook as S, type ConsumerExecutionContext as a, type ConsumerMiddleware as b, type ConsumerNext as c, type
|
|
159
|
+
export { Consumer as C, Kernel as K, type ShutdownHook as S, type ConsumerExecutionContext as a, type ConsumerMiddleware as b, type ConsumerNext as c, type KernelDefaultEnvironment as d, type KernelDependencyInjectionOptions as e, type KernelEnvironment as f, type KernelEnvironmentForSchema as g, type KernelEnvironmentSchema as h, type KernelEnvironmentValue as i, type KernelEnvironmentVariableDefinition as j, type KernelEnvironmentVariablePrimitive as k, type KernelEnvironmentVariableType as l, type KernelEnvironmentVariablesOptions as m, type KernelOptions as n, createKernel as o };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { D as DomainEvent } from './DomainEvent-mXWEtr_J.cjs';
|
|
2
|
+
import { DotenvConfigOutput } from 'dotenv';
|
|
2
3
|
import { D as DomainEventConsumer } from './DomainEventConsumer-Bg-bOwmh.cjs';
|
|
3
4
|
import { Route } from './adapters/ui/routes/index.cjs';
|
|
4
5
|
import { b as DependencyOverride, D as DependencyInjection, S as ServiceClass } from './ServiceClass-BkEHcXDi.cjs';
|
|
@@ -26,14 +27,49 @@ interface KernelDependencyInjectionOptions {
|
|
|
26
27
|
readonly sourceDirectory?: string;
|
|
27
28
|
}
|
|
28
29
|
|
|
29
|
-
|
|
30
|
+
type KernelDefaultEnvironment = NodeJS.ProcessEnv;
|
|
31
|
+
|
|
32
|
+
type KernelEnvironmentVariableType = 'boolean' | 'number' | 'string';
|
|
33
|
+
|
|
34
|
+
type KernelEnvironmentVariablePrimitive<TType extends KernelEnvironmentVariableType> = TType extends 'boolean' ? boolean : TType extends 'number' ? number : string;
|
|
35
|
+
|
|
36
|
+
interface KernelEnvironmentVariableDefinition<TType extends KernelEnvironmentVariableType = KernelEnvironmentVariableType, TRequired extends boolean = boolean> {
|
|
37
|
+
readonly defaultValue?: KernelEnvironmentVariablePrimitive<TType>;
|
|
38
|
+
readonly required?: TRequired;
|
|
39
|
+
readonly type: TType;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type KernelEnvironmentSchema = Readonly<Record<string, KernelEnvironmentVariableDefinition>>;
|
|
43
|
+
|
|
44
|
+
type KernelEnvironmentValue = boolean | number | string;
|
|
45
|
+
|
|
46
|
+
type KernelEnvironmentVariable<TDefinition extends KernelEnvironmentVariableDefinition> = TDefinition extends KernelEnvironmentVariableDefinition<infer TType extends KernelEnvironmentVariableType, infer TRequired extends boolean> ? TDefinition extends {
|
|
47
|
+
readonly defaultValue: unknown;
|
|
48
|
+
} ? KernelEnvironmentVariablePrimitive<TType> : TRequired extends true ? KernelEnvironmentVariablePrimitive<TType> : KernelEnvironmentVariablePrimitive<TType> | undefined : never;
|
|
49
|
+
|
|
50
|
+
type KernelEnvironment<TSchema extends KernelEnvironmentSchema> = {
|
|
51
|
+
readonly [key: string]: KernelEnvironmentValue | undefined;
|
|
52
|
+
} & {
|
|
53
|
+
readonly [TKey in keyof TSchema]: KernelEnvironmentVariable<TSchema[TKey]>;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
type KernelEnvironmentForSchema<TSchema extends KernelEnvironmentSchema | undefined> = TSchema extends KernelEnvironmentSchema ? KernelEnvironment<TSchema> : KernelDefaultEnvironment;
|
|
57
|
+
|
|
58
|
+
interface KernelEnvironmentVariablesOptions<TSchema extends KernelEnvironmentSchema | undefined = undefined> {
|
|
59
|
+
readonly override?: boolean;
|
|
60
|
+
readonly path?: string;
|
|
61
|
+
readonly schema?: TSchema;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface KernelOptions<TEnvironmentSchema extends KernelEnvironmentSchema | undefined = undefined> {
|
|
30
65
|
readonly di?: DependencyInjection;
|
|
66
|
+
readonly environmentSchema?: TEnvironmentSchema;
|
|
31
67
|
readonly logger?: KernelLogger;
|
|
32
68
|
readonly servicesYamlPath?: string;
|
|
33
69
|
readonly sourceDirectory?: string;
|
|
34
70
|
}
|
|
35
71
|
|
|
36
|
-
declare class Kernel {
|
|
72
|
+
declare class Kernel<TEnvironmentSchema extends KernelEnvironmentSchema | undefined = undefined> {
|
|
37
73
|
private readonly options;
|
|
38
74
|
private static readonly stateKey;
|
|
39
75
|
private readonly consumerMiddlewares;
|
|
@@ -43,19 +79,28 @@ declare class Kernel {
|
|
|
43
79
|
private readonly schedulersList;
|
|
44
80
|
private readonly shutdownHooks;
|
|
45
81
|
private dependencyInjectionInstance;
|
|
82
|
+
private environmentVariables;
|
|
46
83
|
private static get state();
|
|
47
84
|
static get configDirectory(): string;
|
|
48
85
|
static get consumers(): Consumer[];
|
|
49
86
|
static get consumerMiddleware(): ConsumerMiddleware[];
|
|
50
87
|
static get di(): DependencyInjection;
|
|
88
|
+
static get environment(): NodeJS.ProcessEnv;
|
|
51
89
|
static get logger(): KernelLogger;
|
|
52
|
-
static get active(): Kernel
|
|
90
|
+
static get active(): Kernel<KernelEnvironmentSchema | undefined>;
|
|
53
91
|
static get rootDirectory(): string;
|
|
54
92
|
static get routes(): ServiceClass<Route>[];
|
|
55
93
|
static get schedulers(): Scheduler[];
|
|
56
94
|
static get sourceDirectory(): string;
|
|
57
95
|
private static getActiveKernel;
|
|
58
|
-
|
|
96
|
+
private static assertRequiredEnvironmentVariable;
|
|
97
|
+
private static getEnvironmentVariablesPath;
|
|
98
|
+
private static parseBooleanEnvironmentVariable;
|
|
99
|
+
private static parseNumberEnvironmentVariable;
|
|
100
|
+
private static parseEnvironmentVariable;
|
|
101
|
+
private static validateEnvironmentVariables;
|
|
102
|
+
static loadEnvironmentVariables(environment?: string, options?: KernelEnvironmentVariablesOptions<KernelEnvironmentSchema | undefined>): DotenvConfigOutput;
|
|
103
|
+
constructor(options?: KernelOptions<TEnvironmentSchema>);
|
|
59
104
|
private closeCandidate;
|
|
60
105
|
private getConsumerFromClass;
|
|
61
106
|
private getInitializerFromClass;
|
|
@@ -64,11 +109,13 @@ declare class Kernel {
|
|
|
64
109
|
get consumers(): Consumer[];
|
|
65
110
|
get consumerMiddleware(): ConsumerMiddleware[];
|
|
66
111
|
get di(): DependencyInjection;
|
|
112
|
+
get environment(): KernelEnvironmentForSchema<TEnvironmentSchema>;
|
|
67
113
|
get logger(): KernelLogger;
|
|
68
114
|
get routes(): ServiceClass<Route>[];
|
|
69
115
|
get schedulers(): Scheduler[];
|
|
70
116
|
private getDependencyInjectionOptions;
|
|
71
117
|
dependencyInjection(options?: KernelDependencyInjectionOptions): Promise<void>;
|
|
118
|
+
loadEnvironmentVariables(environment?: string, options?: KernelEnvironmentVariablesOptions<KernelEnvironmentSchema | undefined>): DotenvConfigOutput;
|
|
72
119
|
getRoutes(): ServiceClass<Route>[];
|
|
73
120
|
registerConsumerMiddleware(...middlewares: ConsumerMiddleware[]): void;
|
|
74
121
|
registerConsumers(...ClassDefinitions: ServiceClass<Consumer>[]): void;
|
|
@@ -87,7 +134,7 @@ declare class Kernel {
|
|
|
87
134
|
runSchedulers(): Promise<void>;
|
|
88
135
|
shutdown(): Promise<void>;
|
|
89
136
|
}
|
|
90
|
-
declare function createKernel(options?: KernelOptions): Kernel
|
|
137
|
+
declare function createKernel<TEnvironmentSchema extends KernelEnvironmentSchema | undefined = undefined>(options?: KernelOptions<TEnvironmentSchema>): Kernel<TEnvironmentSchema>;
|
|
91
138
|
|
|
92
139
|
interface ConsumerExecutionContext {
|
|
93
140
|
readonly causationId?: string;
|
|
@@ -95,7 +142,7 @@ interface ConsumerExecutionContext {
|
|
|
95
142
|
readonly eventId: string;
|
|
96
143
|
readonly eventName: string;
|
|
97
144
|
readonly exchange: string;
|
|
98
|
-
readonly kernel: Kernel
|
|
145
|
+
readonly kernel: Kernel<KernelEnvironmentSchema | undefined>;
|
|
99
146
|
readonly metadata: Readonly<Record<string, unknown>>;
|
|
100
147
|
readonly rawMessage?: unknown;
|
|
101
148
|
readonly queueName: string;
|
|
@@ -109,4 +156,4 @@ interface ConsumerMiddleware {
|
|
|
109
156
|
|
|
110
157
|
type ShutdownHook = () => Promise<void> | void;
|
|
111
158
|
|
|
112
|
-
export { Consumer as C, Kernel as K, type ShutdownHook as S, type ConsumerExecutionContext as a, type ConsumerMiddleware as b, type ConsumerNext as c, type
|
|
159
|
+
export { Consumer as C, Kernel as K, type ShutdownHook as S, type ConsumerExecutionContext as a, type ConsumerMiddleware as b, type ConsumerNext as c, type KernelDefaultEnvironment as d, type KernelDependencyInjectionOptions as e, type KernelEnvironment as f, type KernelEnvironmentForSchema as g, type KernelEnvironmentSchema as h, type KernelEnvironmentValue as i, type KernelEnvironmentVariableDefinition as j, type KernelEnvironmentVariablePrimitive as k, type KernelEnvironmentVariableType as l, type KernelEnvironmentVariablesOptions as m, type KernelOptions as n, createKernel as o };
|
package/dist/adapters/index.cjs
CHANGED
|
@@ -117,6 +117,7 @@ var ConsoleKernelLogger = class {
|
|
|
117
117
|
};
|
|
118
118
|
|
|
119
119
|
// src/Kernel.ts
|
|
120
|
+
var import_dotenv = __toESM(require("dotenv"), 1);
|
|
120
121
|
var import_node_path2 = __toESM(require("path"), 1);
|
|
121
122
|
|
|
122
123
|
// src/infrastructure/dependency-injection/DependencyInjection.ts
|
|
@@ -364,6 +365,14 @@ var DependencyInjection = class _DependencyInjection {
|
|
|
364
365
|
}
|
|
365
366
|
};
|
|
366
367
|
|
|
368
|
+
// src/kernel/KernelEnvironmentValidationError.ts
|
|
369
|
+
var KernelEnvironmentValidationError = class extends Error {
|
|
370
|
+
constructor(message) {
|
|
371
|
+
super(message);
|
|
372
|
+
this.name = "KernelEnvironmentValidationError";
|
|
373
|
+
}
|
|
374
|
+
};
|
|
375
|
+
|
|
367
376
|
// src/Kernel.ts
|
|
368
377
|
var Kernel = class _Kernel {
|
|
369
378
|
constructor(options = {}) {
|
|
@@ -383,6 +392,7 @@ var Kernel = class _Kernel {
|
|
|
383
392
|
schedulersList = [];
|
|
384
393
|
shutdownHooks = [];
|
|
385
394
|
dependencyInjectionInstance;
|
|
395
|
+
environmentVariables = process.env;
|
|
386
396
|
static get state() {
|
|
387
397
|
const stateContainer = globalThis;
|
|
388
398
|
stateContainer[_Kernel.stateKey] = stateContainer[_Kernel.stateKey] ?? {};
|
|
@@ -400,6 +410,9 @@ var Kernel = class _Kernel {
|
|
|
400
410
|
static get di() {
|
|
401
411
|
return _Kernel.getActiveKernel().di;
|
|
402
412
|
}
|
|
413
|
+
static get environment() {
|
|
414
|
+
return process.env;
|
|
415
|
+
}
|
|
403
416
|
static get logger() {
|
|
404
417
|
return _Kernel.getActiveKernel().logger;
|
|
405
418
|
}
|
|
@@ -424,6 +437,83 @@ var Kernel = class _Kernel {
|
|
|
424
437
|
}
|
|
425
438
|
return _Kernel.state.activeKernel;
|
|
426
439
|
}
|
|
440
|
+
static assertRequiredEnvironmentVariable(name, value, schema) {
|
|
441
|
+
if (schema[name]?.required === true && value === void 0) {
|
|
442
|
+
throw new KernelEnvironmentValidationError(
|
|
443
|
+
`Missing required environment variable "${name}".`
|
|
444
|
+
);
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
static getEnvironmentVariablesPath(environment, options) {
|
|
448
|
+
return import_node_path2.default.resolve(
|
|
449
|
+
_Kernel.rootDirectory,
|
|
450
|
+
options.path ?? (environment ? `.env.${environment}` : ".env")
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
static parseBooleanEnvironmentVariable(name, value) {
|
|
454
|
+
if (["1", "true", "yes", "on"].includes(value.toLowerCase())) {
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
if (["0", "false", "no", "off"].includes(value.toLowerCase())) {
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
throw new KernelEnvironmentValidationError(
|
|
461
|
+
`Environment variable "${name}" must be a boolean.`
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
static parseNumberEnvironmentVariable(name, value) {
|
|
465
|
+
if (value.trim() === "") {
|
|
466
|
+
throw new KernelEnvironmentValidationError(
|
|
467
|
+
`Environment variable "${name}" must be a number.`
|
|
468
|
+
);
|
|
469
|
+
}
|
|
470
|
+
const parsedValue = Number(value);
|
|
471
|
+
if (Number.isFinite(parsedValue)) {
|
|
472
|
+
return parsedValue;
|
|
473
|
+
}
|
|
474
|
+
throw new KernelEnvironmentValidationError(
|
|
475
|
+
`Environment variable "${name}" must be a number.`
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
static parseEnvironmentVariable(name, value, schema) {
|
|
479
|
+
const definition = schema[name];
|
|
480
|
+
if (definition.type === "boolean") {
|
|
481
|
+
return _Kernel.parseBooleanEnvironmentVariable(name, value);
|
|
482
|
+
}
|
|
483
|
+
if (definition.type === "number") {
|
|
484
|
+
return _Kernel.parseNumberEnvironmentVariable(name, value);
|
|
485
|
+
}
|
|
486
|
+
return value;
|
|
487
|
+
}
|
|
488
|
+
static validateEnvironmentVariables(schema) {
|
|
489
|
+
const environmentVariables = {};
|
|
490
|
+
for (const [name, definition] of Object.entries(schema)) {
|
|
491
|
+
const value = process.env[name] ?? definition.defaultValue?.toString();
|
|
492
|
+
_Kernel.assertRequiredEnvironmentVariable(name, value, schema);
|
|
493
|
+
if (value !== void 0) {
|
|
494
|
+
environmentVariables[name] = _Kernel.parseEnvironmentVariable(
|
|
495
|
+
name,
|
|
496
|
+
value,
|
|
497
|
+
schema
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
return {
|
|
502
|
+
...process.env,
|
|
503
|
+
...environmentVariables
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
static loadEnvironmentVariables(environment, options = {}) {
|
|
507
|
+
const environmentName = environment ?? process.env.NODE_ENV ?? "local";
|
|
508
|
+
const result = import_dotenv.default.config({
|
|
509
|
+
override: options.override,
|
|
510
|
+
path: _Kernel.getEnvironmentVariablesPath(environmentName, options)
|
|
511
|
+
});
|
|
512
|
+
if (options.schema) {
|
|
513
|
+
_Kernel.validateEnvironmentVariables(options.schema);
|
|
514
|
+
}
|
|
515
|
+
return result;
|
|
516
|
+
}
|
|
427
517
|
async closeCandidate(candidate) {
|
|
428
518
|
if (candidate.shutdown) {
|
|
429
519
|
await candidate.shutdown();
|
|
@@ -465,6 +555,9 @@ var Kernel = class _Kernel {
|
|
|
465
555
|
}
|
|
466
556
|
return this.dependencyInjectionInstance;
|
|
467
557
|
}
|
|
558
|
+
get environment() {
|
|
559
|
+
return this.environmentVariables;
|
|
560
|
+
}
|
|
468
561
|
get logger() {
|
|
469
562
|
return this.loggerInstance;
|
|
470
563
|
}
|
|
@@ -490,6 +583,14 @@ var Kernel = class _Kernel {
|
|
|
490
583
|
await this.dependencyInjectionInstance.compile();
|
|
491
584
|
_Kernel.state.activeKernel = this;
|
|
492
585
|
}
|
|
586
|
+
loadEnvironmentVariables(environment, options = {}) {
|
|
587
|
+
const result = _Kernel.loadEnvironmentVariables(environment, {
|
|
588
|
+
...options,
|
|
589
|
+
schema: options.schema ?? this.options.environmentSchema
|
|
590
|
+
});
|
|
591
|
+
this.environmentVariables = this.options.environmentSchema ? _Kernel.validateEnvironmentVariables(this.options.environmentSchema) : process.env;
|
|
592
|
+
return result;
|
|
593
|
+
}
|
|
493
594
|
getRoutes() {
|
|
494
595
|
return this.routes;
|
|
495
596
|
}
|