UNPKG

alpha-dic

Version:

Asynchronous dependency injection container

78 lines (77 loc) 3.12 kB
import { AnnotationPredicate, ServiceName, DefinitionPredicate, Middleware, ServiceFactory } from './types'; import { Definition } from './Definition'; export declare class Container { private definitions; private services; private middlewares; readonly parent?: Container; /** * Time needed to trigger debug slow creation log */ slowLogThreshold: number; constructor(parent?: Container); /** * Registers given service definition */ registerDefinition(definition: Definition): this; /** * Creates and registers service definition * * Returns created definition for further configuration */ definition(name?: ServiceName): Definition; /** * Creates and registers service definition with given name, function as constructor */ definitionWithConstructor(name: ServiceName, clazz: { new (...args: any[]): any; }): Definition; definitionWithConstructor(clazz: { new (...args: any[]): any; }): Definition; /** * Creates and registers service definition with given name, function as factory */ definitionWithFactory(name: ServiceName, factory: ServiceFactory, type?: Function): Definition; definitionWithFactory(factory: ServiceFactory, type?: Function): Definition; /** * Creates and registers service definition with given name and value as a service */ definitionWithValue(name: ServiceName, value: any): Definition; definitionWithValue(value: any): Definition; /** * Returns definition by given name */ findByName(name: ServiceName): Definition | undefined; /** * Returns definitions that satisfy given predicate */ findByPredicate(predicate: DefinitionPredicate): Definition[]; /** * Returns all definitions that contain annotation that satisfied given predicate */ findByAnnotation(predicate: AnnotationPredicate): Definition[]; findByAnnotation(predicate: AnnotationPredicate, withAnnotation: false): Definition[]; findByAnnotation(predicate: AnnotationPredicate, withAnnotation: true): Array<[Definition, any]>; /** * Registers given middleware */ addMiddleware(...middlewares: Middleware[]): Container; getMiddlewares(): Middleware[]; /** * Returns service for given name or definition */ get<T = any>(nameOrDefinition: ServiceName | Definition): Promise<T>; private create; /** * Returns all services that definition satisfies predicate */ getByPredicate<T = any>(predicate: DefinitionPredicate): Promise<T[]>; /** * Returns all services that definition contains annotation that satisfies given predicate */ getByAnnotation<T = any>(predicate: AnnotationPredicate): Promise<T[]>; getByAnnotation<T = any>(predicate: AnnotationPredicate, withAnnotation: false): Promise<T[]>; getByAnnotation<T = any, TAnnotation = any>(predicate: AnnotationPredicate, withAnnotation: true): Promise<Array<[T, TAnnotation]>>; alias(definition: Definition, options?: Definition.AliasOptions): Definition; }