1 | import { Binding } from './binding';
|
2 | import { BindingSelector } from './binding-filter';
|
3 | import { Context } from './context';
|
4 | import { Injection, InjectionMetadata } from './inject';
|
5 | import { BoundValue, ValueOrPromise } from './value-promise';
|
6 | /**
|
7 | * A function to be executed with the resolution session
|
8 | */
|
9 | export type ResolutionAction = (session: ResolutionSession) => ValueOrPromise<BoundValue>;
|
10 | /**
|
11 | * Wrapper for bindings tracked by resolution sessions
|
12 | */
|
13 | export interface BindingElement {
|
14 | type: 'binding';
|
15 | value: Readonly<Binding>;
|
16 | }
|
17 | /**
|
18 | * Wrapper for injections tracked by resolution sessions
|
19 | */
|
20 | export interface InjectionElement {
|
21 | type: 'injection';
|
22 | value: Readonly<Injection>;
|
23 | }
|
24 | export interface InjectionDescriptor {
|
25 | targetName: string;
|
26 | bindingSelector: BindingSelector;
|
27 | metadata: InjectionMetadata;
|
28 | }
|
29 | /**
|
30 | * Binding or injection elements tracked by resolution sessions
|
31 | */
|
32 | export type ResolutionElement = BindingElement | InjectionElement;
|
33 | /**
|
34 | * Object to keep states for a session to resolve bindings and their
|
35 | * dependencies within a context
|
36 | */
|
37 | export declare class ResolutionSession {
|
38 | /**
|
39 | * A stack of bindings for the current resolution session. It's used to track
|
40 | * the path of dependency resolution and detect circular dependencies.
|
41 | */
|
42 | readonly stack: ResolutionElement[];
|
43 | /**
|
44 | * Fork the current session so that a new one with the same stack can be used
|
45 | * in parallel or future resolutions, such as multiple method arguments,
|
46 | * multiple properties, or a getter function
|
47 | * @param session - The current session
|
48 | */
|
49 | static fork(session?: ResolutionSession): ResolutionSession | undefined;
|
50 | /**
|
51 | * Run the given action with the given binding and session
|
52 | * @param action - A function to do some work with the resolution session
|
53 | * @param binding - The current binding
|
54 | * @param session - The current resolution session
|
55 | */
|
56 | static runWithBinding(action: ResolutionAction, binding: Readonly<Binding>, session?: ResolutionSession): any;
|
57 | /**
|
58 | * Run the given action with the given injection and session
|
59 | * @param action - A function to do some work with the resolution session
|
60 | * @param binding - The current injection
|
61 | * @param session - The current resolution session
|
62 | */
|
63 | static runWithInjection(action: ResolutionAction, injection: Readonly<Injection>, session?: ResolutionSession): any;
|
64 | /**
|
65 | * Describe the injection for debugging purpose
|
66 | * @param injection - Injection object
|
67 | */
|
68 | static describeInjection(injection: Readonly<Injection>): InjectionDescriptor;
|
69 | /**
|
70 | * Push the injection onto the session
|
71 | * @param injection - Injection The current injection
|
72 | */
|
73 | pushInjection(injection: Readonly<Injection>): void;
|
74 | /**
|
75 | * Pop the last injection
|
76 | */
|
77 | popInjection(): Readonly<Injection<any>>;
|
78 | /**
|
79 | * Getter for the current injection
|
80 | */
|
81 | get currentInjection(): Readonly<Injection> | undefined;
|
82 | /**
|
83 | * Getter for the current binding
|
84 | */
|
85 | get currentBinding(): Readonly<Binding> | undefined;
|
86 | /**
|
87 | * Enter the resolution of the given binding. If
|
88 | * @param binding - Binding
|
89 | */
|
90 | pushBinding(binding: Readonly<Binding>): void;
|
91 | /**
|
92 | * Exit the resolution of a binding
|
93 | */
|
94 | popBinding(): Readonly<Binding>;
|
95 | /**
|
96 | * Getter for bindings on the stack
|
97 | */
|
98 | get bindingStack(): Readonly<Binding>[];
|
99 | /**
|
100 | * Getter for injections on the stack
|
101 | */
|
102 | get injectionStack(): Readonly<Injection>[];
|
103 | /**
|
104 | * Get the binding path as `bindingA --> bindingB --> bindingC`.
|
105 | */
|
106 | getBindingPath(): string;
|
107 | /**
|
108 | * Get the injection path as `injectionA --> injectionB --> injectionC`.
|
109 | */
|
110 | getInjectionPath(): string;
|
111 | /**
|
112 | * Get the resolution path including bindings and injections, for example:
|
113 | * `bindingA --> @ClassA[0] --> bindingB --> @ClassB.prototype.prop1
|
114 | * --> bindingC`.
|
115 | */
|
116 | getResolutionPath(): string;
|
117 | toString(): string;
|
118 | }
|
119 | /**
|
120 | * Options for binding/dependency resolution
|
121 | */
|
122 | export interface ResolutionOptions {
|
123 | /**
|
124 | * A session to track bindings and injections
|
125 | */
|
126 | session?: ResolutionSession;
|
127 | /**
|
128 | * A boolean flag to indicate if the dependency is optional. If it's set to
|
129 | * `true` and the binding is not bound in a context, the resolution
|
130 | * will return `undefined` instead of throwing an error.
|
131 | */
|
132 | optional?: boolean;
|
133 | /**
|
134 | * A boolean flag to control if a proxy should be created to apply
|
135 | * interceptors for the resolved value. It's only honored for bindings backed
|
136 | * by a class.
|
137 | */
|
138 | asProxyWithInterceptors?: boolean;
|
139 | }
|
140 | /**
|
141 | * Resolution options or session
|
142 | */
|
143 | export type ResolutionOptionsOrSession = ResolutionOptions | ResolutionSession;
|
144 | /**
|
145 | * Normalize ResolutionOptionsOrSession to ResolutionOptions
|
146 | * @param optionsOrSession - resolution options or session
|
147 | */
|
148 | export declare function asResolutionOptions(optionsOrSession?: ResolutionOptionsOrSession): ResolutionOptions;
|
149 | /**
|
150 | * Contextual metadata for resolution
|
151 | */
|
152 | export interface ResolutionContext<T = unknown> {
|
153 | /**
|
154 | * The context for resolution
|
155 | */
|
156 | readonly context: Context;
|
157 | /**
|
158 | * The binding to be resolved
|
159 | */
|
160 | readonly binding: Readonly<Binding<T>>;
|
161 | /**
|
162 | * The options used for resolution
|
163 | */
|
164 | readonly options: ResolutionOptions;
|
165 | }
|
166 | /**
|
167 | * Error for context binding resolutions and dependency injections
|
168 | */
|
169 | export declare class ResolutionError extends Error {
|
170 | readonly resolutionCtx: Partial<ResolutionContext>;
|
171 | constructor(message: string, resolutionCtx: Partial<ResolutionContext>);
|
172 | private static buildDetails;
|
173 | /**
|
174 | * Build the error message for the resolution to include more contextual data
|
175 | * @param reason - Cause of the error
|
176 | * @param resolutionCtx - Resolution context
|
177 | */
|
178 | private static buildMessage;
|
179 | private static describeResolutionContext;
|
180 | }
|