https://github.com/artemovskiy/configuar
TypeScript config parser&validator&docs generator
https://github.com/artemovskiy/configuar
config nodejs typescript valid
Last synced: 4 months ago
JSON representation
TypeScript config parser&validator&docs generator
- Host: GitHub
- URL: https://github.com/artemovskiy/configuar
- Owner: artemovskiy
- Created: 2022-05-22T15:58:43.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2023-09-29T05:48:31.000Z (over 2 years ago)
- Last Synced: 2025-09-29T04:26:31.055Z (8 months ago)
- Topics: config, nodejs, typescript, valid
- Language: TypeScript
- Homepage:
- Size: 106 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Type-safe configuration loader for TypeScript
This package helps you:
* define you app config type using classes&decorators
* validate the config according to class property types
* read config values from environment and other sources like .env files
* automatically generate documentation about the app config
## How to use
1. Install
`yarn add configuar`
2. Define your application config
```typescript
import { EnvVariable } from 'configuar';
export default class AppConfig {
@EnvVariable()
port: number;
@EnvVariable()
dbUrl: string;
}
```
3. Define the environment using variables or `.env` file
```
// .env
PORT=3001
DB_URL=postgres://localhost:5234/postgres
```
4. Read the configuration
```typescript
const config = ConfigLoader.getConfig({ ctor: AppConfig });
/* AppConfig {
port: 3001,
dbUrl: `postgres://localhost:5234/postgres`
} */
```
`getConfig` throws a validation error, if there are missing variables or wrong value types.
5. Create config schema to share it with your colleagues
```shell
npx configuar get-schema ./dist/app-config.ts
```
You will get:
```json
{
"type": "object",
"properties": {
"PORT": {
"type": "number"
},
"DB_URL": {
"type": "string"
}
},
"required": ["PORT", "DB_URL"]
}
```
Put the schema to README.md to explain your colleagues which env variables are required to run the application.
This was just a basic example. You can [define nested config objects, use optional params](docs/config-class.md) and [use it within a Nestjs
application](docs/nest.md).
## Docs
- [Writing config class](docs/config-class.md)
- [Reference](docs/reference.md)
- [Usage with Nestjs](docs/nest.md)
## Further plans
1. Advanced validation: check value formats like Urls, integers etc
2. More decorator options
3. Flexible config schema generator: add human readable config docs generator