UNPKG

6.74 kBTypeScriptView Raw
1import { IocContract, BindCallback, FakeCallback, LookupNode } from '../Contracts';
2import { IocResolver } from '../Resolver';
3export declare class Ioc implements IocContract {
4 private fakes;
5 private bindings;
6 private injector;
7 private aliases;
8 /**
9 * The current state of using proxies
10 */
11 private usingProxies;
12 /**
13 * A custom method to trap `ioc.use` and `ioc.make` statements
14 */
15 private trapCallback;
16 /**
17 * Define the module type for resolving auto import aliases. Defaults
18 * to `cjs`
19 */
20 module: 'cjs' | 'esm';
21 /**
22 * Registered aliases. The key is the alias and value is the
23 * absolute directory path
24 */
25 get importAliases(): IocContract['importAliases'];
26 /**
27 * Detect if the module export value is an esm module
28 */
29 private isEsm;
30 /**
31 * Wraps object and class to a proxy to enable the fakes
32 * API
33 */
34 private wrapAsProxy;
35 /**
36 * Wrap value inside proxy by also inspecting for esm
37 * default exports
38 */
39 private wrapEsmModuleAsProxy;
40 /**
41 * Makes an instance of a class by injecting dependencies
42 */
43 private makeRaw;
44 /**
45 * Makes an instance of a class asynchronously by injecting dependencies
46 */
47 private makeRawAsync;
48 /**
49 * Enable/disable proxies. Proxies are mainly required for fakes to
50 * work
51 */
52 useProxies(enable?: boolean): this;
53 /**
54 * Register a binding with a callback. The callback return value will be
55 * used when binding is resolved
56 */
57 bind(binding: string, callback: BindCallback<any, this>): this;
58 /**
59 * Same as the [[bind]] method, but registers a singleton only. Singleton's callback
60 * is invoked only for the first time and then the cached value is used
61 */
62 singleton(binding: string, callback: BindCallback<any, this>): this;
63 /**
64 * Define an import alias
65 */
66 alias(absolutePath: string, alias: string): this;
67 /**
68 * Register a fake for a namespace. Fakes works both for "bindings" and "import aliases".
69 * Fakes only work when proxies are enabled using "useProxies".
70 */
71 fake(namespace: string, callback: FakeCallback<any, this>): this;
72 /**
73 * Clear selected or all the fakes. Calling the method with no arguments
74 * will clear all the fakes
75 */
76 restore(namespace?: string): this;
77 /**
78 * Find if a fake has been registered for a given namespace
79 */
80 hasFake(namespace: string): boolean;
81 /**
82 * Find if a binding exists for a given namespace
83 */
84 hasBinding(namespace: string): boolean;
85 /**
86 * Find if a namespace is part of the auto import aliases. Returns false, when namespace
87 * is an alias path but has an explicit binding too
88 */
89 isAliasPath(namespace: string): boolean;
90 /**
91 * Lookup a namespace. The output contains the complete namespace,
92 * along with its type. The type is an "alias" or a "binding".
93 *
94 * Null is returned when unable to lookup the namespace inside the container
95 *
96 * Note: This method just checks if a namespace is registered or binding
97 * or can be it resolved from auto import aliases or not. However,
98 * it doesn't check for the module existence on the disk.
99 *
100 * Optionally you can define a prefix namespace
101 * to be used to build the complete namespace. For example:
102 *
103 * - namespace: UsersController
104 * - prefixNamespace: App/Controllers/Http
105 * - Output: App/Controllers/Http/UsersController
106 *
107 * Prefix namespace is ignored for absolute namespaces. For example:
108 *
109 * - namespace: /App/UsersController
110 * - prefixNamespace: App/Controllers/Http
111 * - Output: App/UsersController
112 */
113 lookup(namespace: string | LookupNode<string>, prefixNamespace?: string): null | any;
114 /**
115 * Same as [[lookup]]. But raises exception instead of returning null
116 */
117 lookupOrFail(namespace: string | LookupNode<string>, prefixNamespace?: string): LookupNode<string>;
118 /**
119 * Resolve a binding by invoking the binding factory function. An exception
120 * is raised, if the binding namespace is unregistered.
121 */
122 resolveBinding(binding: string): any;
123 /**
124 * Import namespace from the auto import aliases. This method assumes you are
125 * using native ES modules
126 */
127 import(namespace: string): Promise<any>;
128 /**
129 * Same as the "import" method, but uses CJS for requiring the module from its
130 * path
131 */
132 require(namespace: string): any;
133 /**
134 * The use method looks up a namespace inside both the bindings and the
135 * auto import aliases
136 */
137 use(namespace: string | LookupNode<string>): any;
138 /**
139 * Same as the [[use]] method, but instead uses ES modules for resolving
140 * the auto import aliases
141 */
142 useAsync(namespace: string | LookupNode<string>): Promise<any>;
143 /**
144 * Makes an instance of the class by first resolving it.
145 */
146 make(namespace: LookupNode<string> | any, args?: any[]): any;
147 /**
148 * Same as the [[make]] method, but instead uses ES modules for resolving
149 * the auto import aliases
150 */
151 makeAsync(namespace: LookupNode<string> | any, args?: any[]): Promise<any>;
152 /**
153 * Define a callback to be called when all of the container
154 * bindings are available.
155 *
156 * Note: This method is exclusive for bindings and doesn't resolve
157 * auto import aliases
158 */
159 withBindings(namespaces: readonly any[], cb: (...args: any) => void): void;
160 /**
161 * @deprecated: Use "withBindings" instead
162 */
163 with(namespaces: readonly any[], cb: (...args: any) => void): void;
164 /**
165 * Call method on an object and automatically resolve its depdencies
166 */
167 call(target: any, method: any, args?: any[]): any;
168 /**
169 * Same as [[call]], but uses ES modules for resolving the auto
170 * import aliases
171 */
172 callAsync(target: any, method: any, args?: any[]): Promise<any>;
173 /**
174 * Trap container lookup calls. It includes
175 *
176 * - Ioc.use
177 * - Ioc.useAsync
178 * - Ioc.make
179 * - Ioc.makeAsync
180 * - Ioc.require
181 * - Ioc.import
182 * - Ioc.resolveBinding
183 */
184 trap(callback: (namespace: string) => any): this;
185 /**
186 * Returns the resolver instance to resolve Ioc container bindings with
187 * little ease. Since, the IocResolver uses an in-memory cache to
188 * improve the lookup speed, we suggest keeping a reference to
189 * the output of this method to leverage caching
190 */
191 getResolver(fallbackMethod?: string, rcNamespaceKey?: string, fallbackNamespace?: string): IocResolver;
192}