1.0.55 • Published 3 years ago

code-config v1.0.55

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

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
1.0.55

3 years ago

1.0.44

3 years ago

1.0.43

3 years ago

1.0.48

3 years ago

1.0.47

3 years ago

1.0.46

3 years ago

1.0.45

3 years ago

1.0.49

3 years ago

1.0.51

3 years ago

1.0.50

3 years ago

1.0.54

3 years ago

1.0.52

3 years ago

1.0.41

4 years ago

1.0.40

4 years ago

1.0.38

4 years ago

1.0.33

4 years ago

1.0.31

4 years ago

1.0.37

4 years ago

1.0.36

4 years ago

1.0.35

4 years ago

1.0.34

4 years ago

1.0.27

4 years ago

1.0.30

4 years ago

1.0.26

4 years ago

1.0.25

4 years ago

1.0.24

4 years ago

1.0.23

4 years ago

1.0.22

4 years ago

1.0.21

4 years ago

1.0.20

4 years ago

1.0.19

4 years ago

1.0.18

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.14

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago