config-manager.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Available configuration sources
  3. */
  4. export const ConfigSource = {
  5. env: 'env',
  6. db: 'db',
  7. } as const;
  8. export type ConfigSource = typeof ConfigSource[keyof typeof ConfigSource];
  9. /**
  10. * Metadata for a configuration value
  11. */
  12. export interface ConfigDefinition<T> {
  13. defaultValue: T;
  14. envVarName?: string;
  15. isSecret?: boolean;
  16. }
  17. /**
  18. * Helper function for defining configurations with type safety
  19. */
  20. export const defineConfig = <T>(config: ConfigDefinition<T>): ConfigDefinition<T> => config;
  21. /**
  22. * Interface for loading configuration values
  23. */
  24. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  25. export interface IConfigLoader<K extends string, V extends Record<K, any>> {
  26. /**
  27. * Load configurations from environment variables
  28. */
  29. loadFromEnv(): Promise<RawConfigData<K, V>>;
  30. /**
  31. * Load configurations from database
  32. */
  33. loadFromDB(): Promise<RawConfigData<K, V>>;
  34. }
  35. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  36. export type RawConfigData<K extends string, V extends Record<K, any>> = Record<K, {
  37. value: V[K];
  38. definition?: ConfigDefinition<V[K]>;
  39. }>;
  40. export type UpdateConfigOptions = { skipPubsub?: boolean };
  41. /**
  42. * Interface for managing configuration values
  43. */
  44. // eslint-disable-next-line @typescript-eslint/no-explicit-any
  45. export interface IConfigManager<K extends string, V extends Record<K, any>> {
  46. /**
  47. * Load configurations
  48. * @param options.source - Specify which source to load from
  49. */
  50. loadConfigs(options?: { source?: ConfigSource }): Promise<void>;
  51. /**
  52. * Get a configuration value
  53. */
  54. getConfig<T extends K>(key: T, source?: ConfigSource): V[T];
  55. /**
  56. * Update a configuration value
  57. */
  58. updateConfig<T extends K>(key: T, value: V[T], options?: UpdateConfigOptions): Promise<void>;
  59. /**
  60. * Update multiple configuration values
  61. */
  62. updateConfigs(updates: Partial<{ [T in K]: V[T] }>, options?: UpdateConfigOptions): Promise<void>;
  63. /**
  64. * Remove multiple configuration values
  65. */
  66. removeConfigs(keys: K[], options?: UpdateConfigOptions): Promise<void>;
  67. /**
  68. * Get environment variables managed with ConfigDefinitions
  69. */
  70. getManagedEnvVars(showSecretValues: boolean): Record<string, string>;
  71. }