Socket
Socket
Sign inDemoInstall

@unifig/core

Package Overview
Dependencies
Maintainers
1
Versions
39
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@unifig/core - npm Package Compare versions

Comparing version 1.0.3-dev.0 to 1.0.3-dev.2

dist/loader/get-nested-templates/get-nested-templates.d.ts

1

dist/loader/index.d.ts
export * from './decorators';
export * from './get-nested-templates';

@@ -18,1 +18,2 @@ "use strict";

__exportStar(require("./decorators"), exports);
__exportStar(require("./get-nested-templates"), exports);

9

dist/manager/manager.impl.js

@@ -5,2 +5,3 @@ "use strict";

require("reflect-metadata");
const loader_1 = require("../loader");
const config_not_initialized_exception_1 = require("./exceptions/config-not-initialized.exception");

@@ -35,8 +36,9 @@ class InternalConfigManager {

}
groups.forEach((group) => group.templates.forEach((template) => this._groups.set(template, group)));
groups.forEach((group) => group.templates.forEach((template) => (0, loader_1.getNestedTemplates)(template).forEach((template) => this._groups.set(template, group))));
}
initSourceGroups(options) {
let templates = options.templates;
if (!templates)
if (!templates) {
templates = [options.template];
}
const sourceGroup = this._sourceGroupFactory();

@@ -52,4 +54,5 @@ // Register only templates that were not registered already

const group = this._groups.get(template);
if (!group)
if (!group) {
throw new config_not_initialized_exception_1.ConfigNotInitializedException(template);
}
return group.getContainer(template);

@@ -56,0 +59,0 @@ }

@@ -16,2 +16,3 @@ import { TemplateAdapter } from '../../adapters';

private readonly _containers;
private readonly _parentTemplates;
constructor(_loader: Loader, _validator: Validator, _containerFactory: typeof containerFactory);

@@ -33,2 +34,3 @@ init(adapter: TemplateAdapter, templates: ClassConstructor[], options: SourceGroupOptions): void;

private loadFromSource;
private setContainerValues;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigSourceGroup = void 0;
const loader_1 = require("../../loader");
const constants_1 = require("../../loader/constants");
const exceptions_1 = require("./exceptions");

@@ -11,2 +13,3 @@ class ConfigSourceGroup {

this._containers = new Map();
this._parentTemplates = new Set();
}

@@ -16,3 +19,6 @@ init(adapter, templates, options) {

this._adapter = adapter;
templates.forEach((template) => this._containers.set(template, this._containerFactory(this)));
templates.forEach((template) => {
this._parentTemplates.add(template);
(0, loader_1.getNestedTemplates)(template).forEach((template) => this._containers.set(template, this._containerFactory(this)));
});
}

@@ -48,12 +54,28 @@ get templates() {

loadFromSource(source, skipValidation) {
const values = this.templates.map((template) => this._loader.load(template, source, this._options));
const parents = Array.from(this._parentTemplates);
const values = parents.map((template) => this._loader.load(template, source, this._options));
if (!skipValidation) {
const validationResult = this._validator.validate(values);
if (validationResult)
if (validationResult) {
throw validationResult;
}
}
this.templates.forEach((template, idx) => this.getContainer(template).setValues(values[idx]));
parents.forEach((template, idx) => this.setContainerValues(template, values[idx]));
return values;
}
setContainerValues(template, values) {
const container = this.getContainer(template);
container.setValues(values);
const nesting = Reflect.getMetadata(constants_1.PROPERTIES_NESTING_METADATA, template);
if (!nesting) {
return;
}
for (const [targetKey, getSubTemplate] of nesting) {
const value = values[targetKey];
if (typeof value !== 'undefined') {
this.setContainerValues(getSubTemplate(), value);
}
}
}
}
exports.ConfigSourceGroup = ConfigSourceGroup;
{
"name": "@unifig/core",
"version": "1.0.3-dev.0+189c944",
"version": "1.0.3-dev.2+fdca8de",
"description": "Universal, typed and validated configuration manager",

@@ -41,3 +41,3 @@ "keywords": [

},
"gitHead": "189c944682f00a742d2477162e92a77999ec8e88"
"gitHead": "fdca8de911053eda7cbce5d0239717cf07d80075"
}

@@ -19,3 +19,3 @@ <h1 align="center">Unifig</h1>

- [Setting up Templates](#templates)
- [Subtemplates](#templates_subtemplates)
- [Nested Configuration](#templates_subtemplates)
- [Using Configuration](#loading)

@@ -27,2 +27,3 @@ - [Quick Start](#loading_quick_start)

- [Multiple Configurations](#loading_multiple_configurations)
- [Accessing Nested Configuration](#loading_accessing_nested_configuration)
- [Inline Validation Rejection](#loading_inline_rejection)

@@ -80,7 +81,7 @@ - [Stale Data](#stale_data)

### Subtemplates
### Nested Configuration
<a name="templates_subtemplates"></a>
They allow to keep config structure organized by grouping inseparable properties and allowing reusing of them. The subtemplate itself is declared just as regular template, including option to nest further subtemplates in it.
Allows to keep config structure organized by grouping inseparable properties and allowing reusage of them. The subtemplate itself is declared just as regular template, including option to nest further subtemplates in it.

@@ -98,7 +99,7 @@ ```ts

## Using Configuration
## Accessing Configuration
<a name="loading"></a>
Such defined templates should be loaded before any other action in the application takes place. After that configuration can be accessed from any place in the app via global `Config` reference.
Templates should be loaded before any other action in the application takes place. After that configuration can be accessed from any place in the app via global `Config` reference.

@@ -234,2 +235,30 @@ ### Quick Start

### Accessing Nested Configuration
<a name="loading_accessing_nested_configuration"></a>
[Nested config values](#templates_subtemplates) can be accessed directly after parent initialization.
```ts
class DbConfig {
@IsString()
url: string;
}
class AppConfig {
@IsInt()
port: number;
@Nested(() => DbConfig)
db: DbConfig;
}
Config.registerSync({
template: AppConfig,
adapter: () => ({ port: 3000, db: { url: 'db://localhost:5467' } }),
});
console.log('Db url: ' + Config.getValues(DbConfig).url); // => Db url: db://localhost:5467
```
### Inline Validation Rejection

@@ -236,0 +265,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc