Fake Mirrors
    Preparing search index...

    Class EnvConfig

    Env-based config implementation.

    Provides loading configuration data from environment variables.

    Depends:

    import { DIContainer } from '@famir/common'
    import { VALIDATOR, Validator, AjvValidator, JSONSchemaType } from '@famir/validator'
    import { CONFIG, Config, EnvConfig, ConfigData } from '@famir/config'

    // Get container singleton.
    const container = DIContainer.getInstance()

    // Register dependencies in container
    AjvValidator.register(container)
    EnvConfig.register(container)

    // Resolve dependencies from container
    const validator = container.resolve<Validator>(VALIDATOR)
    const config = container.resolve<Config>(CONFIG)

    // Define your configuration interface
    interface ServerConfig extends ConfigData {
    PORT: number
    SECRET: string
    }

    // Define your configuration schema
    const serverConfigSchema: JSONSchemaType<ServerConfig> = {
    type: 'object',
    required: ['PORT', 'SECRET'],
    properties: {
    PORT: { type: 'number', default: 3000 },
    SECRET: { type: 'string' }
    },
    additionalProperties: false,
    } as const

    // Add schema to validator
    validator.addSchema('server-config', serverConfigSchema)

    // Get parsed and validated configuration object
    const configData = config.get<ServerConfig>('server-config')

    // TypeScript knows this is ServerConfig
    console.log(configData.PORT)

    Implements

    • Retrieves and validates a configuration object.

      The configuration data is loaded from the implementation's source and validated against a pre-registered JSON Schema. The result is cached to avoid redundant processing.

      Type Parameters

      • T extends ConfigData

        The expected type of the configuration object.

      Parameters

      • schemaName: string

        The name of the schema registered in the validator.

      Returns T

      The validated and typed configuration object.

      BootstrapError If loading or validation of the configuration fails.

    • Registers the config as a singleton in the DI container.

      Parameters

      • container: DIContainer

        The DI container to register in.

      Returns void