nest-typed-config
Advanced tools
Comparing version 2.2.3 to 2.3.0
import { DynamicModule } from '@nestjs/common'; | ||
import { ClassConstructor } from 'class-transformer'; | ||
export declare const selectConfig: <T>(module: DynamicModule, Config: ClassConstructor<T>) => T; | ||
export interface SelectConfigOptions { | ||
/** | ||
* when true, allow undefined config declared with `@IsOptional()` | ||
*/ | ||
allowOptional?: boolean; | ||
} | ||
export declare const selectConfig: <T, Option extends SelectConfigOptions>(module: DynamicModule, Config: ClassConstructor<T>, options?: Option | undefined) => Option extends { | ||
allowOptional: true; | ||
} ? T | undefined : T; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.selectConfig = void 0; | ||
const selectConfig = (module, Config) => { | ||
const selectConfig = (module, Config, options) => { | ||
const providers = module.providers; | ||
const selectedConfig = (providers || []).filter(({ provide }) => provide === Config)[0]; | ||
if (options === null || options === void 0 ? void 0 : options.allowOptional) { | ||
return selectedConfig === null || selectedConfig === void 0 ? void 0 : selectedConfig.useValue; | ||
} | ||
if (!selectedConfig) { | ||
throw new Error(`You can only select config which exists in providers`); | ||
throw new Error('You can only select config which exists in providers. \ | ||
If the config being selected is marked as optional, \ | ||
please pass `allowOptional` in options argument.'); | ||
} | ||
@@ -10,0 +15,0 @@ return selectedConfig.useValue; |
import { DynamicModule, ValueProvider } from '@nestjs/common'; | ||
import { ClassConstructor } from 'class-transformer'; | ||
export const selectConfig = <T>( | ||
export interface SelectConfigOptions { | ||
/** | ||
* when true, allow undefined config declared with `@IsOptional()` | ||
*/ | ||
allowOptional?: boolean; | ||
} | ||
export const selectConfig = <T, Option extends SelectConfigOptions>( | ||
module: DynamicModule, | ||
Config: ClassConstructor<T>, | ||
): T => { | ||
options?: Option, | ||
): Option extends { allowOptional: true } ? T | undefined : T => { | ||
const providers = module.providers as ValueProvider<T>[]; | ||
@@ -12,6 +20,13 @@ const selectedConfig = (providers || []).filter( | ||
)[0]; | ||
if (options?.allowOptional) { | ||
return selectedConfig?.useValue; | ||
} | ||
if (!selectedConfig) { | ||
throw new Error(`You can only select config which exists in providers`); | ||
throw new Error( | ||
'You can only select config which exists in providers. \ | ||
If the config being selected is marked as optional, \ | ||
please pass `allowOptional` in options argument.', | ||
); | ||
} | ||
return selectedConfig.useValue; | ||
}; |
{ | ||
"name": "nest-typed-config", | ||
"version": "2.2.3", | ||
"version": "2.3.0", | ||
"description": "Intuitive, type-safe configuration module for Nest framework", | ||
@@ -31,3 +31,3 @@ "author": "Nikaple Zhou", | ||
"lint": "eslint {lib/**/*.ts,tests/**/*.ts,examples/**/*.ts} --fix", | ||
"lint:dontfix": "eslint {lib/**/*.ts,test/**/*.ts,examples/**/*.ts}", | ||
"lint:dontfix": "eslint {lib/**/*.ts,tests/**/*.ts,examples/**/*.ts}", | ||
"format": "prettier --write .", | ||
@@ -49,12 +49,12 @@ "format:dontfix": "prettier --check .", | ||
"devDependencies": { | ||
"@commitlint/cli": "14.1.0", | ||
"@commitlint/cli": "15.0.0", | ||
"@iarna/toml": "2.2.5", | ||
"@latipun7/commitlintrc": "1.1.0", | ||
"@latipun7/commitlintrc": "1.1.3", | ||
"@latipun7/releaserc": "2.2.0", | ||
"@nestjs/axios": "0.0.3", | ||
"@nestjs/cli": "8.1.5", | ||
"@nestjs/common": "8.2.3", | ||
"@nestjs/core": "8.2.3", | ||
"@nestjs/platform-express": "8.2.3", | ||
"@nestjs/testing": "8.2.3", | ||
"@nestjs/cli": "8.1.6", | ||
"@nestjs/common": "8.2.4", | ||
"@nestjs/core": "8.2.4", | ||
"@nestjs/platform-express": "8.2.4", | ||
"@nestjs/testing": "8.2.4", | ||
"@types/debug": "4.1.7", | ||
@@ -66,5 +66,5 @@ "@types/express": "4.17.13", | ||
"@types/lodash.set": "4.3.6", | ||
"@types/node": "16.11.12", | ||
"@typescript-eslint/eslint-plugin": "5.6.0", | ||
"@typescript-eslint/parser": "5.6.0", | ||
"@types/node": "16.11.17", | ||
"@typescript-eslint/eslint-plugin": "5.8.0", | ||
"@typescript-eslint/parser": "5.8.0", | ||
"cosmiconfig": "7.0.1", | ||
@@ -78,4 +78,4 @@ "dotenv": "10.0.0", | ||
"husky": "7.0.4", | ||
"jest": "27.0.0-next.9", | ||
"lint-staged": "12.1.2", | ||
"jest": "27.4.5", | ||
"lint-staged": "12.1.4", | ||
"parse-json": "5.2.0", | ||
@@ -87,5 +87,5 @@ "prettier": "2.5.1", | ||
"semantic-release": "18.0.1", | ||
"ts-jest": "27.0.0-next.12", | ||
"ts-jest": "27.1.2", | ||
"typedoc": "0.22.10", | ||
"typescript": "4.5.2", | ||
"typescript": "4.5.4", | ||
"yaml": "1.10.2" | ||
@@ -92,0 +92,0 @@ }, |
@@ -494,3 +494,3 @@ <h1 align="center">Nest-Typed-Config</h1> | ||
## Type casting on environment variables | ||
## Transforming the raw configuration | ||
@@ -654,2 +654,4 @@ Environment variables are always loaded as strings, but configuration schemas are not. In such case, you can transform the raw config with `normalize` function: | ||
> If target configuration model is marked with `@Optional()`, you should call `selectConfig` with `{ allowOptional: true }` to allow optional configuration. | ||
```ts | ||
@@ -656,0 +658,0 @@ // app.controller.ts |
Sorry, the diff of this file is not supported yet
94910
1515
685