1.0.55 • Published 2 years ago

code-config v1.0.55

Weekly downloads
-
License
MIT
Repository
github
Last release
2 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

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.49

2 years ago

1.0.51

2 years ago

1.0.50

2 years ago

1.0.54

2 years ago

1.0.52

2 years ago

1.0.41

2 years ago

1.0.40

3 years ago

1.0.38

3 years ago

1.0.33

3 years ago

1.0.31

3 years ago

1.0.37

3 years ago

1.0.36

3 years ago

1.0.35

3 years ago

1.0.34

3 years ago

1.0.27

3 years ago

1.0.30

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.23

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago