Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Nikaple/nest-typed-config
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.3.1
Choose a base ref
...
head repository: Nikaple/nest-typed-config
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.4.0
Choose a head ref
  • 10 commits
  • 11 files changed
  • 2 contributors

Commits on May 17, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    add3b4b View commit details
  2. chore: add pre-push hook

    Nikaple committed May 17, 2021
    Copy the full SHA
    215474d View commit details
  3. test: refactor app.close

    Nikaple committed May 17, 2021
    Copy the full SHA
    274dae3 View commit details
  4. Copy the full SHA
    104e78c View commit details
  5. chore: update lock file

    Nikaple committed May 17, 2021
    Copy the full SHA
    c2474a1 View commit details

Commits on May 19, 2021

  1. Copy the full SHA
    cfb8fd1 View commit details

Commits on Jun 1, 2021

  1. feat: expose getConfigErrorMessage

    This helps us to reuse the function when providing a custom validate parameter
    wagoid committed Jun 1, 2021
    Copy the full SHA
    631e550 View commit details
  2. Copy the full SHA
    9b14a74 View commit details

Commits on Jun 3, 2021

  1. Copy the full SHA
    41a72da View commit details
  2. chore(): release v1.4.0

    Nikaple committed Jun 3, 2021
    Copy the full SHA
    d11c007 View commit details
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run test:cov
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
# [1.4.0](https://github.com/Nikaple/nest-typed-config/compare/1.3.1...1.4.0) (2021-06-03)


### Bug Fixes

* improve typing for forRootAsync ([6a96e36](https://github.com/Nikaple/nest-typed-config/commit/6a96e36ebddfc5d5c9f38bc073990945195c6c6f))


### Features

* expose getConfigErrorMessage ([cdc2263](https://github.com/Nikaple/nest-typed-config/commit/cdc22635558d4f0c31b33d45024a590866298a9a))

## [1.3.1](https://github.com/Nikaple/nest-typed-config/compare/1.3.0...1.3.1) (2021-05-17)


27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -155,7 +155,7 @@ export class AppService {
}
```

For a full example, please visit our [examples](https://github1s.com/Nikaple/nest-typed-config/blob/main/examples/basic/src/app.module.ts) folder.
For a full example, please visit [CodeSandbox](https://codesandbox.io/s/affectionate-tdd-1juv6?file=/src/config.ts), or our [examples](https://github.com/Nikaple/nest-typed-config/blob/main/examples/basic/src/app.module.ts) folder.


## Using loaders
@@ -408,6 +408,29 @@ export class Config {
}
```

## Custom validate function

If the default `validate` function doesn't suite your use case, you can provide it like in the example below:

```ts
TypedConfigModule.forRoot({
schema: RootConfig,
validate: (rawConfig: any) => {
const config = plainToClass(RootConfig, rawConfig)
const schemaErrors = validateSync(config, {
forbidUnknownValues: true,
whitelist: true,
})

if (schemaErrors.length) {
throw new Error(TypedConfigModule.getConfigErrorMessage(schemaErrors))
}

return config as RootConfig
},
})
```

## Using config outside Nest's IoC container (Usage in decorators)

### Caution!
@@ -482,7 +505,7 @@ export class AppController {
}
```

For a full example, please visit our [examples](https://github1s.com/Nikaple/nest-typed-config/blob/main/examples/preload/src/app.module.ts) folder.
For a full example, please visit [CodeSandbox](https://codesandbox.io/s/restless-snowflake-l2eyx?file=/src/config.module.ts), or our [examples](https://github1s.com/Nikaple/nest-typed-config/blob/main/examples/preload/src/app.module.ts) folder.

## API

2 changes: 1 addition & 1 deletion lib/interfaces/typed-config-module-options.interface.ts
Original file line number Diff line number Diff line change
@@ -51,5 +51,5 @@ export interface TypedConfigModuleAsyncOptions
/**
* Function(s) to load configurations, can be synchronous or asynchronous.
*/
load: AsyncConfigLoader | AsyncConfigLoader[];
load: ConfigLoader | AsyncConfigLoader | (ConfigLoader | AsyncConfigLoader)[];
}
3 changes: 1 addition & 2 deletions lib/loader/file-loader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { cosmiconfigSync, OptionsSync } from 'cosmiconfig';
import { parse as parseToml } from '@iarna/toml';
import { Config } from 'cosmiconfig/dist/types';
import { basename, dirname } from 'path';
import { debug } from '../utils/debug.util';

@@ -67,7 +66,7 @@ const getSearchOptions = (options: FileLoaderOptions) => {
* @param options cosmiconfig initialize options. See: https://github.com/davidtheclark/cosmiconfig#cosmiconfigoptions
*/
export const fileLoader = (options: FileLoaderOptions = {}) => {
return (): Config => {
return (): Record<string, any> => {
const { searchPlaces, searchFrom } = getSearchOptions(options);
const loaders = {
'.toml': loadToml,
2 changes: 1 addition & 1 deletion lib/typed-config.module.ts
Original file line number Diff line number Diff line change
@@ -137,7 +137,7 @@ export class TypedConfigModule {
return config;
}

private static getConfigErrorMessage(errors: ValidationError[]) {
static getConfigErrorMessage(errors: ValidationError[]): string {
const messages = this.formatValidationError(errors)
.map(({ property, value, constraints }) => {
const constraintMessage = Object.entries(
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nest-typed-config",
"version": "1.3.1",
"version": "1.4.0",
"description": "Intuitive, type-safe configuration module for Nest framework",
"author": "Nikaple Zhou",
"license": "MIT",
@@ -16,9 +16,9 @@
"scripts": {
"prepare": "husky install",
"build": "rimraf -rf dist && tsc -p tsconfig.build.json",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test": "jest --runInBand",
"test:watch": "jest --runInBand --watch",
"test:cov": "jest --runInBand --coverage",
"doc": "typedoc lib/index.ts --tsconfig tsconfig.build.json",
"lint": "eslint {lib/**/*.ts,tests/**/*.ts,examples/**/*.ts} --fix",
"lint:dontfix": "eslint {lib/**/*.ts,test/**/*.ts,examples/**/*.ts}",
@@ -63,14 +63,14 @@
"eslint-plugin-import": "2.22.1",
"eslint-plugin-prettier": "^3.4.0",
"husky": "6.0.0",
"jest": "26.6.3",
"jest": "^27.0.0-next.9",
"lint-staged": "10.5.4",
"prettier": "2.2.1",
"reflect-metadata": "0.1.13",
"release-it": "14.6.1",
"rimraf": "3.0.2",
"rxjs": "6.6.7",
"ts-jest": "26.5.6",
"ts-jest": "^27.0.0-next.12",
"typedoc": "^0.20.36",
"typescript": "4.2.4"
},
4 changes: 1 addition & 3 deletions tests/e2e/no-config.spec.ts
Original file line number Diff line number Diff line change
@@ -20,8 +20,6 @@ describe('No config', () => {
});

afterEach(async () => {
if (app) {
await app.close();
}
await app?.close();
});
});
4 changes: 1 addition & 3 deletions tests/e2e/parsing-failed.spec.ts
Original file line number Diff line number Diff line change
@@ -34,8 +34,6 @@ describe('Parsing failed', () => {
});

afterEach(async () => {
if (app) {
await app.close();
}
await app?.close();
});
});
4 changes: 1 addition & 3 deletions tests/e2e/validation-failed.spec.ts
Original file line number Diff line number Diff line change
@@ -22,8 +22,6 @@ describe('Validation failed', () => {
});

afterEach(async () => {
if (app) {
await app.close();
}
await app?.close();
});
});
14 changes: 7 additions & 7 deletions tests/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -87,7 +87,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: Config,
load: fileLoader({
basename: '.config',
@@ -105,7 +105,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: Config,
load: fileLoader({
absolutePath: join(__dirname, '.env.error.toml'),
@@ -131,7 +131,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: Config,
load: fileLoader(),
}),
@@ -143,7 +143,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: Config,
load: fileLoader({
absolutePath: join(__dirname, '.env.invalid.toml'),
@@ -157,7 +157,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: Config,
load: fileLoader({
searchFrom: __dirname,
@@ -171,7 +171,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: TableConfig,
load: dotenvLoader(),
}),
@@ -183,7 +183,7 @@ export class AppModule {
return {
module: AppModule,
imports: [
TypedConfigModule.forRootAsync({
TypedConfigModule.forRoot({
schema: Config,
load: dotenvLoader(option),
validationOptions: {