https://github.com/robinbuschmann/type-config
Type safe way defining configurations fed by environment variables, process arguments or json config files (including deserialization and validation)
https://github.com/robinbuschmann/type-config
config env json type-safety
Last synced: about 1 year ago
JSON representation
Type safe way defining configurations fed by environment variables, process arguments or json config files (including deserialization and validation)
- Host: GitHub
- URL: https://github.com/robinbuschmann/type-config
- Owner: RobinBuschmann
- License: mit
- Created: 2018-05-20T12:17:23.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-06-13T16:12:47.000Z (about 8 years ago)
- Last Synced: 2025-03-27T15:52:34.726Z (about 1 year ago)
- Topics: config, env, json, type-safety
- Language: TypeScript
- Homepage:
- Size: 151 KB
- Stars: 13
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://travis-ci.org/RobinBuschmann/type-config)
[](https://codecov.io/gh/RobinBuschmann/type-config)
# type-env
Type safe way defining configurations fed by environment variables, process arguments or json config files
(including deserialization and validation).
## Installation
```bash
npm install type-env --save
```
*type-env* requires [reflect-metadata](https://www.npmjs.com/package/reflect-metadata)
```
npm install reflect-metadata --save
```
Your `tsconfig.json` needs the following flags:
```json
{
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
```
## Getting started
#### Setup configuration class
```typescript
import {Config, EnvValue} from 'type-env';
@Config
class DataBaseConfiguration {
@EnvValue('DB_HOST') host: string = 'localhost'; // default value
@EnvValue('DB_NAME') name: string;
@EnvValue('DB_PORT') port: number;
}
```
```typescript
import {Config, ArgsValue} from 'type-env';
@Config
class LoggingConfiguration {
@ArgsValue('log-level') level: string;
@ArgsValue('silent') silent: boolean;
}
```
```json
{
"auth": {
"jwt": {
"issuer": "type-env"
},
"timestamp": "2018-05-27T17:35:54.391Z"
}
}
```
```typescript
import {Config, JsonConfiguration, JsonValue} from 'type-env';
@Config
@JsonConfiguration(`${__dirname}/config.json`)
class AuthConfiguration {
@JsonValue('auth.jwt.issuer') jwtIssuer: string = 'type-env';
@JsonValue('auth.timestamp') timestamp: Date;
}
```
#### Run application
```bash
DB_HOST='127.0.0.1' /
DB_NAME='type-env' /
DB_PORT='1234' /
node app.js --log-level info --silent
```
## Options
```typescript
import {buildDecorators, NodeEnvConfigSource, NodeArgsConfigSource, JsonConfigSource} from 'type-env';
const {Value, EnvValue, ArgsValue, JsonValue} = buildDecorators({
/**
* Enables validation if true. Throws if config value is invalid.
* @default true
*/
validate: true,
/**
* Throws if value does not exist on source.
* @default true
*/
required: true,
/**
* If true, loads config value when property is accessed.
* @default true
*/
lazyLoad: true,
/**
* Do not throw on validation or requirement errors, but logs a warning instead.
* @default false
*/
warnOnly: false,
/**
* Map of decorator key name and config source.
* @default {
Value: NodeEnvConfigSource,
EnvValue: NodeEnvConfigSource,
ArgsValue: NodeArgsConfigSource,
JsonValue: JsonConfigSource,
}
*/
decoratorMeta: {
Value: NodeEnvConfigSource,
EnvValue: NodeEnvConfigSource,
ArgsValue: NodeArgsConfigSource,
JsonValue: JsonConfigSource,
}
})
```