Skip to content

Commit

Permalink
Merge pull request #3 from ukitgroup/docs-fixes
Browse files Browse the repository at this point in the history
Docs improvement
  • Loading branch information
Goodluckhf authored Jul 13, 2020
2 parents 758d803 + 0b656e1 commit aac26ec
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 63 deletions.
127 changes: 64 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# nestjs-config

![Travis](https://img.shields.io/travis/ukitgroup/nestjs-config/master.svg?style=flat-square)
[![Coverage Status](https://coveralls.io/repos/github/ukitgroup/nestjs-config/badge.svg?branch=master)](https://coveralls.io/github/ukitgroup/nestjs-config?branch=master) [![Greenkeeper badge](https://badges.greenkeeper.io/ukitgroup/nestjs-config.svg)](https://greenkeeper.io/)
![Coverage Status](https://coveralls.io/repos/github/ukitgroup/nestjs-config/badge.svg?branch=master)
![node](https://img.shields.io/node/v/@ukitgroup/nestjs-config.svg?style=flat-square)
![npm](https://img.shields.io/npm/v/@ukitgroup/nestjs-config.svg?style=flat-square)

Expand All @@ -10,13 +10,13 @@
![David](https://img.shields.io/david/ukitgroup/nestjs-config.svg?style=flat-square)
![David](https://img.shields.io/david/dev/ukitgroup/nestjs-config.svg?style=flat-square)

![license](https://img.shields.io/github/license/Goodluckhf/IoC-container.svg?style=flat-square)
![GitHub last commit](https://img.shields.io/github/last-commit/Goodluckhf/IoC-container.svg?style=flat-square)
![license](https://img.shields.io/github/license/ukitgroup/nestjs-config.svg?style=flat-square)
![GitHub last commit](https://img.shields.io/github/last-commit/ukitgroup/nestjs-config.svg?style=flat-square)
![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square)

## Description

### Convenient modular config for [`nest`](https://github.com/nestjs/nest) applications
### Convenient modular config for [`nestjs`](https://github.com/nestjs/nest) applications

- type casting (everything in `environment` is string) E.g. 'true' -> boolean ...
- runtime validation
Expand Down Expand Up @@ -56,7 +56,7 @@ export class CatConfig {
@Number()
readonly weight: number;

@Env('KNOW_PROGRAMMING')
@Env('KNOWS_PROGRAMMING')
@Boolean()
readonly knowsProgramming: boolean = true;
}
Expand Down Expand Up @@ -84,6 +84,65 @@ export class CatService {
}
```

## API

Define options for config in AppModule with:

```typescript
ConfigModule.forRoot(options: ConfigOptions)
```

```typescript
ConfigOptions: {
fromFile?: string,
configs?: ClassType[],
imports?: NestModule[],
providers?: Provider[],
}
```

If you don't set `fromFile` option, `process.env` will be used.

In addition you can provide logger to the library to log validation errors via token `CONFIG_LOGGER`.

So as raw object via token `RAW_CONFIG`. You might need it in your tests:

```typescript
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(RAW_CONFIG)
.useValue({
APP__HTTP_PORT: '3000',
CAT__WEIGHT: '5',
})
.compile();
```

Define configs in any module with:

```typescript
ConfigModule.forFeature(configs: ClassType[])
```

### Decorators

| Decorator | Description |
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| **Common config decorators** | |
| `@Config(name: string)` | Add prefix to env variables |
| `@Env(name: string)` | Extract env with `name` to this varaible |
| | |
| **Type decorators** | Import from `@ukitgroup/nestjs-config/types` |
| `@String()` | String variable (`@IsString`) |
| `@Number()` | Number variable (`parseFloat` + `@IsNumber` |
| `@Integer()` | Integer variable (`parseInt` + `@IsInteger` |
| `@Boolean()` | Boolean variable ('true','false' + @IsBool`) |
| `@Transform(transformFn: Function)` | Custom transformation. Import from `@ukitgroup/nestjs-config/transformer` |
| | |
| **Validation decorators** | The same as [`class-validator`](https://github.com/typestack/class-validator). Import from `@ukitgroup/nestjs-config/validator`. |


## Usage

**app.module.ts**
Expand Down Expand Up @@ -226,64 +285,6 @@ APP__HTTP_PORT=3000 CAT__NAME=vasya CAT__WEIGHT=5 node dist/main.js

Also you can see the example on github

## API

Define options for config in AppModule with:

```typescript
ConfigModule.forRoot(options: ConfigOptions)
```

```typescript
ConfigOptions: {
fromFile?: string,
configs?: ClassType[],
imports?: NestModule[],
providers: Provider[],
}
```

If you don't set `fromFile` option, `process.env` will be used.

In addition you can provide logger to the library to log validation errors via token `CONFIG_LOGGER`.

So as raw object via token `RAW_CONFIG`. You might need it in your tests:

```typescript
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(RAW_CONFIG)
.useValue({
APP__HTTP_PORT: '3000',
CAT__WEIGHT: '5',
})
.compile();
```

Define configs in any module with:

```typescript
ConfigModule.forFeature(configs: ClassType[])
```

### Decorators

| Decorator | Description |
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| **Common config decorators** | |
| `@Config(name: string)` | Add prefix to env variables |
| `@Env(name: string)` | Extract env with `name` to this varaible |
| | |
| **Type decorators** | Import from `@ukitgroup/nestjs-config/types` |
| `@String()` | String variable (`@IsString`) |
| `@Number()` | Number variable (`parseFloat` + `@IsNumber` |
| `@Integer()` | Integer variable (`parseInt` + `@IsInteger` |
| `@Boolean()` | Boolean variable ('true','false' + @IsBool`) |
| `@Transform(transformFn: Function)` | Custom transformation. Import from `@ukitgroup/nestjs-config/transformer` |
| | |
| **Validation decorators** | The same as [`class-validator`](https://github.com/typestack/class-validator). Import from `@ukitgroup/nestjs-config/validator`. |

## Transformation

You can either use our built-in types like Integer, Boolean, etc..
Expand Down
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@
"url": "https://github.com/ukitgroup/nestjs-config/issues"
},
"homepage": "https://github.com/ukitgroup/nestjs-config#readme",
"keywords": [
"nestjs",
"config",
"env",
"variables",
"type",
"validate",
"transform"
],
"publishConfig": {
"access": "public"
},
"lint-staged": {
"*.{js,ts}": [
"eslint --fix --ignore-path .gitignore",
Expand Down Expand Up @@ -111,6 +123,10 @@
{
"type": "chore",
"release": "patch"
},
{
"type": "docs",
"release": "patch"
}
]
}
Expand Down

0 comments on commit aac26ec

Please sign in to comment.