configuration helper
Find a file
2025-04-10 17:19:40 +07:00
.gitignore initial commit 2025-02-01 06:23:57 +07:00
index.js initial commit 2025-02-01 06:23:57 +07:00
package-lock.json initial commit 2025-02-01 06:23:57 +07:00
package.json initial commit 2025-02-01 06:23:57 +07:00
readme.md correct link display 2025-04-10 17:19:40 +07:00

confer

confer is a configuration helper. it wraps the dotenv library. application config is defined in a .env file, or in the shell environment. confer is used to filter, validate, transform, and display the configuration values.

usage

put your configuration in a .env file in the root of your project.

SERVICE=https/example.org/foo
USERNAME=bob
PASSWORD=supersecret
DATA=/tmp
ALLOW_LIST=sally, marge
DEBUG=true

call init_config() at the beginning of program execution. the parameter passed to init_config() contains rules for processing app configuration.

import { init_config, print_config } from "confer";

const config = init_config({
  required_keys: ["SERVICE", "USERNAME", "PASSWORD", "DATA"],
  optional_keys: ["ALLOW_LIST", "DEBUG"],
  hidden_keys: ["PASSWORD"],
  validate: (config) => {
    config.is_writable("DATA");
  },
  transform: (config) => {
    config.to_array("ALLOW_LIST");
    config.to_boolean("DEBUG");
  },
});

print_config(config);

print_config() will display the processed config values. keys in the hidden_keys array will be obfuscated. the validate and transform functions are passed the config object which can be manipulated directly, or validated and transformed with the attached helpers. is_readable(), is_writable(), to_array() and to_boolean().

SERVICE https/example.org/foo
USERNAME bob
PASSWORD ***********
DATA /tmp
ALLOW_LIST [ 'sally', 'marge' ]
DEBUG true