Home Reference Source Test Repository
import Config from 'trek/src/Config.js'
public class | source

Config

The app's configuration

Example:


 const config = new Config('./your-config-folder')

Constructor Summary

Public Constructor
public

constructor(root: String, separator: String)

Member Summary

Public Members
public

list: *

public

parsers: *

public

paths: *

public

root: *

public
public

stores: *

Method Summary

Public Methods
public

* compile(filename: String, namespace: String, env: String): nconf.Memory

Use templatly to render thie file, then parse file to Memory
public

dotenv(): void

Load dotenv `.env.{development|test|production}`
public

get(key: String, defaultValue: *): *

Get value by key from Config
public

* load(): void

Load dotenv and app config files
public

* loadList(): void

Load app.toml app.{development|test|production}.toml
public

parse(content: String, filename: String): nconf.Memory

Parse the file from `.js`, `.json`, `.toml`, `.yml` formatters.
public

* render(filename: String, locals: Object): String

First, uses native `template-strings` for rendering configuration file
public

set(key: String, value: *, type: String): Config

Assigns setting key to value

Public Constructors

public constructor(root: String, separator: String) source

Params:

NameTypeAttributeDescription
root String

The app's root path

separator String
  • optional
  • default: '.'

The keypath separator

Public Members

public list: * source

public parsers: * source

public paths: * source

public root: * source

public separator: * source

public stores: * source

Public Methods

public * compile(filename: String, namespace: String, env: String): nconf.Memory source

Use templatly to render thie file, then parse file to Memory

Params:

NameTypeAttributeDescription
filename String

The file path

namespace String

Set a namespace for current store

env String

Select env if you want

Return:

nconf.Memory

Example:


 yield config.compile('./your-config.json', 'user', 'test')

public dotenv(): void source

Load dotenv .env.{development|test|production}

Return:

void

Example:


 config.dotenv()

public get(key: String, defaultValue: *): * source

Get value by key from Config

Params:

NameTypeAttributeDescription
key String
defaultValue *
  • optional

Return:

*

Example:


 // search: env -> user -> global
 config.get('host')
 config.get('port', 80) // 80 by default

public * load(): void source

Load dotenv and app config files

Return:

void

Example:


 yield config.load()
 // load steps
 // 0. config.dotenv()
 // 1. yield config.loadList()

public * loadList(): void source

Load app.toml app.{development|test|production}.toml

Return:

void

Example:


 yield config.loadList()

public parse(content: String, filename: String): nconf.Memory source

Parse the file from .js, .json, .toml, .yml formatters.

Params:

NameTypeAttributeDescription
content String

The file raw content

filename String

The file path

Return:

nconf.Memory

Example:


 config.parse(content, filename)

public * render(filename: String, locals: Object): String source

First, uses native template-strings for rendering configuration file

Params:

NameTypeAttributeDescription
filename String

The config file path

locals Object

An object whose properties define local variables for the configuration

Return:

String

The rendered template strings

Example:


 yield config.render('./your-config.yml', { name: 'your-app-name' })

public set(key: String, value: *, type: String): Config source

Assigns setting key to value

Params:

NameTypeAttributeDescription
key String
value *
type String
  • optional
  • default: 'user'

The namespace is user by default

Return:

Config

this

Example:


 config.set('port', 8080)
 config.set('app.name', 'trek')