@unifig/core
Advanced tools
Comparing version 0.14.0 to 0.14.2-dev.3
@@ -1,4 +0,3 @@ | ||
export * from './adapter'; | ||
export * from './plain'; | ||
export * from './sync-adapter'; | ||
export * from './class-adapters'; | ||
export * from './function-adapters'; | ||
export * from './types'; |
@@ -17,5 +17,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./adapter"), exports); | ||
__exportStar(require("./plain"), exports); | ||
__exportStar(require("./sync-adapter"), exports); | ||
__exportStar(require("./class-adapters"), exports); | ||
__exportStar(require("./function-adapters"), exports); | ||
__exportStar(require("./types"), exports); |
@@ -0,1 +1,3 @@ | ||
import { ConfigAdapter, ConfigSyncAdapter } from './class-adapters'; | ||
import { ConfigFunctionAdapter, ConfigFunctionSyncAdapter } from './function-adapters'; | ||
export type ConfigSource = { | ||
@@ -5,1 +7,2 @@ [key: string]: ConfigSourceEntry; | ||
export type ConfigSourceEntry = string | number | boolean | ConfigSource | Array<ConfigSourceEntry>; | ||
export type TemplateAdapter = ConfigSyncAdapter | ConfigAdapter | ConfigFunctionSyncAdapter | ConfigFunctionAdapter; |
import { SourceGroup } from '../source-group/source-group'; | ||
import { EditableConfigContainer } from './editable-container'; | ||
export declare class InternalConfigContainer<TTemplate extends Record<string, any> = any> implements EditableConfigContainer<TTemplate> { | ||
export declare class InternalConfigContainer<TTemplate extends Record<string, any> = Record<string, any>> implements EditableConfigContainer<TTemplate> { | ||
private readonly _sourceGroup; | ||
@@ -5,0 +5,0 @@ private _values; |
import { ConfigContainer } from './container'; | ||
export interface EditableConfigContainer<TTemplate extends Record<string, any> = any> extends ConfigContainer<TTemplate> { | ||
export interface EditableConfigContainer<TTemplate extends Record<string, any> = Record<string, any>> extends ConfigContainer<TTemplate> { | ||
setValues(values: TTemplate): void; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { ConfigSyncAdapter } from '../../adapters'; | ||
import { ConfigFunctionSyncAdapter, ConfigSyncAdapter } from '../../adapters'; | ||
import { ClassConstructor } from '../../utils/class-constructor'; | ||
@@ -6,3 +6,3 @@ import { SourceGroupOptions } from '../source-group/source-group.options'; | ||
interface RegisterTemplatesOptions extends SourceGroupOptions { | ||
adapter: ConfigSyncAdapter; | ||
adapter: ConfigSyncAdapter | ConfigFunctionSyncAdapter; | ||
} | ||
@@ -9,0 +9,0 @@ export interface RegisterSyncSingleTemplateOptions extends RegisterTemplatesOptions { |
@@ -1,2 +0,2 @@ | ||
import { ConfigAdapter, ConfigSyncAdapter } from '../../adapters'; | ||
import { TemplateAdapter } from '../../adapters'; | ||
import { ClassConstructor } from '../../utils/class-constructor'; | ||
@@ -6,3 +6,3 @@ import { SourceGroupOptions } from '../source-group/source-group.options'; | ||
interface RegisterTemplatesOptions extends SourceGroupOptions { | ||
adapter: ConfigSyncAdapter | ConfigAdapter; | ||
adapter: TemplateAdapter; | ||
} | ||
@@ -9,0 +9,0 @@ export interface RegisterSingleTemplateOptions extends RegisterTemplatesOptions { |
@@ -1,2 +0,2 @@ | ||
import { ConfigAdapter, ConfigSyncAdapter } from '../../adapters'; | ||
import { TemplateAdapter } from '../../adapters'; | ||
import { ClassConstructor } from '../../utils/class-constructor'; | ||
@@ -7,3 +7,3 @@ import { EditableConfigContainer } from '../container/editable-container'; | ||
readonly templates: ClassConstructor<any>[]; | ||
init(adapter: ConfigAdapter | ConfigSyncAdapter, templates: ClassConstructor[], options: SourceGroupOptions): void; | ||
init(adapter: TemplateAdapter, templates: ClassConstructor[], options: SourceGroupOptions): void; | ||
getContainer(template: ClassConstructor): EditableConfigContainer<any> | undefined; | ||
@@ -10,0 +10,0 @@ load(skipValidation?: boolean): Promise<object[]>; |
@@ -1,2 +0,2 @@ | ||
import { ConfigAdapter, ConfigSyncAdapter } from '../../adapters'; | ||
import { TemplateAdapter } from '../../adapters'; | ||
import { Loader } from '../../loader/loader'; | ||
@@ -17,5 +17,5 @@ import { ClassConstructor } from '../../utils/class-constructor'; | ||
constructor(_loader: Loader, _validator: Validator, _containerFactory: typeof containerFactory); | ||
init(adapter: ConfigAdapter | ConfigSyncAdapter, templates: ClassConstructor[], options: SourceGroupOptions): void; | ||
init(adapter: TemplateAdapter, templates: ClassConstructor[], options: SourceGroupOptions): void; | ||
get templates(): ClassConstructor<any>[]; | ||
getContainer(template: ClassConstructor): EditableConfigContainer<any> | undefined; | ||
getContainer(template: ClassConstructor): EditableConfigContainer<Record<string, any>> | undefined; | ||
/** | ||
@@ -31,3 +31,4 @@ * @param {boolean} skipValidation | ||
loadSync(skipValidation?: boolean): any[]; | ||
private getFactoryMethod; | ||
private loadFromSource; | ||
} |
@@ -28,3 +28,3 @@ "use strict"; | ||
async load(skipValidation = false) { | ||
const source = await this._adapter.load(); | ||
const source = await this.getFactoryMethod(this._adapter)(); | ||
return this.loadFromSource(source, skipValidation); | ||
@@ -37,3 +37,3 @@ } | ||
loadSync(skipValidation = false) { | ||
const source = this._adapter.load(); | ||
const source = this.getFactoryMethod(this._adapter)(); | ||
if (source instanceof Promise) { | ||
@@ -44,2 +44,5 @@ throw new exceptions_1.AdapterTypeMismatchException(this._adapter.constructor); | ||
} | ||
getFactoryMethod(adapter) { | ||
return typeof adapter === 'function' ? adapter : adapter.load.bind(adapter); | ||
} | ||
loadFromSource(source, skipValidation) { | ||
@@ -46,0 +49,0 @@ const values = this.templates.map((template) => this._loader.load(template, source, this._options)); |
@@ -9,5 +9,6 @@ "use strict"; | ||
this.message = | ||
'Following configurations failed validation: ' + errors.map(({ template }) => template.name).join(', '); | ||
'Following configurations failed validation: ' + | ||
errors.map(({ template }) => template.name).join(', '); | ||
} | ||
} | ||
exports.ConfigValidationError = ConfigValidationError; |
@@ -23,3 +23,6 @@ "use strict"; | ||
template: config.constructor, | ||
errors: (0, class_validator_1.validateSync)(config, { skipMissingProperties: false, forbidUnknownValues: false }).map((error) => this.toPropertyError(error)), | ||
errors: (0, class_validator_1.validateSync)(config, { | ||
skipMissingProperties: false, | ||
forbidUnknownValues: false, | ||
}).map((error) => this.toPropertyError(error)), | ||
}); | ||
@@ -60,3 +63,5 @@ } | ||
const propertyNestingType = nesting === null || nesting === void 0 ? void 0 : nesting.get(propertyKey); | ||
return (propertyNestingType ? propertyNestingType() : Reflect.getMetadata('design:type', config, propertyKey)).name; | ||
return (propertyNestingType | ||
? propertyNestingType() | ||
: Reflect.getMetadata('design:type', config, propertyKey)).name; | ||
} | ||
@@ -63,0 +68,0 @@ formatPropertyValue(config, propertyKey, value) { |
{ | ||
"name": "@unifig/core", | ||
"version": "0.14.0", | ||
"version": "0.14.2-dev.3+ae975b5", | ||
"description": "Universal, typed and validated configuration manager", | ||
@@ -41,3 +41,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "a4756681fddaab5b9ad9ae444e791491b62ce510" | ||
"gitHead": "ae975b58daea0abf375cfcdb508bc80ab6e7dda9" | ||
} |
@@ -20,5 +20,7 @@ <h1 align="center">Unifig</h1> | ||
- [Subtemplates](#templates_subtemplates) | ||
- [Loading](#loading) | ||
- [Using Configuration](#loading) | ||
- [Quick Start](#loading_quick_start) | ||
- [Values Adapters](#loading_adapters) | ||
- [Types conversion](#loading_adapters_conversion) | ||
- [Functions adapters](#loading_adapters_functions) | ||
- [Multiple Configurations](#loading_multiple_configurations) | ||
@@ -95,8 +97,12 @@ - [Inline Validation Rejection](#loading_inline_rejection) | ||
## Loading configuration | ||
## Using Configuration | ||
<a name="loading"></a> | ||
After defining template they should be loaded before any other action in the application takes place. | ||
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. | ||
### Quick Start | ||
<a name="loading_quick_start"></a> | ||
```ts | ||
@@ -108,3 +114,3 @@ import { Config, PlainConfigAdapter } from '@unifig/core'; | ||
template: AppSettings, | ||
adapter: new PlainConfigAdapter({ | ||
adapter: async () => ({ | ||
PORT: 3000, | ||
@@ -117,14 +123,2 @@ DB_URL: 'localhost:5467', | ||
// OR | ||
const validationError = Config.registerSync({ | ||
template: AppSettings, | ||
adapter: new PlainConfigAdapter({ | ||
PORT: 3000, | ||
DB_URL: 'localhost:5467', | ||
DB_PASSWORD: 'password', | ||
DB_RECONNECT_DELAYS: '56,98,34,72', | ||
}), | ||
}); | ||
if (validationError) { | ||
@@ -135,3 +129,5 @@ console.error(validationError.message); | ||
console.log(Config.getValues(AppSettings).port); // output: 3000 | ||
const options = Config.getValues(AppSettings); | ||
console.log(options.port); // output: 3000 | ||
console.log(options.db.url); // output: localhost:5467 | ||
} | ||
@@ -142,6 +138,2 @@ | ||
Above example uses built-in adapter which transforms static object into Settings. See full list of adapters [here](https://github.com/Matii96/unifig#packages). | ||
<a name="loading"></a> | ||
### Values adapters | ||
@@ -197,4 +189,24 @@ | ||
#### Types conversion | ||
See full list of adapters [here](https://github.com/Matii96/unifig#packages). | ||
#### Functions Adapters | ||
<a name="loading_adapters_functions"></a> | ||
Alternatively adapter can be defined as standalone sync or async function with same rules applied. | ||
```ts | ||
Config.registerSync({ | ||
template: AppSettings, | ||
adapter: () => ({ | ||
PORT: '3000', | ||
DB_URL: 'localhost:5467', | ||
DB_PASSWORD: 'password', | ||
DB_RECONNECT_DELAYS: '56,98,34,72', | ||
}), | ||
}); | ||
``` | ||
#### Types Conversion | ||
<a name="loading_adapters_conversion"></a> | ||
@@ -226,3 +238,3 @@ | ||
### Inline validation rejection | ||
### Inline Validation Rejection | ||
@@ -229,0 +241,0 @@ <a name="loading_inline_rejection"></a> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62233
119
1164
309
1