Cfg is a configuration handler for Deno with support for .env
and scopes
.
Some features require access to the file system and the environment. Please run your project with the flags --allow-read
, --allow-env
and --unstable
.
Import the latest release.
// As named import
import { cfg } from "https://deno.land/x/cfg@v1.0.0-beta.3/mod.ts";
// Or as default import
import cfg from "https://deno.land/x/cfg@v1.0.0-beta.3/mod.ts";
This is the easiest way to use Cfg.
const config = cfg([{ foo: "bar" }]);
console.log(config.get("foo"))
// Or
console.log(config.getp("foo"))
A type parameter can also be used in order to benefit from intellisense.
const obj = { foo: "bar" };
const config = cfg<typeof obj>([obj]);
console.log(config.get("foo"));
// Or
console.log(config.getp("foo"))
cfg
is an overloaded function. It has the following signature:
cfg<Config extends Configuration, Env extends ENVConfiguration = {}>(arguments): Cfg<Config, Env>
Calling cfg()
without arguments is used to access a configuration which has already been initiated on the default scope
Calling cfg(scope)
is used to access a configuration which has already been initiated on the specified scope
Loadable
Calling cfg([Loadable])
is used to create a configuration on the default scope. An array of Loadable
is passed as argument. Loadable
is the type that describes anything that can loaded to the configuration.
Loadable
Calling cfg(scope, [Loadable])
is used to create a configuration on the specified scope.
Calling cfg(options)
allow to specify additional options
All configurations are stored by Cfg and can be accessed at any time by calling cfg(scope)
. There are a few things to keep in mind:
The Opts
interface describes the option object that is used to configure cfg()
.
interface Opts {
env?: boolean | string | EnvOpt;
load?: Loadable | Loadable[];
scope?: string;
}
The Opts.scope
property defines the scope. The Opts.load
property is used to specify one or several Loadable
which will be merged to a Configuration
object. The Opts.env
property is used to configure the environment variables. When Opts.env
is true
, Cfg will look for an .env
file in the root directory. Opts.env
can also be a string that represents the path to the .env
file. More options can be configured by using an EnvOpt
object.
interface EnvOpt {
export?: boolean;
import?: ENVConfiguration;
merge?: boolean;
path?: string;
}
When true
, EnvOpt.export
exports the environment variables to Deno.env
. It will never overwrite a value that already exist. With EnvOpt.import
, an object of type ENVConfiguration
can be imported. This can be useful if, for example, one needs to use another parser for .env
files. When true, EnvOpt.merge
will merge the environment variables with the configuration. Finally, EnvOpt.path
is a string that that represents the path to the .env
file.
This method returns the configuration. Parameters can be used to return a specific property. When cfg()
is called with a type parameter, intellisense checks that the paramters are correct and knows the type of the value returned.
Cfg.get()
allows up to 4 parameters.
This method returns the configuration just like Cfg.get()
. But it accepts a path as parameter with the syntax a.b.c
or a[b][c]
. This method will always return a value with type unknown.
Cfg.getEnv()
is similar to Cfg.get()
except that it accepts only one parameter and returns environment variables.
The interface Configuration
defines the configuration object.
interface Configuration {
[key: string]: (string | number | boolean | Configuration) | (string | number | boolean | Configuration)[];
}
The interface ENVConfiguration
defines the .env
object
interface ENVConfiguration {
[key: string]: string | number | boolean;
}
The type Loadable
defines the sources of configuration that will be merged to a Configuration
object.
type Loadable = string | Configuration | ConfigFunction;
The type ConfigFunction
defines a function that returns a Configuration
object.
type ConfigFunction<T extends ENVConfiguration = {}> = (env: T) => Configuration;
PRs are welcome!
Author: ymonb1291
Source Code: https://github.com/ymonb1291/cfg
#deno #nodejs #node #javascript