UNPKG

7.8 kBTypeScriptView Raw
1import { HashMap } from "@azera/util/is";
2import { ContainerValue, Factory, IAutoTagger, IContainer, IDefinition, IMethod, Injectable, Invokable, MockMethod, Service, Constructor } from "./types";
3export declare const FACTORY_REGEX: RegExp;
4export declare const SERVICE_REGEX: RegExp;
5/**
6 * Dependency Injection Container
7 * @author Masoud Zohrabi <mdzzohrabi@gmail.com>
8 */
9export declare class Container implements IContainer {
10 /**
11 * Check if decorated class was inherited from another decorated class
12 * @param target Class
13 */
14 static isInheritedServiceDecorator(target: Function): boolean;
15 /**
16 * Prepare class if inherited from service decorated class
17 * @param target Class
18 */
19 static checkInheritance(target: Function): void;
20 /**
21 * Invoked services in-memory cache
22 */
23 private instances;
24 /**
25 * Container parameters collection
26 */
27 private params;
28 /**
29 * Declared service definitions
30 */
31 private services;
32 /**
33 * Tag extractor functions
34 */
35 private autoTags;
36 /**
37 * Type factories
38 */
39 private factories;
40 /**
41 * Container-specified class definitions
42 */
43 private types;
44 /**
45 * Imported things
46 */
47 private imported;
48 /** Exception on async dependencies when invoke called synchrounosly */
49 strictAsync: boolean;
50 constructor(services?: HashMap<Service>, parameters?: HashMap<any>, autoTags?: IAutoTagger[]);
51 /**
52 * Get container parameters
53 */
54 getParameters(): HashMap<any>;
55 /**
56 * Build service from factory
57 * @param factory Factory
58 * @param _stack Stack
59 * @internal
60 */
61 private buildFromFactory;
62 /**
63 * Resolve import
64 * @param item Path or Function to import
65 * @internal
66 */
67 private resolveImport;
68 /**
69 * Resolve definition imports
70 * @param service Service definition
71 * @internal
72 */
73 private resolveImports;
74 /**
75 * Resolve (build) service definition
76 * @param definition Service definition
77 * @param _stack Resolving stack
78 * @param options Options
79 * @internal
80 */
81 private resolveDefinition;
82 private _throwError;
83 /**
84 * Build service by name
85 * @param name Service name
86 * @param stack Injection stack ( for debugging )
87 * @throws ServiceNotFoundError
88 * @internal
89 */
90 private getService;
91 /**
92 * Get service/parameters
93 * @param name Service or paremeter name
94 * @param stack Injection stack
95 * @internal
96 */
97 private _get;
98 get<T extends Function>(service: T): T | undefined;
99 get<T>(name: string): T | undefined;
100 /**
101 * Build tagged services
102 * @param tag Tag
103 */
104 getByTag<T>(tag: string): T[];
105 getByTagAsync<T>(tag: string): Promise<T[]>;
106 /**
107 * Get tagged services definitions
108 * @param tag Tag
109 */
110 findByTag(tag: string): IDefinition[];
111 /**
112 * Make an method injectable for later use
113 * @param context Method context (Class instance)
114 * @param method Method name
115 */
116 invokeLater<T extends object, M extends keyof T>(context: Constructor<T>, method: M): MockMethod<T, M>;
117 invokeLater<T extends object, M extends keyof T>(context: T, method: M): MockMethod<T, M>;
118 invokeLater<R>(callable: Injectable<R>): (...params: any[]) => R;
119 /**
120 * Make an method injectable for later use
121 * @param context Method context (Class instance)
122 * @param method Method name
123 */
124 invokeLaterAsync<T extends object, M extends keyof T>(context: Constructor<T>, method: M): MockMethod<T, M>;
125 invokeLaterAsync<T extends object, M extends keyof T>(context: T, method: M): MockMethod<T, M>;
126 invokeLaterAsync<R>(callable: Injectable<R>): (...params: any[]) => Promise<R>;
127 /**
128 * Invoke a function and resolve its dependencies
129 * @param value Invokable value
130 */
131 invoke<T>(value: Invokable<T>): T;
132 invokeAsync<T>(value: Invokable<T>): Promise<T>;
133 /**
134 *
135 * @param value Invokable value
136 * @param stack Injection stack
137 * @param options
138 */
139 private _invoke;
140 /**
141 * Set parameter value
142 * @param name Parameter name
143 * @param value Value
144 */
145 setParameter(name: string, value: any): this;
146 /**
147 * Set parameters value
148 * @param params Parameters
149 */
150 setParameters(params: {
151 [key: string]: any;
152 }): this;
153 /**
154 * Set factory for a function
155 * @param type Type
156 * @param factory Factory
157 */
158 setFactory(type: Function | string, factory: Factory): this;
159 /**
160 * Set alias for a type
161 * @param type Type
162 * @param alias Alias value
163 */
164 setAlias(type: Function, alias: any): this;
165 /**
166 * Get overrided service definition
167 * @param type Type
168 */
169 getType(type: Function): IDefinition | undefined;
170 /**
171 * Set batch services and parameters
172 * @param values Services and Parameters
173 */
174 set(values: {
175 [name: string]: IDefinition | ContainerValue;
176 }): this;
177 /**
178 * Set service/parameter
179 * @param name Service/Parameter name
180 * @param value
181 */
182 set(name: string, value: IDefinition | ContainerValue): this;
183 /**
184 * Container-scope class definition
185 * @param type Type/Class
186 * @param definition Definition
187 */
188 set(type: Function, definition: Partial<IDefinition>): this;
189 /**
190 * Get service built-in definition
191 * @param target Class/Function
192 */
193 getDefinition(target: string | IMethod | Injectable): IDefinition;
194 /**
195 * Add definition to container
196 * @param definition Definition
197 */
198 addDefinition(definition: IDefinition): this;
199 /**
200 * Get parameter value
201 * @param name Parameter name
202 */
203 getParameter(name: string): any;
204 /**
205 * Check parameter exists
206 * @param name Parameter name
207 */
208 hasParameter(name: string): boolean;
209 /**
210 * Check for service definition exist
211 * @param name Service name
212 */
213 has(name: string): boolean;
214 /**
215 * Add Auto-tagger
216 * @param tagger Auto-tagger
217 */
218 autoTag(tagger: IAutoTagger): this;
219 autoTag(base: Function, tags: string[]): this;
220 /**
221 * Declare class or function to container
222 */
223 add(...services: Function[]): this;
224 /**
225 * Get declared definitions
226 */
227 readonly definitions: HashMap<IDefinition<Function>>;
228 /**
229 * Return declared service names
230 */
231 readonly names: string[];
232 /**
233 * Get services count
234 */
235 readonly size: number;
236 [Symbol.iterator](): {
237 next(): {
238 done: boolean;
239 value: string | undefined;
240 };
241 };
242}
243/**
244 * Get injectable dependencies
245 * @param value Injectable
246 */
247export declare function getDependencies(value: Injectable | IMethod): {
248 deps: any;
249 func: Function;
250};
251/**
252 * Create new container definition
253 * @returns {IDefinition}
254 */
255export declare function Definition(definition: IDefinition): IDefinition;
256export declare function isFactory(value: any): value is Factory;
257export declare function isService(value: any): value is Service;
258export declare function isDefinition(value: any): value is IDefinition;
259export declare function Method(context: any, method: string): {
260 context: any;
261 method: string;
262};
263export declare function isMethod(value: any): value is IMethod;
264export declare function isInternalClass(value: any): boolean;