UNPKG

3.14 kBTypeScriptView Raw
1import { ContainerInstance } from './container-instance.class';
2import { Token } from './token.class';
3import { Handler } from './interfaces/handler.interface';
4import { Constructable } from './types/constructable.type';
5import { ServiceIdentifier } from './types/service-identifier.type';
6import { ServiceOptions } from './interfaces/service-options.interface';
7import { AbstractConstructable } from './types/abstract-constructable.type';
8/**
9 * Service container.
10 */
11export declare class Container {
12 /**
13 * All registered handlers. The @Inject() decorator uses handlers internally to mark a property for injection.
14 **/
15 static readonly handlers: Handler[];
16 /** Global container instance. */
17 private static readonly globalInstance;
18 /** Other containers created using Container.of method. */
19 private static readonly instances;
20 /**
21 * Gets a separate container instance for the given instance id.
22 */
23 static of(containerId?: string): ContainerInstance;
24 /**
25 * Checks if the service with given name or type is registered service container.
26 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
27 */
28 static has<T>(type: Constructable<T>): boolean;
29 static has<T>(id: string): boolean;
30 static has<T>(id: Token<T>): boolean;
31 /**
32 * Retrieves the service with given name or type from the service container.
33 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
34 */
35 static get<T>(type: Constructable<T>): T;
36 static get<T>(type: AbstractConstructable<T>): T;
37 static get<T>(id: string): T;
38 static get<T>(id: Token<T>): T;
39 /**
40 * Gets all instances registered in the container of the given service identifier.
41 * Used when service defined with multiple: true flag.
42 */
43 static getMany<T>(id: string): T[];
44 static getMany<T>(id: Token<T>): T[];
45 /**
46 * Sets a value for the given type or service name in the container.
47 */
48 static set<T = unknown>(type: Function, value: any): Container;
49 static set<T = unknown>(type: Constructable<T>, value: any): Container;
50 static set<T = unknown>(type: AbstractConstructable<T>, value: any): Container;
51 static set<T = unknown>(name: string, value: any): Container;
52 static set<T = unknown>(token: Token<T>, value: any): Container;
53 static set<T = unknown>(value: ServiceOptions<T>): Container;
54 static set<T = unknown>(values: ServiceOptions<T>[]): Container;
55 /**
56 * Removes services with a given service identifiers.
57 */
58 static remove(identifierOrIdentifierArray: ServiceIdentifier | ServiceIdentifier[]): Container;
59 /**
60 * Completely resets the container by removing all previously registered services and handlers from it.
61 */
62 static reset(containerId?: string): Container;
63 /**
64 * Registers a new handler.
65 */
66 static registerHandler(handler: Handler): Container;
67 /**
68 * Helper method that imports given services.
69 */
70 static import(services: Function[]): Container;
71}