forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
forge-di.d.ts
206 lines (192 loc) · 6.05 KB
/
forge-di.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Type definitions for forge-di v0.9.5
// Project: https://github.com/nkohari/forge
// Definitions by: Adam Carr <https://github.com/adamcarr/>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module "forge-di" {
/**
* Implementation of the forge dependency injection manager.
*/
class Forge {
/**
* Creates a new instance
* @returns {Forge} a new instance.
*/
new(): Forge;
/**
* The bindings mapped to this forge instance.
*/
bindings: Forge.IBindingMap;
/**
* Creates a new binding.
* @param {string} name The binding name.
*/
bind(name: string): Forge.IBinding;
/**
* Unbinds then recreates a binding for this name.
* @param {string} name The binding name.
*/
rebind(name: string): Forge.IBinding;
/**
* Unbinds all bindings for this name. Returns the number of bindings removed.
* @param {string} name The binding name.
*/
unbind(name: string): number;
/**
* Get instance or instances of type registered under the provided name and optional hint.
* @param {string} name The binding name.
* @param {string} hint The binding hint.
* @param {...args} args Additional args.
*/
get<T>(name: string, hint?: string, ...args: any[]): T;
/**
* Get a single instance of type registered under the provided name and optional hint.
* @param {string} name The binding name.
* @param {string} hint The binding hint.
* @param {...args} args Additional args.
*/
getOne<T>(name: string, hint?: string, ...args: any[]): T;
/**
* Gets all instances of the type registered under the provided name.
* @param {string} name The binding name.
* @param {...args} args Additional args.
*/
getAll<T>(name: string, ...args: any[]): T | T[];
/**
* Creates an instance of the target type attempting to resolve any dependencies.
* @param {T} target The target type.
* @param {...args} args Additional args.
*/
create<T>(target: T, ...args: any[]): T;
/**
* Get all bindings registered under a binding name and optional hint.
* @param {string} name The binding name.
* @param {string} hint The binding hint.
*/
getMatchingBindings(name: string, hint?: string): Forge.IBinding[];
/**
* Returns a string that represents all bindings within this forge instance.
*/
inspect(): string;
resolve<T>(name: string, context?: Forge.IContext, hint?: string, all?: boolean, ...args: any[]): T | T[];
resolveBindings(context: Forge.IContext, bindings: Forge.IBinding[], hint: string, args: any[], unwrap: boolean): Forge.IBinding[];
}
module Forge {
interface IContext {
new (): IContext;
bindings: IBinding[];
has(binding: IBinding): boolean;
push(binding: IBinding): void;
pop(): IBinding;
toString(indent: number): string;
}
interface IType {
new (...args: any[]): any;
}
/**
* Represents arguments to help with resolving a binding.
*/
interface IBindingArguments {
[name: string]: any;
}
/**
* Represents a binding between a name, type/instance/function and optional hint.
*/
interface IBinding {
/** The forge that contains this binding. */
forge: Forge;
/** The binding name. */
name: string;
/** Alias mapping to this binding. */
to: IBinding;
/** Alias mapping to this binding. */
as: IBinding;
/** Whether or not this binding is currently resolving. */
isResolving: boolean;
/** The resolver for this binding. */
resolver: IResolver;
/** The lifecycle associated with this binding. Defaults to singleton. */
lifecycle: ILifecycle;
/** The predicate associated with this binding. Used to support hints. */
predicate: IPredicate;
/** The additional binding arguments to help resolve dependencies. */
arguments: IBindingArguments;
/**
* Checks whether or not this binding matches the hint by executing the predicate.
* @param {string} hint The hint to check against.
*/
matches(hint: string): boolean;
/**
* Registers a type to a binding. This type must have a constructor.
* @param {T} target The target type.
*/
type<T extends IType>(target: T): IBinding;
/**
* Registers a type to a binding. This must be a callable function.
* @param {T} target The target function.
*/
function<T extends{(...args: any[]):any}>(target: T): IBinding;
/**
* Registeres an instance to a binding. This instance will always be returned.
* @param {T} target The target instance.
*/
instance<T extends Object>(target: T): IBinding;
/**
* Configures this binding lifecycle as a singleton. This is the default lifecycle.
*/
singleton(): IBinding;
/**
* Configures this binding lifecycle as transient.
* New instances will be created, if this is a type based binding, on each get.
*/
transient(): IBinding;
/**
* Registers a predicate for this binding.
* @param {IPredicate} predicate The predicate.
*/
when(predicate:IPredicate): IBinding;
/**
* Registers a hint for this binding.
* @param {string} hint The hint.
*/
when(hint: string): IBinding;
/**
* Registers additional binding arguments to help with resolving.
* @param {IBindingArguments} args The additional binding arguments.
*/
with(args: IBindingArguments): IBinding;
/**
* Returns a string representing this binding.
*/
toString(): string;
}
/** Represents a binding map. */
interface IBindingMap {
/** Gets a binding by name. */
[name: string]: IBinding[];
}
/** Represents a predicate. */
interface IPredicate {
/**
* Returns whether or not the hit satisfies this predicate.
* @param {string} hint The hint to check against.
*/
(hint: string): boolean;
}
/** Represents a resolver. */
interface IResolver {
/**
* Resolves a specific type.
*/
resolve<T>(): T;
}
/** Represents a binding lifecycle. */
interface ILifecycle {
/**
* Returns the instance from a resolver based on the configured lifecycle.
* @param {IResolver} resolver The type resolver.
*/
getInstance<T>(resolver: IResolver): T;
}
}
export = Forge;
}