nestjs-any-config
Advanced tools
Comparing version 0.8.1 to 0.9.0
@@ -8,2 +8,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var ConfigModule_1; | ||
@@ -23,14 +32,2 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
static forRoot(options) { | ||
let config = this.loadFile(options); | ||
if (options.validationSchema) { | ||
const validationOptions = this.getSchemaValidationOptions(options); | ||
const { error, value: validatedConfig, } = options.validationSchema.validate(config, validationOptions); | ||
if (error) { | ||
throw new Error(`Config validation error: ${error.message}`); | ||
} | ||
config = validatedConfig; | ||
} | ||
if (options.type === 'env') { | ||
this.assignVariablesToProcess(config); | ||
} | ||
const isConfigToLoad = options.load !== undefined && options.load.length > 0; | ||
@@ -50,5 +47,17 @@ const providers = (options.load || []) | ||
provide: config_constants_1.CONFIGURATION_CONTENT_INITIALIZATION, | ||
useFactory: (configHost) => { | ||
useFactory: (configHost) => __awaiter(this, void 0, void 0, function* () { | ||
let config = yield this.loadFile(options); | ||
if (options.validationSchema) { | ||
const validationOptions = this.getSchemaValidationOptions(options); | ||
const { error, value: validatedConfig, } = options.validationSchema.validate(config, validationOptions); | ||
if (error) { | ||
throw new Error(`Config validation error: ${error.message}`); | ||
} | ||
config = validatedConfig; | ||
} | ||
if (options.type === 'env') { | ||
this.assignVariablesToProcess(config); | ||
} | ||
configHost[config_constants_1.VALIDATED_CONFIGURATION_KEY] = config; | ||
}, | ||
}), | ||
inject: [config_constants_1.CONFIGURATION_TOKEN], | ||
@@ -87,10 +96,14 @@ }, | ||
static loadFile(options) { | ||
switch (options.type) { | ||
case 'env': | ||
return file_loaders_util_1.loadEnvFile(options.envFile); | ||
case 'json': | ||
return file_loaders_util_1.loadJsonFile(options.jsonFile); | ||
default: | ||
throw new Error(`Incorrect configure type: ${options.type}`); | ||
} | ||
return __awaiter(this, void 0, void 0, function* () { | ||
switch (options.type) { | ||
case 'env': | ||
return yield file_loaders_util_1.loadEnvFile(options.envFile); | ||
case 'json': | ||
return yield file_loaders_util_1.loadJsonFile(options.jsonFile); | ||
case 'custom': | ||
return yield options.configLoader(); | ||
default: | ||
throw new Error(`Incorrect configure type: ${options.type}`); | ||
} | ||
}); | ||
} | ||
@@ -97,0 +110,0 @@ static forFeature(config) { |
import { ConfigFactory } from './config-factory.interface'; | ||
export interface ConfigModuleOptions { | ||
isGlobal?: boolean; | ||
type: 'env' | 'json'; | ||
export interface ConfigModuleEnvFileOptions { | ||
type: 'env'; | ||
envFile?: { | ||
@@ -12,2 +11,5 @@ ignoreEnvFile?: boolean; | ||
}; | ||
} | ||
export interface ConfigModuleJsonFileOptions { | ||
type: 'json'; | ||
jsonFile?: { | ||
@@ -17,2 +19,9 @@ filePath?: string | string[]; | ||
}; | ||
} | ||
export interface ConfigModuleCustomLoaderOptions { | ||
type: 'custom'; | ||
configLoader: () => Promise<Record<string, any>>; | ||
} | ||
interface ConfigModuleBaseOptions { | ||
isGlobal?: boolean; | ||
validationSchema?: any; | ||
@@ -22,1 +31,3 @@ validationOptions?: Record<string, any>; | ||
} | ||
export declare type ConfigModuleOptions = ConfigModuleBaseOptions & (ConfigModuleEnvFileOptions | ConfigModuleJsonFileOptions | ConfigModuleCustomLoaderOptions); | ||
export {}; |
@@ -1,3 +0,3 @@ | ||
import { ConfigModuleOptions } from '../interfaces'; | ||
export declare function loadEnvFile(options?: ConfigModuleOptions['envFile']): Record<string, any>; | ||
export declare function loadJsonFile(options?: ConfigModuleOptions['jsonFile']): Record<string, any>; | ||
import { ConfigModuleEnvFileOptions, ConfigModuleJsonFileOptions } from '../interfaces'; | ||
export declare function loadEnvFile(options?: ConfigModuleEnvFileOptions['envFile']): Promise<Record<string, any>>; | ||
export declare function loadJsonFile(options?: ConfigModuleJsonFileOptions['jsonFile']): Promise<Record<string, any>>; |
@@ -21,2 +21,11 @@ "use strict"; | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -32,32 +41,36 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
function loadEnvFile(options = {}) { | ||
const envFilePaths = Array.isArray(options.filePath) | ||
? options.filePath | ||
: [options.filePath || path_1.resolve(process.cwd(), '.env')]; | ||
const merged = envFilePaths | ||
.filter(filePath => fs.existsSync(filePath)) | ||
.reduce((config, filePath) => { | ||
const merged = Object.assign(dotenv.parse(fs.readFileSync(filePath)), config); | ||
if (options.expandVariables) { | ||
return dotenv_expand_1.default({ parsed: merged }).parsed || merged; | ||
} | ||
else { | ||
return merged; | ||
} | ||
}, {}); | ||
return options.ignoreEnvVars | ||
? merged | ||
: Object.assign(Object.assign({}, merged), process.env); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const envFilePaths = Array.isArray(options.filePath) | ||
? options.filePath | ||
: [options.filePath || path_1.resolve(process.cwd(), '.env')]; | ||
const merged = envFilePaths | ||
.filter(filePath => fs.existsSync(filePath)) | ||
.reduce((config, filePath) => { | ||
const merged = Object.assign(dotenv.parse(fs.readFileSync(filePath)), config); | ||
if (options.expandVariables) { | ||
return dotenv_expand_1.default({ parsed: merged }).parsed || merged; | ||
} | ||
else { | ||
return merged; | ||
} | ||
}, {}); | ||
return options.ignoreEnvVars | ||
? merged | ||
: Object.assign(Object.assign({}, merged), process.env); | ||
}); | ||
} | ||
exports.loadEnvFile = loadEnvFile; | ||
function loadJsonFile(options = {}) { | ||
const jsonFilePaths = Array.isArray(options.filePath) | ||
? options.filePath | ||
: [options.filePath || path_1.resolve(process.cwd(), 'config.json')]; | ||
return jsonFilePaths | ||
.filter(filePath => fs.existsSync(filePath)) | ||
.reduce((config, filePath) => { | ||
var _a; | ||
return Object.assign(JSON.parse(fs.readFileSync(filePath, (_a = options.encoding) !== null && _a !== void 0 ? _a : 'utf8')), config); | ||
}, {}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const jsonFilePaths = Array.isArray(options.filePath) | ||
? options.filePath | ||
: [options.filePath || path_1.resolve(process.cwd(), 'config.json')]; | ||
return jsonFilePaths | ||
.filter(filePath => fs.existsSync(filePath)) | ||
.reduce((config, filePath) => { | ||
var _a; | ||
return Object.assign(JSON.parse(fs.readFileSync(filePath, (_a = options.encoding) !== null && _a !== void 0 ? _a : 'utf8')), config); | ||
}, {}); | ||
}); | ||
} | ||
exports.loadJsonFile = loadJsonFile; |
{ | ||
"name": "nestjs-any-config", | ||
"version": "0.8.1", | ||
"description": "A module for NestJS that support JSON and dotenv (maybe more?) config files.", | ||
"version": "0.9.0", | ||
"description": "A module for NestJS that support JSON, dotenv config files or custom config loader.", | ||
"author": "Aaron Jan", | ||
@@ -6,0 +6,0 @@ "contributors": [ |
@@ -11,3 +11,3 @@ # NestJS Any Config | ||
This project is based on [`@nestjs/config`](https://github.com/nestjs/config), added JSON config support and kept original functions. | ||
This project is based on [`@nestjs/config`](https://github.com/nestjs/config), added JSON config and **custom config loader** support and kept original functions. | ||
@@ -64,2 +64,28 @@ ## Installation | ||
### Using custom config file loader | ||
You can using this method to load config from anywhere you want: | ||
```javascript | ||
import { Module } from '@nestjs/common'; | ||
import { ConfigModule } from 'nestjs-any-config'; | ||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
type: 'custom', | ||
configLoader: async () => { | ||
// Do your thing here | ||
// return the config | ||
return { | ||
// ... | ||
} | ||
}, | ||
}), | ||
], | ||
}) | ||
export class AppModule {} | ||
``` | ||
## Credits | ||
@@ -66,0 +92,0 @@ |
33302
617
96