dino-core
Version:
A dependency injection framework for NodeJS applications
110 lines (109 loc) • 5.88 kB
TypeScript
import { type ApplicationContext } from '../context/ApplicationContext';
import { type Injectable } from './injectable';
import { type ResolvableInjectable, type ResolvedInjectable, Resolver } from './resolver';
import { type Scope } from './scope';
export type InjectableType = new (...a: any[]) => Injectable;
export type ComponentType = string | Injectable | Record<string, unknown> | InjectableType | ResolvableInjectable<Injectable> | unknown;
export type RequiredComponentType = Injectable | Record<string, unknown> | InjectableType | ResolvableInjectable<Injectable>;
/**
* The definition of the object that need to be created for the application context
* @public
*/
export declare class ComponentDescriptor {
private readonly name;
private readonly scope;
private readonly lazy;
private readonly resolver;
private readonly dependencies;
private readonly isConfiguration;
private readonly dependencyExtractor;
private component;
/**
* Create a new component descriptor
* @param {String} name the component registration name
* @param {String|Injectable} component the source for the component
* @param {Scope} scope the scope for the component resolution
* @param {Resolver} resolver the resolver for the component
* @param {Injectable} value deprecated use source instead
* @param {Boolean} lazy a flag indicating if the component should be lazy loaded
* @param {Array<String>} dependencies the dependencies defined for this component
* @param {Boolean} isConfiguration a flag indicating if this component descriptor is loading a configuration class
*/
constructor(name: string, component: ComponentType, scope: Scope, resolver: Resolver, lazy?: boolean, dependencies?: string[], isConfiguration?: boolean);
getName(): string;
getScope(): Scope;
isForConfiguration(): boolean;
/**
* Resolve a component in to its primitive form to be managed by awilix
* @param {ApplicationContext} applicationContext the application context that will be injected for later
* resolution of if the component is defined as lazy
*
* @returns {any} the awilix resolver
* @public
*/
resolve(applicationContext: ApplicationContext): ResolvedInjectable<Injectable>;
/**
* Validate if the component wrapped on this component descriptor has no dependencies or depends only on base dependencies;
* @returns {Boolean} true if has not dependencies or depends only on base ones, false otherwise
*
* @public
*/
hasNotDependenciesOrDependsOnlyOnBase(): boolean;
/**
* Allows to access the dependencies for the wrapped component
* @returns {Array<String>} the array that describe the dependencies for this component
*
* @public
*/
getDependencies(): string[];
private requireIfNeeded;
/**
* Create an new generic component descriptor.
*
* @param {String} name the name of the component, it will be used as a reference to the component
* in the application context and for injection.
* @param {String|Component} component the path of the source file relative to the root of the project or the component itself
* @param 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
* @param {Resolver} resolver the resolver that will be used to create the resolution scope (or a child scope)
* @param {Boolean} lazy a flag indicating if this component descriptor should be lazily instantiated
* @param {Array<String>} dependencies the dependencies for the component described using this component descriptor
*
* @returns {ComponentDescriptor} the ComponentDescriptor
*
* @public
* @static
*
* @deprecated use any of {ComponentDescriptor#createFromValue} or {ComponentDescriptor#createFromImport} or {ComponentDescriptor#createFromType} methods
*/
static create(name: string, source: ComponentType, scope: Scope, resolver: Resolver, lazy: boolean | undefined, dependencies: string[], isConfiguration?: boolean): ComponentDescriptor;
/**
* Create a component descriptor for value base context registration
* @param name the name of the value, used to reference this value on the application context
* @param value the value to set on the application context
* @param scope the resolution scope for the value
*
* @returns the ComponentDescriptor
*/
static createFromValue(name: string, value: any, scope: Scope): ComponentDescriptor;
/**
* Create a component descriptor for value base context registration
* @param name the name of the value, used to reference this value on the application context
* @param path the path to the script to import and that will define the object to register on the application context
* @param scope the resolution scope for the value
*
* @returns the ComponentDescriptor
*/
static createFromImport(name: string, path: string, scope: Scope): ComponentDescriptor;
/**
* Create a component descriptor for class base context registration
* @param name the name of the value, used to reference this value on the application context
* @param type the type definition to register as class in the application context
* @param scope the scope of the value
*
* @returns {ComponentDescriptor} the ComponentDescriptor
*/
static createFromType(name: string, type: InjectableType | ResolvableInjectable<Injectable>, scope: Scope): ComponentDescriptor;
}