New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@slimio/config

Package Overview
Dependencies
Maintainers
3
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@slimio/config

SlimIO Reactive JSON Config loaded

  • 0.4.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-71.43%
Maintainers
3
Weekly downloads
 
Created
Source

Config

Maintenance GitHub license

SlimIO - Reactive JSON Config loader

Features

  • Hot-reloading of configuration
  • Reactive with observable key(s)
  • Safe with JSON Schema validation

Getting Started

This package is available in the Node Package Repository and can be easily installed with npm or yarn.

$ npm i @slimio/config
# or
$ yarn add @slimio/config

Usage example

Create a simple json file for your project (As below)

{
    "loglevel": 5,
    "logsize": 4048,
    "login": "administrator"
}

Now create a new Configuration instance and read it

const Config = require("@slimio/config");

const cfg = new Config("./path/to/config.json");
cfg.read().then(() => {
    console.log(cfg.get("loglevel")); // stdout: 5
}).catch(console.error);

API

constructor(configFilePath: string, options?: Config.ConstructorOptions)

Create a new Configuration instance

const options = { autoReload: true };
const cfg = new Config("./path/to/file.json", options);

Available options are

interface ConstructorOptions {
    createOnNoEntry?: boolean;
    writeOnSet?: boolean;
    autoReload?: boolean;
    reloadDelay?: number;
    defaultSchema?: object;
}

read(defaultPayload?: T): Promise;

Will trigger and read the local configuration (on disk).

const cfg = new Config("./path/to/file.json");
assert.equal(cfg.configHasBeenRead, false); // true
await cfg.read();
assert.equal(cfg.configHasBeenRead, true); // true

Retriggering the method will made an hot-reload of all properties. For a cold reload you will have to close the configuration before.

setupAutoReload(): void;

Setup hot reload (with a file watcher). This method is automatically triggered if the Configuration has been created with the option autoReload set to true.

get(fieldPath: string): H

Get a value from a key (field path).

For example, image a json file with a foo field

const cfg = new Config("./path/to/file.json");
await cfg.read();
const fooValue = cfg.get("foo");

set(fieldPath: string, fieldValue: H): void;

Set a given field in the configuration

const cfg = new Config("./config.json", {
    createOnNoEntry: true
});

await cfg.read({ foo: "bar" });
cfg.set("foo", "hello world!");
await cfg.writeOnDisk();

observableOf(fieldPath: string): ObservableLike;

Observe a given configuration key with an Observable object!

const { writeFile } = require("fs").promises;
const cfg = new Config("./config.json", {
    autoReload: true,
    createOnNoEntry: true
});
await cfg.read({ foo: "bar" });

// Observe initial and next value(s) of foo
cfg.observableOf("foo").subscribe(console.log);

// Re-write local config file
const newPayload = { foo: "world" };
await writeFile("./config.json", JSON.stringify(newPayload, null, 4));

writeOnDisk(): Promise

Write the configuration on the disk

close(): Promise

Close (and write on disk) the configuration (it will close the watcher and clean all active observers).

Keywords

FAQs

Package last updated on 22 Sep 2018

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