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.
85 lines (84 loc) • 3.85 kB
JavaScript
;
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 __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Configuration = void 0;
const common_1 = require("@nestjs/common");
const _1 = require(".");
const flat_1 = require("flat");
const _ = __importStar(require("lodash"));
let Configuration = exports.Configuration = class Configuration {
constructor(optional, configObject) {
this.optional = optional;
this.configObject = configObject;
// 一次性處理:複製並凍結
this.immutableConfig = this.makeImmutable(this.configObject);
}
makeImmutable(obj) {
// 使用 structuredClone (Node.js 17+) 或 lodash cloneDeep
const cloned = typeof structuredClone !== 'undefined'
? structuredClone(obj)
: _.cloneDeep(obj);
return this.deepFreeze(cloned);
}
deepFreeze(obj) {
if (obj && typeof obj === 'object') {
Object.values(obj).forEach(value => this.deepFreeze(value));
Object.freeze(obj);
}
return obj;
}
// get<T = any>(key: string): T {
// const k = key.replace(this.optional.keyPathDelimiter as string, '.');
// const section = _.get(this.configObject, k) ;
// return _.cloneDeep(section) as T;
// }
get(key) {
const k = key.replace(this.optional.keyPathDelimiter, '.');
const section = _.get(this.immutableConfig, k);
// 直接返回,因為已經是不可變的
return section;
}
getEntries() {
return (0, flat_1.flatten)(this.configObject, { delimiter: this.optional.keyPathDelimiter });
}
};
exports.Configuration = Configuration = __decorate([
(0, common_1.Injectable)({ scope: common_1.Scope.DEFAULT }),
__param(0, (0, common_1.Inject)(_1.CONFIG_OPTIONAL)),
__param(1, (0, common_1.Inject)(_1.CONFIG_OBJECT)),
__metadata("design:paramtypes", [Object, Object])
], Configuration);