UNPKG

dino-core

Version:

A dependency injection framework for NodeJS applications

122 lines 6.52 kB
// Copyright 2018 Quirino Brizi [quirino.brizi@gmail.com] // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. 'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.Configuration = void 0; const tslib_1 = require("tslib"); const camel_case_1 = tslib_1.__importDefault(require("camel-case")); const object_helper_1 = require("../helper/object.helper"); const ComponentDescriptor_1 = require("./ComponentDescriptor"); const injectable_1 = require("./injectable"); const resolver_1 = require("./resolver"); const scope_1 = require("./scope"); /** * A configuration allows to provide component descriptors that describe Injectables that may need additional configuration or that are * external to the application core * * When contextScan is set to true, classes extending this class will be * automatically discovered and injected on the application context. * @typedef {Configuration} * * @public * @abstract */ class Configuration extends injectable_1.Injectable { constructor({ applicationContext }) { super(); this.applicationContext = applicationContext; } /** * Create a component descriptor to be used to register the value defined on this configuration on the application context. * * @param {String|Component} value the {String|Component} value to register on the application context * @param {Scope} scope the {@link Scope} that the newly registered component should have defaults to {@link Scope.SINGLETON} * @param {Resolver} resolver the {@link Resolver} that the newly registered component should have defaults to {@link Resolver.CLASS} * @param {String} name the name to assign to the component at resolution time, it will be converted as a camel-case, * will be inferred from `value.name` if undefined * * @returns {ComponentDescriptor} the {@link ComponentDescriptor} to use for resolution * * @deprecated use any of {ComponentDescriptor#createFromValue} or {ComponentDescriptor#createFromImport} or {ComponentDescriptor#createFromType} methods * @public */ asComponentDescriptor(value, scope = scope_1.Scope.SINGLETON, resolver = resolver_1.Resolver.CLASS, name = undefined) { if (object_helper_1.ObjectHelper.isNotDefined(name) && object_helper_1.ObjectHelper.isNotDefined(value.constructor.name)) { throw new Error('unable to determine a name for the component descriptor'); } const _name = (0, camel_case_1.default)(name ?? value.constructor.name); return ComponentDescriptor_1.ComponentDescriptor.create(_name, value, scope, resolver, false, []); } /** * Create a component descriptor to be used to register the value defined on this configuration on the application context. * * @param {Component} value the value to register on the application context * @param {String} scope the scope of the component, can be one of: * - **transient**: a new instance of the component will be created ond injected when required * - **singleton**: a single component instance will be create and reused every time is required * - **scoped**: The registration is scoped to the container - that means that the resolved value * * @deprecated use {@link Configuration.asComponentDescriptor} as `Configuration.asComponentDescriptor(component, scope, Resolver.VALUE)` instead * @public * */ asComponentDescriptorWithScope(value, scope) { return this.asComponentDescriptor(value, scope, resolver_1.Resolver.VALUE); } /** * Create a component descriptor to be used to register the value defined on this configuration on the application context. * * @param {Component} value the value to register on the application context * @param {String} name the name of the value component to use it will be used as a primary reference, if not provided * the framework will try to read the name from the value property of the provided component. * * @deprecated use {@link Configuration.asComponentDescriptor} as `Configuration.asComponentDescriptor(component, Scope.SINGLETON, Resolver.VALUE, name)` instead * @public */ createValueTypeComponentDescriptor(value, name) { return this.asComponentDescriptor(value, scope_1.Scope.SINGLETON, resolver_1.Resolver.VALUE, name); } /** * Create a component descriptor to be used to register the value defined on this configuration on the application context. * * @param {Component} value the value to register on the application context * @param {String} name the name of the value component to use it will be used as a primary reference, if not provided * the framework will try to read the name from the value property of the provided component. * @param {String} scope the scope of the component, can be one of: * - **transient**: a new instance of the component will be created ond injected when required * - **singleton**: a single component instance will be create and reused every time is required * - **scoped**: The registration is scoped to the container - that means that the resolved value * * @deprecated use {@link Configuration.asComponentDescriptor} as `Configuration.asComponentDescriptor(component, scope, Resolver.VALUE, name)` instead * @public */ createValueTypeComponentDescriptorWithScope(value, name, scope) { return this.asComponentDescriptor(value, scope, resolver_1.Resolver.VALUE, name); } /** * Requires a component and register it as part of the application context * * @param {String} component the path of the component to require * * @public */ require(component) { this.applicationContext.require(component); } getTypeName() { return 'Configuration'; } } exports.Configuration = Configuration; //# sourceMappingURL=configuration.js.map