Configuration handler for deno with support for .env and scopes

Configuration handler for deno with support for .env and scopes

Cfg is a configuration handler for Deno with support for .env and scopes. Configuration handler for deno with support for .env and scopes

Cfg

Cfg is a configuration handler for Deno with support for .env and scopes.

Run

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

Import the latest release.

// As named import
import { cfg } from "https://deno.land/x/[email protected]/mod.ts";

// Or as default import
import cfg from "https://deno.land/x/[email protected]/mod.ts";

Basic usage

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"))

Usage

cfg() function

cfg is an overloaded function. It has the following signature:

cfg<Config extends Configuration, Env extends ENVConfiguration = {}>(arguments): Cfg<Config, Env>

Call without argument

Calling cfg() without arguments is used to access a configuration which has already been initiated on the default scope

Call with a scope

Calling cfg(scope) is used to access a configuration which has already been initiated on the specified scope

Call with a 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.

Call with a scope and a Loadable

Calling cfg(scope, [Loadable]) is used to create a configuration on the specified scope.

Call with an options object

Calling cfg(options) allow to specify additional options

Persistance & scopes

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:

  • When no scope is specified in the options, a default scope is used.
  • Scopes are shared between modules. It's therefore best for each module to have its own scope and leave the default scope for the final user.
  • Only one configuration per scope can be created. Once it's there, it can't be deleted

Options

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.

Methods

Cfg.get(...props: string[])

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.

Cfg.getp(path: string)

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(prop: string)

Cfg.getEnv() is similar to Cfg.get() except that it accepts only one parameter and returns environment variables.

Types

Configuration

The interface Configuration defines the configuration object.

interface Configuration {
  [key: string]: (string | number | boolean | Configuration) | (string | number | boolean | Configuration)[];
}

ENVConfiguration

The interface ENVConfiguration defines the .env object

interface ENVConfiguration {
  [key: string]: string | number | boolean;
}

Loadable

The type Loadable defines the sources of configuration that will be merged to a Configuration object.

type Loadable = string | Configuration | ConfigFunction;

ConfigFunction

The type ConfigFunction defines a function that returns a Configuration object.

type ConfigFunction<T extends ENVConfiguration = {}> = (env: T) => Configuration;

Contributions

PRs are welcome!

Download Details:

Author: ymonb1291

Source Code: https://github.com/ymonb1291/cfg

deno nodejs node javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

AEM Querybuilder for JavaScript (Browser, Node, Deno)

Helix Querybuilder .AEM Querybuilder for JavaScript (Browser, Node, Deno)

NKeys for JavaScript - Node.js, Browsers, and Deno

A public-key signature system based on Ed25519 for the NATS ecosystem system for JavaScript. The nkeys.js library works in Deno, Node.js, and the browser!

Javascript module for easily generating mazes,compatible with both Deno and Node

A work-in-progress Javascript maze generator module, compatible with both Deno and Node.

Deno Tutorial for Beginners: Deno vs. Node: Better Than NodeJS?

Deno is released, it’s time to find out if it can prove a worthy competition for Node.js. What is Deno? Why Deno can be considered an alternative to Node? The main differences between Deno and Node. Write a simple Deno app.

What is Deno? The Node.JS Killer? The Deno-minator to Node?

What is Deno? The Node.JS Killer? The Deno-minator to Node? Deno is the new hype and will it be better than node.js? Deno is a secure runtime for JavaScript and TypeScript that is based on the V8 JavaScript engine and the Rust programming language that addresses Node’s shortcomings