项目作者: SarinaJs

项目描述 :
Key/value pair configuration
高级语言: TypeScript
项目地址: git://github.com/SarinaJs/sarina-configuration.git
创建时间: 2020-04-10T12:02:58Z
项目社区:https://github.com/SarinaJs/sarina-configuration

开源协议:MIT License

下载


Sarina-Configuration

build-and-test
npm version

Key/Value pair configuration library

The package is part of @Sarina framework

Installtion

Install by yarn

  1. yarn add @sarina/configuration

Quick Start

  1. import { ConfigurationBuilder, MemoryConfigurationSource, EnvironmentConfigurationSource } from '@sarina/configuration';
  2. const bootstrap = async () => {
  3. const provider = await new ConfigurationBuilder()
  4. .add(new MemoryConfigurationSource({ HOST: 'http://127.0.0.1', PORT: '3000' }))
  5. .add(new EnvironmentConfigurationSource())
  6. .build();
  7. const host = provider.getAsString('HOST');
  8. const port = provider.getAsString('PORT');
  9. };
  10. bootstrap()
  11. .then()
  12. .catch();

API

has()

To check if value exists or not use has method:

  1. const provider = await new ConfigurationBuilder()
  2. .add(new MemoryConfigurationSource({ HOST: 'http://127.0.0.1', PORT: '3000' }))
  3. .build();
  4. const isHostExists = provider.has("HOST");
  5. // true

has()

To check if value exists or not use has method:

  1. const provider = await new ConfigurationBuilder()
  2. .add(new MemoryConfigurationSource({ HOST: 'http://127.0.0.1', PORT: '3000' }))
  3. .build();
  4. const isHostExists = provider.has("HOST");
  5. // true

get(path: string)

To get configuration element use get method:

  1. const provider = await new ConfigurationBuilder()
  2. .add(new MemoryConfigurationSource({ HOST: 'http://127.0.0.1', PORT: '3000' }))
  3. .build();
  4. const config = provider.get("HOST");
  5. const host = config.value;

getScoped(path: string)

Library supports nested configuration. The getScoped method, will get a path and returns an instance of IConfiguration which contains all configuration methods scoped to the path:

  1. const provider = await new ConfigurationBuilder()
  2. .add(new MemoryConfigurationSource([
  3. { path:"host:url",value:"http://127.0.0.1" },
  4. { path:"host:port",value:"3000" }
  5. ]))
  6. .build();
  7. const hostConfig = provider.getScoped("host");
  8. const host = hostConfig.get("url").value;
  9. const port = hostConfig.get("port").value;

getAsString(path: string, defaultValue?: string)

In order to get a value of configuration, use getAsString:

  1. const provider = await new ConfigurationBuilder()
  2. .add(new MemoryConfigurationSource([
  3. { path:"host:url",value:"http://127.0.0.1" },
  4. { path:"host:port",value:"3000" }
  5. ]))
  6. .build();
  7. const url = provider.getAsString("host:url","http://localhost");
  8. // url = http://127.0.0.1

Custom Source

Sarina-Configuration allows developer to implement custom sources. To create your own source, you need to impement IConfigurationSource and implement load method

  1. class MySource implements IConfigurationSource {
  2. public async load(): Promise<ConfigurationElement[]> {
  3. return [
  4. {
  5. path: 'host',
  6. value: 'http://127.0.0.1',
  7. },
  8. ];
  9. }
  10. }
  11. const provider = await new ConfigurationBuilder()
  12. .add(new MySource())
  13. .build();

Sarina provides some built-in sources:

  • MemoryConfigurationSource provides in memory configuration as array or object.
  • EnvironmentConfigurationSource provides elememtns by using process.env.

How to contribute

Just fork the project, make your changes send us a PR.