centig
Advanced tools
Comparing version 1.0.6 to 1.0.8
{ | ||
"name": "centig", | ||
"version": "1.0.6", | ||
"description": "Configuration library for your node.js project", | ||
"main": "lib/config.js", | ||
"types": "lib/config.d.ts", | ||
"version": "1.0.8", | ||
"description": "The Configuration Management library for your JavaScript application", | ||
"main": "lib/centig.js", | ||
"types": "lib/centig.d.ts", | ||
"scripts": { | ||
"test": "jest --config jestconfig.json", | ||
"dev": "nodemon --watch src/**/*.ts --ignore src/**/*.test.ts --exec ts-node src/dev.ts", | ||
"build": "tsc", | ||
"build:watch": "tsc --watch", | ||
"test": "jest --watch --config jest.config.js", | ||
"pipeline:ci": "npm run lint && npm run coverage && npm run build", | ||
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"", | ||
"lint": "tslint -p tsconfig.json", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm test && npm run lint", | ||
"preversion": "npm run lint", | ||
"version": "npm run format && git add -A src", | ||
"postversion": "git push origin master && git push --tags" | ||
"postversion": "git push origin master && git push --tags", | ||
"coverage": "jest --config jest.config.js --verbose --coverage", | ||
"coverage:upload": "codecov" | ||
}, | ||
@@ -28,3 +29,2 @@ "repository": { | ||
"config", | ||
"node", | ||
"nodejs", | ||
@@ -35,2 +35,3 @@ "configuration", | ||
"javascript", | ||
"js", | ||
"env", | ||
@@ -48,5 +49,9 @@ "simple", | ||
"@types/jest": "^24.0.20", | ||
"@types/node": "^12.11.7", | ||
"codecov": "^3.6.1", | ||
"jest": "^24.9.0", | ||
"nodemon": "^1.19.4", | ||
"prettier": "^1.18.2", | ||
"ts-jest": "^24.1.0", | ||
"ts-node": "^8.4.1", | ||
"tslint": "^5.20.0", | ||
@@ -53,0 +58,0 @@ "tslint-config-prettier": "^1.18.0", |
198
README.md
# Centig | ||
Configuration library for your node js project | ||
[![NPM version](https://img.shields.io/npm/v/centig.svg)](https://www.npmjs.org/package/centig) | ||
[![Build Status](https://travis-ci.org/Alexloof/centig.svg?branch=master)](https://travis-ci.org/Alexloof/centig) | ||
[![codecov](https://codecov.io/gh/Alexloof/centig/branch/master/graph/badge.svg)](https://codecov.io/gh/Alexloof/centig) | ||
> The Configuration Management library for your JavaScript application | ||
## Introduction | ||
The goal with Centig is to give the developer the opportunity to include all configuration in one single file with a logical hierarchical structure. The Centig configuration schema API aims to be very distinct with a simple overview of the context and the origin of each config value. Therefore, Centig does not believe in overriding and extending configurations. Each value can be configured very differently, depending on how important or dynamic the value can be. Centig has a validation-first approach (by default) where it should **NOT** be possible to launch the application without having all the values present that were defined in the schema. | ||
## Install | ||
```shell | ||
npm install centig | ||
``` | ||
## Usage | ||
Here is an example of a config file with some static and some configs coming from environment variables. | ||
```javascript | ||
const centig = require('centig'); | ||
const config = centig({ | ||
db: { | ||
host: 'localhost', | ||
port: 5050, | ||
name: 'admin', | ||
}, | ||
api: { | ||
url: { | ||
type: String, | ||
env: 'API_URL', | ||
validate: value => new URL(value), | ||
}, | ||
key: { | ||
type: Number, | ||
env: 'API_KEY', | ||
preprocess: value => Number(value), | ||
}, | ||
}, | ||
newFeature: { | ||
isSupported: { | ||
type: Boolean, | ||
env: 'NEW_FEATURE_SUPPORT', | ||
preprocess: value => Boolean(Number(value)), | ||
}, | ||
regex: { | ||
type: RegExp, | ||
value: /\babc\b/, | ||
}, | ||
}, | ||
logLevel: { | ||
type: String, | ||
env: 'LOG_LEVEL', | ||
validate: value => { | ||
const logLevels = ['debug', 'trace', 'info', 'warn', 'error', 'fatal']; | ||
if (!logLevels.includes(value)) { | ||
throw Error( | ||
`The value - ${value} for logLevel must be one of ${logLevels}`, | ||
); | ||
} | ||
}, | ||
}, | ||
}); | ||
module.exports = config; | ||
``` | ||
Then we can in any other file import and use the config module as seen below. | ||
```javascript | ||
const config = require('./config'); | ||
const apiUrl = config.get('api').url; | ||
console.log(`This is the api url: ${apiUrl}`); | ||
const isNewFeatureSupported = config.get('newFeature.isSupported'); | ||
console.log(`This is the newFeature isSupported: ${isNewFeatureSupported}`); | ||
``` | ||
## API | ||
### const config = centig(schema) | ||
The configuration schema passed into the Centig module can be configured very differently depending on the needs. Eather we use the shortcut alternative or we pass a Centig specific configuration object. The following properties can be used: | ||
| Properties | Type | Required | Description | | ||
| ---------- | --------------------- | --------------- | ----------------------------------------------------------------------------------------------------------------------- | | ||
| type | constructor | ✘ | Simple type validation. Supported are: String, Number, Boolean, Array, Object, RegExp. | | ||
| env | string | ✓ (if no value) | This value is grabbed from **process.env**. | | ||
| value | any | ✓ (if no env) | If the config value is not from process.env we can use this property. But maybe the shortcut alternative fits better. | | ||
| preprocess | (value: any) => value | ✘ | A custom function if we wanna process the value. Useful if we wanna perform any conversion before the validation. | | ||
| validate | (value: any) => void | ✘ | A custom function if we wanna perform extra validation. Remember to throw Error if the value is not validating. | | ||
| optional | boolean | ✘ | Defaults to false. We can flip this to true if we don't wanna require a value to be present. No validation will be made | | ||
Example: | ||
```javascript | ||
const centig = require('centig'); | ||
const config = centig({ | ||
api: { | ||
url: { | ||
type: String, | ||
env: 'API_URL', // Trying to grab process.env.API_URL | ||
validate: value => new URL(value), | ||
}, | ||
key: { | ||
type: Number, | ||
env: 'API_KEY', // Trying to grab process.env.API_KEY | ||
preprocess: value => Number(value), | ||
}, | ||
}, | ||
}); | ||
``` | ||
To use the shortcut method we simply define key-value pairs, where the value could be whatever we want, such as a number or a string. This may be a good choice if no validation or processing is needed. See the example below. | ||
```javascript | ||
const centig = require('centig'); | ||
const config = centig({ | ||
db: { | ||
host: 'localhost', | ||
port: 5050, | ||
name: 'admin', | ||
}, | ||
api: { | ||
url: 'https://api.url.com', | ||
key: 'api-key', | ||
}, | ||
}); | ||
``` | ||
### config.get(key) | ||
Returns the value by key name. | ||
```javascript | ||
config.get('api').url; | ||
// or | ||
config.get('api.url'); | ||
``` | ||
### config.all() | ||
Returns all configurations | ||
## Typescript | ||
Centig comes with types and to take advantage of this we can define an interface for our schema and pass it when we call `centig<Interface>(schema)`. | ||
```typescript | ||
import centig from 'centig'; | ||
interface Schema { | ||
db: { | ||
host: string; | ||
port: number; | ||
name: string; | ||
}; | ||
api: { | ||
url: string; | ||
key: number; | ||
}; | ||
} | ||
const config = centig<Schema>({ | ||
db: { | ||
host: 'localhost', | ||
port: 5050, | ||
name: 'admin', | ||
}, | ||
api: { | ||
url: { | ||
type: String, | ||
env: 'API_URL', // Trying to grab process.env.API_URL | ||
validate: value => new URL(value), | ||
}, | ||
key: { | ||
type: Number, | ||
env: 'API_KEY', // Trying to grab process.env.API_KEY | ||
preprocess: value => Number(value), | ||
}, | ||
}, | ||
}); | ||
config.get('api').url; // is now fully typed | ||
``` | ||
Calling .get() with a dotted path is not yet supported. | ||
## Inspiration | ||
- [mozilla/node-convict](https://github.com/mozilla/node-convict) Featureful configuration management library for Node.js. | ||
- [lorenwest/node-config](https://github.com/lorenwest/node-config) Node.js Application Configuration. |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
22712
15
206
200
11
913
2