-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.ts
268 lines (235 loc) · 6.54 KB
/
types.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { z } from 'zod';
/**
* Represents a type that can be bound to the DI container.
* This can include strings, symbols, functions, or objects.
*/
export type Bindable = string | symbol | Function | object;
/**
* Enum representing the different lifetimes for bindings in the DI container.
*/
export enum Lifetime {
/**
* Singleton lifetime: A single instance is created and shared across all requests.
*/
Singleton,
/**
* Transient lifetime: A new instance is created each time it is requested.
*/
Transient,
/**
* Scoped lifetime: An instance is created per scope (e.g., per request).
*/
Scoped,
}
// Remove or comment out this line:
// export type { Lifetime as LifetimeEnum };
/**
* Interface representing a binding in the DI container.
*/
export interface Binding {
/**
* The factory function used to create the instance.
*/
factory: Function;
/**
* The lifetime of the binding, determining how instances are managed.
*/
lifetime: Lifetime;
/**
* The instance created by the factory, if applicable (for Singleton and Scoped lifetimes).
*/
instance?: any;
/**
* The resolver function used to resolve the binding.
*/
resolver: Function;
}
/**
* Interface representing a contextual binding in the DI container.
*/
export interface ContextualBinding {
/**
* The concrete type that this binding is associated with.
*/
when: Bindable;
/**
* The dependency that this binding provides.
*/
need: Bindable;
/**
* The value or factory function that provides the dependency.
*/
give: Function | any;
}
/**
* Interface for classes that have parameter types defined.
*/
export interface WithParamTypes {
/**
* An optional array of parameter types for the class constructor.
*/
paramTypes?: any[];
}
/**
* Type representing a middleware function in the DI container.
* Middleware can intercept and modify the resolution process.
*
* @param next - A function to call the next middleware in the chain.
* @returns The result of the middleware processing.
*/
export type Middleware = (next: () => any) => any;
/**
* Interface for the Dependency Injection Container.
*/
export interface IContainer {
/**
* Bind a type to a factory function.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @param schema - Optional Zod schema for validation.
* @returns The IContainer instance.
*/
bind(abstract: Bindable, factory: Function, schema?: z.ZodType<any>): this;
/**
* Bind a singleton.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @returns The IContainer instance.
*/
singleton(abstract: Bindable, factory: Function): this;
/**
* Bind an instance.
*
* @param abstract - The abstract type to bind.
* @param instance - The instance to bind.
* @returns The IContainer instance.
*/
instance(abstract: Bindable, instance: any): this;
/**
* Alias an abstract type.
*
* @param alias - The alias to create.
* @param abstract - The abstract type to alias.
*/
alias(alias: string | symbol, abstract: Bindable): void;
/**
* Tag multiple abstracts with a tag.
*
* @param abstracts - The abstracts to tag.
* @param tag - The tag to assign.
* @returns The IContainer instance.
*/
tag(abstracts: Bindable[], tag: string): this;
/**
* Create an instance of a class with dependencies.
*
* @param Target - The class to instantiate.
* @returns The created instance.
*/
createInstance<T>(Target: Bindable & WithParamTypes): T;
/**
* Resolve a type.
*
* @param abstract - The abstract type to resolve.
* @param context - Optional context for contextual bindings.
* @returns The resolved instance.
*/
resolve<T = any>(abstract: Bindable, context?: Bindable): T;
/**
* Resolve a type asynchronously.
*
* @param abstract - The abstract type to resolve.
* @param context - Optional context for contextual bindings.
* @returns A promise that resolves to the instance.
*/
resolveAsync<T = any>(abstract: Bindable, context?: Bindable): Promise<T>;
/**
* Bind a transient.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @returns The IContainer instance.
*/
transient(abstract: Bindable, factory: Function): this;
/**
* Bind a scoped instance.
*
* @param abstract - The abstract type to bind.
* @param factory - The factory function to create the instance.
* @returns The IContainer instance.
*/
scoped(abstract: Bindable, factory: Function): this;
/**
* Create a new scope.
*
* @returns A new IContainer instance representing the scope.
*/
createScope(): IContainer;
/**
* Dispose of the container and its bindings.
*/
dispose(): void;
/**
* Create a child container.
*
* @returns A new IContainer instance representing the child container.
*/
createChild(): IContainer;
/**
* Lazy bind a type to a factory function.
*
* @param token - The token to bind.
* @param factory - The factory function to create the instance.
*/
lazyBind<T>(token: any, factory: () => T): void;
/**
* Use a middleware function.
*
* @param middleware - The middleware function to use.
*/
use(middleware: Middleware): void;
}
/**
* Interface for the Contextual Binding Builder.
*/
export interface IContextualBindingBuilder {
/**
* Define the dependency that the concrete type needs.
*
* @param need - The dependency that the concrete type needs.
* @returns A builder for defining the contextual binding.
*/
needs(need: Bindable): IContextualBindingNeedsBuilder;
}
/**
* Interface for the Contextual Binding Needs Builder.
*/
export interface IContextualBindingNeedsBuilder {
/**
* Define the value or factory function for the contextual binding.
*
* @param give - The value or factory function to provide.
*/
give(give: Function | any): void;
}
/**
* Interface for the Observer.
*/
export interface IObserver {
/**
* Subscribe to an event.
*
* @param event - The event to subscribe to.
* @param callback - The callback function to call when the event is published.
*/
subscribe(event: string, callback: Function): void;
/**
* Publish an event.
*
* @param event - The event to publish.
* @param data - Optional data to pass to the event listeners.
*/
publish(event: string, data?: any): void;
}