made config async
This commit is contained in:
parent
92d4b90987
commit
caf1c5fcaf
6 changed files with 91 additions and 143 deletions
|
|
@ -33,9 +33,9 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Initialize the configuration by loading from all sources
|
||||
* Initialize the configuration by loading from all sources synchronously.
|
||||
*/
|
||||
async initialize(schema?: ConfigSchema): Promise<T> {
|
||||
initialize(schema?: ConfigSchema): T {
|
||||
if (this.config) {
|
||||
return this.config;
|
||||
}
|
||||
|
|
@ -48,7 +48,8 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
// Load configurations from all sources
|
||||
const configs: Record<string, unknown>[] = [];
|
||||
for (const loader of sortedLoaders) {
|
||||
const config = await loader.load();
|
||||
// Assuming all loaders now have a synchronous `load` method
|
||||
const config = loader.load();
|
||||
if (config && Object.keys(config).length > 0) {
|
||||
configs.push(config);
|
||||
}
|
||||
|
|
@ -82,51 +83,6 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the configuration synchronously (only env vars, no file loading)
|
||||
*/
|
||||
initializeSync(schema?: ConfigSchema): T {
|
||||
if (this.config) {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
this.schema = schema;
|
||||
|
||||
// Only use EnvLoader for sync initialization
|
||||
const envLoader = this.loaders.find(loader => loader.constructor.name === 'EnvLoader');
|
||||
if (!envLoader) {
|
||||
throw new ConfigError('No EnvLoader found for synchronous initialization');
|
||||
}
|
||||
|
||||
// Load env vars synchronously
|
||||
const envLoaderInstance = envLoader as any;
|
||||
const config = envLoaderInstance.loadSync ? envLoaderInstance.loadSync() : {};
|
||||
|
||||
// Add environment if not present
|
||||
if (typeof config === 'object' && config !== null && !('environment' in config)) {
|
||||
(config as Record<string, unknown>)['environment'] = this.environment;
|
||||
}
|
||||
|
||||
// Validate if schema provided
|
||||
if (this.schema) {
|
||||
try {
|
||||
this.config = this.schema.parse(config) as T;
|
||||
} catch (error) {
|
||||
if (error instanceof z.ZodError) {
|
||||
throw new ConfigValidationError(
|
||||
'Configuration validation failed',
|
||||
error.errors
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
} else {
|
||||
this.config = config as T;
|
||||
}
|
||||
|
||||
return this.config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current configuration
|
||||
*/
|
||||
|
|
@ -176,7 +132,7 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
throw new ConfigError('Configuration not initialized. Call initialize() first.');
|
||||
}
|
||||
|
||||
const updated = this.deepMerge(this.config as any, updates as any) as T;
|
||||
const updated = this.deepMerge(this.config as Record<string, unknown>, updates as Record<string, unknown>) as T;
|
||||
|
||||
// Re-validate if schema is present
|
||||
if (this.schema) {
|
||||
|
|
@ -240,8 +196,8 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
}
|
||||
}
|
||||
|
||||
private deepMerge(...objects: Record<string, any>[]): Record<string, any> {
|
||||
const result: Record<string, any> = {};
|
||||
private deepMerge(...objects: Record<string, unknown>[]): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {};
|
||||
|
||||
for (const obj of objects) {
|
||||
for (const [key, value] of Object.entries(obj)) {
|
||||
|
|
@ -253,7 +209,10 @@ export class ConfigManager<T = Record<string, unknown>> {
|
|||
!(value instanceof Date) &&
|
||||
!(value instanceof RegExp)
|
||||
) {
|
||||
result[key] = this.deepMerge(result[key] || {}, value);
|
||||
result[key] = this.deepMerge(
|
||||
(result[key] as Record<string, unknown>) || {} as Record<string, unknown>,
|
||||
value as Record<string, unknown>
|
||||
);
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue