UNPKG

3.66 kBTypeScriptView Raw
1import type { Make, Hooks, Swaps, Bindings, BindingKey, Constructor, ErrorCreator, BindingValues, ExtractFunctions, ContainerOptions, ContextualBindings, AbstractConstructor } from './types.js';
2/**
3 * Container resolver exposes the APIs to resolve bindings. You can think
4 * of resolver as an isolated container instance, with only the APIs
5 * to resolve bindings.
6 *
7 * ```ts
8 * const container = new Container()
9 * const resolver = container.createResolver()
10 *
11 * await resolver.make(BINDING_NAME)
12 * await resolver.make(CLASS_CONSTRUCTOR)
13 * ```
14 */
15export declare class ContainerResolver<KnownBindings extends Record<any, any>> {
16 #private;
17 constructor(container: {
18 bindings: Bindings;
19 bindingValues: BindingValues;
20 swaps: Swaps;
21 hooks: Hooks;
22 aliases: Map<Partial<keyof KnownBindings>, keyof KnownBindings | AbstractConstructor<any>>;
23 contextualBindings: Map<Constructor<any>, ContextualBindings>;
24 }, options: ContainerOptions);
25 /**
26 * Find if the resolver has a binding registered using the
27 * "bind", the "singleton", or the "bindValue" methods.
28 */
29 hasBinding<Binding extends keyof KnownBindings>(binding: Binding): boolean;
30 hasBinding(binding: BindingKey): boolean;
31 /**
32 * Find if the resolver has all the bindings registered using the
33 * "bind", the "singleton", or the "bindValue" methods.
34 */
35 hasAllBindings<Binding extends keyof KnownBindings>(bindings: Binding[]): boolean;
36 hasAllBindings(bindings: BindingKey[]): boolean;
37 /**
38 * Resolves binding in context of a parent. The method is same as
39 * the "make" method, but instead takes a parent class
40 * constructor.
41 */
42 resolveFor<Binding>(parent: unknown, binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Make<Binding>>;
43 /**
44 * Resolves the binding or constructor a class instance as follows.
45 *
46 * - Resolve the binding from the values (if registered)
47 * - Resolve the binding from the bindings (if registered)
48 * - If binding is a class, then create a instance of it. The constructor
49 * dependencies are further resolved as well.
50 * - All other values are returned as it is.
51 *
52 * ```ts
53 * await resolver.make('route')
54 * await resolver.make(Database)
55 * ```
56 */
57 make<Binding extends keyof KnownBindings>(binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Binding extends string | symbol ? KnownBindings[Binding] : Make<Binding>>;
58 make<Binding>(binding: Binding, runtimeValues?: any[], createError?: ErrorCreator): Promise<Make<Binding>>;
59 /**
60 * Call a method on an object by injecting its dependencies. The method
61 * dependencies are resolved in the same manner as a class constructor
62 * dependencies.
63 *
64 * ```ts
65 * await resolver.call(await resolver.make(UsersController), 'index')
66 * ```
67 */
68 call<Value extends Record<any, any>, Method extends ExtractFunctions<Value>>(value: Value, method: Method, runtimeValues?: any[], createError?: ErrorCreator): Promise<ReturnType<Value[Method]>>;
69 /**
70 * Register a binding as a value
71 *
72 * ```ts
73 * container.bindValue(Route, new Route())
74 * ```
75 */
76 bindValue<Binding extends keyof KnownBindings>(
77 /**
78 * Need to narrow down the "Binding" for the case where "KnownBindings" are <any, any>
79 */
80 binding: Binding extends string | symbol ? Binding : never, value: KnownBindings[Binding]): void;
81 bindValue<Binding extends AbstractConstructor<any>>(binding: Binding, value: InstanceType<Binding>): void;
82}