UNPKG

4.07 kBTypeScriptView Raw
1import { ServiceMetadata } from "./types/ServiceMetadata";
2import { ObjectType } from "./types/ObjectType";
3import { Handler } from "./types/Handler";
4import { Token } from "./Token";
5import { ServiceIdentifier } from "./types/ServiceIdentifier";
6import { ContainerInstance } from "./ContainerInstance";
7/**
8 * Service container.
9 */
10export declare class Container {
11 /**
12 * Global container instance.
13 */
14 private static readonly globalInstance;
15 /**
16 * Other containers created using Container.of method.
17 */
18 private static readonly instances;
19 /**
20 * All registered handlers.
21 */
22 static readonly handlers: Handler[];
23 /**
24 * Gets a separate container instance for the given instance id.
25 */
26 static of(instanceId: any): ContainerInstance;
27 /**
28 * Checks if the service with given name or type is registered service container.
29 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
30 */
31 static has<T>(type: ObjectType<T>): boolean;
32 /**
33 * Checks if the service with given name or type is registered service container.
34 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
35 */
36 static has<T>(id: string): boolean;
37 /**
38 * Checks if the service with given name or type is registered service container.
39 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
40 */
41 static has<T>(id: Token<T>): boolean;
42 /**
43 * Retrieves the service with given name or type from the service container.
44 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
45 */
46 static get<T>(type: ObjectType<T>): T;
47 /**
48 * Retrieves the service with given name or type from the service container.
49 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
50 */
51 static get<T>(id: string): T;
52 /**
53 * Retrieves the service with given name or type from the service container.
54 * Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
55 */
56 static get<T>(id: Token<T>): T;
57 /**
58 * Gets all instances registered in the container of the given service identifier.
59 * Used when service defined with multiple: true flag.
60 */
61 static getMany<T>(id: string): T[];
62 /**
63 * Gets all instances registered in the container of the given service identifier.
64 * Used when service defined with multiple: true flag.
65 */
66 static getMany<T>(id: Token<T>): T[];
67 /**
68 * Sets a value for the given type or service name in the container.
69 */
70 static set<T, K extends keyof T>(service: ServiceMetadata<T, K>): Container;
71 /**
72 * Sets a value for the given type or service name in the container.
73 */
74 static set(type: Function, value: any): Container;
75 /**
76 * Sets a value for the given type or service name in the container.
77 */
78 static set(name: string, value: any): Container;
79 /**
80 * Sets a value for the given type or service name in the container.
81 */
82 static set(token: Token<any>, value: any): Container;
83 /**
84 * Sets a value for the given type or service name in the container.
85 */
86 static set<T, K extends keyof T>(values: ServiceMetadata<T, K>[]): Container;
87 /**
88 * Removes services with a given service identifiers (tokens or types).
89 */
90 static remove(...ids: ServiceIdentifier[]): Container;
91 /**
92 * Completely resets the container by removing all previously registered services and handlers from it.
93 */
94 static reset(containerId?: any): Container;
95 /**
96 * Registers a new handler.
97 */
98 static registerHandler(handler: Handler): Container;
99 /**
100 * Helper method that imports given services.
101 */
102 static import(services: Function[]): Container;
103}