1.0.4 • Published 10 months ago

polyconfig v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

PolyConfig

A polyglot configuration library for node.js

Bored of writing the same configuration code over and over again? PolyConfig is here to help! you can use it to load configuration from various sources and mix them together with a priority order.

Deep dive

.env

API_SERVER_PORT=8080
API_SERVER_HEADERS_X-POWERED-BY=Env-Mummy

api.yaml

server:
  headers:
    x-powered-by: Yaml-Daddy
  ping_response: up

index.js

const poly = PolyConfig()
    .from('yaml', 1, { path: './api.yaml' }) // load from yaml file with priority of 1 (highest)
    .from('env', 2, { prefix: 'API' }) // load from environment variables with priority of 2 (lower)
    .require('server.port', Number) // set your first required var, require will throw if not found
    .optional('server.headers.x-powered-by', String, 'Default-Granny') // you can set optional vars with default values
    .optional('server.ping_response', (value: any) => (['up', 'down'].includes(value) ? value : 'down')) // and you can also set custom validator function ;)
let fullConfig = poly.get(); // access the full config object
let serverPort = poly.get('server.port'); // or to a specific value
let serverConfig = poly.get('server'); // or to a sub-object

console.log(fullConfig);
// {
//     server: {
//         port: 8080,
//         headers: {
//             'x-powered-by': 'Yaml-Daddy'
//         },
//         ping_response: 'up'
//     }
// }
1.0.4

10 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago