UNPKG

nest-simple-config

Version:

A powerful and flexible configuration management library for NestJS applications. Supports JSON, YAML file loading, environment variable overrides, immutable configurations, and type-safe configuration access with dependency injection.

148 lines (147 loc) 6.83 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var SimpleConfigModule_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.SimpleConfigModule = void 0; const common_1 = require("@nestjs/common"); const _1 = require("."); const _ = __importStar(require("lodash")); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const config_options_module_1 = require("./config-options.module"); let SimpleConfigModule = exports.SimpleConfigModule = SimpleConfigModule_1 = class SimpleConfigModule { static forRoot(options) { options = SimpleConfigModule_1.mergeDefaultOptional(options); return SimpleConfigModule_1.forRootWithConfigBuilder(SimpleConfigModule_1.createBuildActionFromOptions(options)); } static forRootWithConfigBuilder(buildAction) { const defaultOptions = new _1.DefaultSimpleConfigOptions(); const builder = new _1.ConfigurationBuilder(defaultOptions); buildAction = buildAction ?? ((b) => { b.add(new _1.JsonConfigurationProvider(path.join(__dirname, 'appsettings.json'))); b.add(new _1.JsonConfigurationProvider(path.join(__dirname, `appsettings.${process.env.NODE_ENV}.json`), true)); b.add(new _1.EnvConfigurationProvider(defaultOptions.envOptions)); b.add(new _1.CommandlineConfigurationProvider()); }); buildAction(builder); const configObj = builder.build(); return { module: SimpleConfigModule_1, global: true, providers: [ { provide: _1.CONFIG_OPTIONAL, useValue: builder.options, }, { provide: _1.CONFIG_OBJECT, useFactory: () => { return configObj; }, }, { provide: _1.Configuration, useFactory: (optional, config) => new _1.Configuration(optional, config), inject: [_1.CONFIG_OPTIONAL, _1.CONFIG_OBJECT] }, ], exports: [_1.Configuration], }; } static mergeDefaultOptional(options) { const defaultOptions = { keyPathDelimiter: '.', arrayMergeMode: 'section', configFileOptions: { fileType: 'json', filename: 'appsettings', rootPath: '.', includeMiddleNames: [], }, envOptions: { prefix: 'NestApp', delimiter: '__' }, }; options = _.merge(defaultOptions, (0, _1.definedProps)(options)); return options; } static createBuildActionFromOptions(options) { const _options = SimpleConfigModule_1.mergeDefaultOptional(options); const generateFileConfigProviders = (fileOptions) => { const baseFile = path.parse(fileOptions.filename); const baseFilename = baseFile.name; const ext = baseFile.ext !== '' ? baseFile.ext : `.${fileOptions.fileType}`; const root = baseFile.dir !== '' ? baseFile.dir : fileOptions.rootPath; const includeMiddleNames = fileOptions.includeMiddleNames ?? []; const allConfigFiles = [undefined, process.env.NODE_ENV, ...includeMiddleNames] .map(m => { const filename = m ? [baseFilename, m].join('.') : baseFilename; const fullFilName = path.join(root, `${filename}${ext}`); return fullFilName; }); const fileProviderFactory = (filename) => { switch (fileOptions.fileType) { case 'json': return new _1.JsonConfigurationProvider(filename); break; case 'yaml': return new _1.YamlConfigurationProvider(filename); default: throw new Error(`type ${fileOptions.fileType} not supports`); } }; return _.chain(allConfigFiles) .filter(f => fs.existsSync(f)) .map(f => fileProviderFactory(f)) .value(); }; return (b) => { b.options.arrayMergeMode = _options.arrayMergeMode; b.options.keyPathDelimiter = _options.keyPathDelimiter; b.addRange(...generateFileConfigProviders(_options.configFileOptions)) .add(new _1.EnvConfigurationProvider(_options.envOptions)) .add(new _1.CommandlineConfigurationProvider()); }; } // eslint-disable-next-line @typescript-eslint/ban-types static registerOptions(optionTypes) { const configOptionsModule = config_options_module_1.ConfigOptionsModule.register(optionTypes); return { module: SimpleConfigModule_1, imports: [configOptionsModule], providers: configOptionsModule.providers, exports: configOptionsModule.exports, }; } }; exports.SimpleConfigModule = SimpleConfigModule = SimpleConfigModule_1 = __decorate([ (0, common_1.Module)({}) ], SimpleConfigModule);