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

code-config

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

code-config

type friendly json configuration file management

  • 1.0.55
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
104
decreased by-43.17%
Maintainers
1
Weekly downloads
 
Created
Source

code-config

A type friendly JSON configuration file manager for NodeJS.

Why code-config?

code-config makes dealing with dynamic/static configuration files in TypeScript so much easier since it implements a system that allows you to define the JSON definition and it can also infer the types by the default value you pass through parameters.

Installation

Using yarn:

$ yarn add code-config

Using npm:

$ npm i code-config

Note: add --save if you are using npm < 5.0.0

Examples

Getting a JSON file from a path:

method init() will automatically create the file if it doesn't exists.

import { ConfigFactory } from 'code-config';

interface Definition {
  hello: string
}

export const config = ConfigFactory.getConfig<Definition>('path/to/config.json').init();

console.log(config.hello); // Should work perfectly.

console.log(config.test); // Should throw a type error.

Getting JSON file and place a default value if it doesn't exist:

import { ConfigFactory } from 'code-config';

interface Definition {
  hello: string
}

const defaultValue: Definition = {
  hello: 'World'
}

export const config = ConfigFactory.getConfig<Definition>('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World

Saving a JSON file:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World'
}

export const config = ConfigFactory.getConfig<Definition>('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World

config.hello = 'Test';

config.save();

console.log(config.hello); // Output: Test

Clear a JSON file:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World'
}

export const config = ConfigFactory.getConfig('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World

config.clear();

config.save();

console.log(config.hello); // Output: undefined

Getters:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World',
  test: {
    value: 'Result'
  }
}

export const config = ConfigFactory.getConfig('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: World
console.log(config.test.value); // Output: Result

console.log(config.get('hello')); // Output: World
console.log(config.get('test.value')); // Output: Result

Setters:

import { ConfigFactory } from 'code-config';

const defaultValue = {
  hello: 'World',
  test: {
    value: 'Result'
  }
}

export const config = ConfigFactory.getConfig('path/to/config.json', defaultValue).init();

console.log(config.hello); // Output: Hello

config.hello = 'Test';

console.log(config.hello); // Output: Test

config.set('hello', 'Set');

console.log(config.hello); // Output: Set

FAQs

Package last updated on 16 May 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