Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@unifig/core

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@unifig/core

Universal, typed and validated configuration manager

  • 0.3.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
941
decreased by-54.72%
Maintainers
1
Weekly downloads
 
Created
Source

Unifig

Universal, typed and validated configuration manager.

MIT Licensed NPM version Commitizen friendly codecov Build Status

Table of contents

💡 Goal

Unifig aims to provides simple and abstract way of handling app's configuration. It allows to load configuration data from multiple sources without changing defined config template. Many templates an be defined to further organize the code eg. MainConfiguration and ModuleConfiguration.

Adapted configuration data is transformed into templates and validated via class-transformer and class-validator.

Installation

npm i @unifig/core
# or
yarn add @unifig/core

Quick Start

import { From, Nested } from '@unifig/core';
import { Transform } from 'class-transformer';
import { IsString, IsArray } from 'class-validator';

class DbSettings {
  @From('global.dbUrl')
  @IsString()
  url: string;

  @From('global.dbPassword')
  @IsString()
  password: string;
}

export class Settings {
  @From('local.port')
  @IsInt()
  port: number;

  @Transform(({ value }) => value.split(',').map((n) => Number(n)))
  @IsArray()
  intervals: number[];

  @Nested(DbSettings)
  db: DbSettings;
}
import { Config, PlainConfigAdapter } from '@unifig/core';

async function bootstrap() {
  await Config.register({
    template: Settings,
    adapter: new PlainConfigAdapter({
      local: { port: 3000 },
      global: { dbUrl: 'localhost:5467', dbPassword: 'password' },
      intervals: '56,98,34,72',
    }),
  });

  console.log(Config.values(Settings).port); // output: 3000
}

bootstrap();

Above example uses built-in adapter which transforms static object into Settings. See full list of adapters here.

Multiple Configurations

async function bootstrap() {
  await Config.register(
    { template: Settings, adapter: ... },
    { template: AnotherSettings, adapter: ... },
    { templates: [MoreAnotherSettings, YetMoreAnotherSettings], adapter: ... },
  );

  config.log(Config.values(Settings).someProperty)
  config.log(Config.values(AnotherSettings).someProperty)
  config.log(Config.values(MoreAnotherSettings).someProperty)
  config.log(Config.values(YetMoreAnotherSettings).someProperty)
}

Additional configs may be registered with separate .register() calls.

Stale Data

Upon changing application's configuration one must be usually restared to re-fetch new values. Unifig delivers an option to reload registered configurations in real time without app's restart.

await Config.getContainer(Settings).refresh();

License

This project is licensed under the MIT License - see the LICENSE file for details.

Keywords

FAQs

Package last updated on 22 Oct 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc