config changes to make it not async

This commit is contained in:
Boki 2025-06-20 20:05:05 -04:00
parent 24680e403d
commit 92d4b90987
5 changed files with 131 additions and 13 deletions

View file

@ -82,6 +82,51 @@ 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
*/