UNPKG

alpha-dic

Version:

Asynchronous dependency injection container

84 lines (83 loc) 2.58 kB
import { ServiceFactory, ServiceName } from './types'; import { TypeRef } from './TypeRef'; import { Container } from "./Container"; export interface DefinitionData { name: ServiceName; annotations: any[]; factory: ServiceFactory; args: any[]; type?: TypeRef; } export declare class Definition implements DefinitionData { args: any[]; annotations: any[]; factory: ServiceFactory; name: ServiceName; type?: TypeRef; owner?: Container; constructor(name?: ServiceName); /** * Sets service constructor */ useConstructor(constructor: { new (...args: any[]): any; }): this; /** * Alias for {@see useConstructor} */ useClass(clazz: { new (...args: any[]): any; }): this; /** * Sets factory used to create an instance of service. * In case of asynchronous service creation, factory should return a promise. * * The factory value is called in context of AlphaDIC. */ useFactory(factory: (...args: any[]) => any | Promise<any>, type?: Function): this; setOwner(container: Container): this; /** * Sets information about type of final service */ markType(type: TypeRef | Function): this; /** * Defined service as provided value */ useValue(value: any): this; /** * Sets the array of arguments provided to service factory. * All arguments are provided directly to service constructor or * factory unless they are an instance of ContainerArg which has to be resolved first */ withArgs(...args: any[]): this; /** * Adds annotation to the service * Annotation is a simple metadata object assigned to service that you might use for different purposes */ annotate(...annotations: any[]): this; static create(name?: ServiceName): Definition; /** * Locks definitions by making it immutable */ lock(): Readonly<this>; /** * Returns new, locked one, object of Definition with new data */ modify(data: Partial<DefinitionData>): Readonly<Definition>; /** * Creates new definition that is an alias for current one */ createAlias(options?: Definition.AliasOptions): Definition; } export declare namespace Definition { interface AliasOptions { /** * New definition ane */ name?: string; /** * Forwards all annotations if true, none if false or the ones that satisfy given predicate */ withAnnotations?: boolean | ((ann: any) => boolean); } }