@avanio/variable-util
    Preparing search index...

    Class ConfigMap<Data>

    ConfigMap

    type ConfigEnv = {
    PORT: number;
    HOST: string;
    DEBUG: boolean;
    URL: URL;
    };
    const env = new EnvConfigLoader();
    const config = new ConfigMap<ConfigEnv>(
    {
    DEBUG: {defaultValue: false, parser: booleanParser()},
    HOST: {defaultValue: 'localhost', parser: stringParser()},
    PORT: {defaultValue: 3000, parser: integerParser()},
    URL: {defaultValue: new URL('http://localhost:3000'), parser: new UrlParser({urlSanitize: true})},
    },
    [env],
    );
    console.log('port', await config.get('PORT'));

    v1.1.0

    Type Parameters

    • Data extends Record<string, unknown>

      type of config map

    Index
    • Ensure that the specified keys are loaded and cached in the ConfigMap. If any key fails to load, the error will be returned.

      Parameters

      • lookupKeys: keyof Data | Iterable<keyof Data, any, any>

        The key or iterable of keys to ensure are loaded.

      • OptionalencodeOptions: EncodeOptions

        Optional encode options to use when loading the keys.

      Returns Promise<IResult<void>>

      • A promise that resolves to an IResult indicating success or failure. If any key fails to load, the error will be returned.
    • get env value as string from config map

      Type Parameters

      • Key extends string | number | symbol = keyof Data

      Parameters

      • key: Key

        key of config map

      • OptionalencodeOptions: EncodeOptions

        optional encode options

      Returns Promise<undefined extends Data[Key] ? string | undefined : string>

      Promise of string value or undefined

      const port: string = await config.getString('PORT');
      
    • get env value as string from config map as Result

      Type Parameters

      • Key extends string | number | symbol = keyof Data

      Parameters

      • key: Key

        key of config map

      • OptionalencodeOptions: EncodeOptions

        optional encode options

      Returns Promise<IResult<undefined extends Data[Key] ? string | undefined : string>>

      Result Promise of string value or undefined

      const port: Result<string> = await config.getStringResult('PORT');
      if (port.isOk()) {
      console.log('port', port.ok());
      }
    • Read the cached configuration value for the given key. (use ensure to load/reload the value first)

      Type Parameters

      • K extends string | number | symbol

        The type of the key to look up in the configuration schema.

      Parameters

      • lookupKey: K

      Returns Data[K]

      • The cached configuration value.
      • If the key is not cached, indicating that ensure was not called first.
    • Read the cached configuration value as a string for the given key. (use ensure to load/reload the value first)

      Type Parameters

      • K extends string | number | symbol

        The type of the key to look up in the configuration schema.

      Parameters

      • lookupKey: K

        The key to look up in the configuration schema.

      Returns string

      • The cached string value.
      • If the key is not cached, indicating that ensure was not called first.
    • Read the cached configuration value as a string for the given key. (use ensure to load/reload the value first)

      Type Parameters

      • K extends string | number | symbol

        The type of the key to look up in the configuration schema.

      Parameters

      • lookupKey: K

        The key to look up in the configuration schema.

      Returns IResult<string, VariableLookupError>

      • The cached string value wrapped in a Result.