ditox 3.2.0 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -27
- package/dist/index.cjs +531 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{umd/index.d.ts → index.d.cts} +191 -196
- package/dist/{browser/index.d.ts → index.d.ts} +191 -196
- package/dist/index.js +514 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +537 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +22 -20
- package/src/container.test.ts +77 -33
- package/src/container.ts +26 -11
- package/src/index.ts +22 -24
- package/src/modules.test.ts +190 -37
- package/src/modules.ts +22 -5
- package/src/removal-compatibility.test.ts +259 -0
- package/src/removal.test.ts +190 -0
- package/src/tokens.test.ts +9 -8
- package/src/tokens.ts +2 -2
- package/src/utils.test.ts +18 -17
- package/src/utils.ts +21 -21
- package/dist/browser/index.js +0 -2
- package/dist/browser/index.js.map +0 -1
- package/dist/cjs/container.d.ts +0 -93
- package/dist/cjs/container.js +0 -216
- package/dist/cjs/container.js.map +0 -1
- package/dist/cjs/index.d.ts +0 -7
- package/dist/cjs/index.js +0 -24
- package/dist/cjs/index.js.map +0 -1
- package/dist/cjs/modules.d.ts +0 -121
- package/dist/cjs/modules.js +0 -143
- package/dist/cjs/modules.js.map +0 -1
- package/dist/cjs/tokens.d.ts +0 -56
- package/dist/cjs/tokens.js +0 -20
- package/dist/cjs/tokens.js.map +0 -1
- package/dist/cjs/utils.d.ts +0 -137
- package/dist/cjs/utils.js +0 -156
- package/dist/cjs/utils.js.map +0 -1
- package/dist/esm/container.d.ts +0 -93
- package/dist/esm/container.js +0 -211
- package/dist/esm/container.js.map +0 -1
- package/dist/esm/index.d.ts +0 -7
- package/dist/esm/index.js +0 -5
- package/dist/esm/index.js.map +0 -1
- package/dist/esm/modules.d.ts +0 -121
- package/dist/esm/modules.js +0 -136
- package/dist/esm/modules.js.map +0 -1
- package/dist/esm/tokens.d.ts +0 -56
- package/dist/esm/tokens.js +0 -15
- package/dist/esm/tokens.js.map +0 -1
- package/dist/esm/utils.d.ts +0 -137
- package/dist/esm/utils.js +0 -145
- package/dist/esm/utils.js.map +0 -1
- package/dist/umd/index.js +0 -2
- package/dist/umd/index.js.map +0 -1
|
@@ -1,21 +1,22 @@
|
|
|
1
|
+
//#region src/tokens.d.ts
|
|
1
2
|
/**
|
|
2
3
|
* @ignore
|
|
3
4
|
* Binding token for mandatory value
|
|
4
5
|
*/
|
|
5
6
|
type RequiredToken<T> = {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
symbol: symbol;
|
|
8
|
+
type?: T;
|
|
9
|
+
isOptional?: false;
|
|
9
10
|
};
|
|
10
11
|
/**
|
|
11
12
|
* @ignore
|
|
12
13
|
* Binding token for optional value
|
|
13
14
|
*/
|
|
14
15
|
type OptionalToken<T> = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
symbol: symbol;
|
|
17
|
+
type?: T;
|
|
18
|
+
isOptional: true;
|
|
19
|
+
optionalValue: T;
|
|
19
20
|
};
|
|
20
21
|
/**
|
|
21
22
|
* Binding token
|
|
@@ -25,17 +26,14 @@ type Token<T> = RequiredToken<T> | OptionalToken<T>;
|
|
|
25
26
|
* Token options
|
|
26
27
|
*/
|
|
27
28
|
type TokenOptions = {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
description?: undefined;
|
|
29
|
+
/**
|
|
30
|
+
* Key for token's symbol. It allows to create shareable tokens.
|
|
31
|
+
*/
|
|
32
|
+
key: string; /** @ignore */
|
|
33
|
+
description?: undefined;
|
|
34
34
|
} | {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
/** @ignore */
|
|
38
|
-
key?: undefined;
|
|
35
|
+
/** Description for better error messages */description?: string; /** @ignore */
|
|
36
|
+
key?: undefined;
|
|
39
37
|
};
|
|
40
38
|
/**
|
|
41
39
|
* Creates a new binding token.
|
|
@@ -45,7 +43,8 @@ declare function token<T>(description?: string): Token<T>;
|
|
|
45
43
|
/**
|
|
46
44
|
* Creates a new binding token.
|
|
47
45
|
* @param options - Token description for better error messages.
|
|
48
|
-
*/
|
|
46
|
+
*/
|
|
47
|
+
declare function token<T>(options?: TokenOptions): Token<T>;
|
|
49
48
|
/**
|
|
50
49
|
* Decorate a token with an optional value.
|
|
51
50
|
* This value is be used as default value in case a container does not have registered token.
|
|
@@ -54,12 +53,13 @@ declare function token<T>(description?: string): Token<T>;
|
|
|
54
53
|
*/
|
|
55
54
|
declare function optional<T>(token: Token<T>, optionalValue: T): OptionalToken<T>;
|
|
56
55
|
declare function optional<T>(token: Token<T>): OptionalToken<T | undefined>;
|
|
57
|
-
|
|
56
|
+
//#endregion
|
|
57
|
+
//#region src/container.d.ts
|
|
58
58
|
/**
|
|
59
59
|
* ResolverError is thrown by the resolver when a token is not found in a container.
|
|
60
60
|
*/
|
|
61
61
|
declare class ResolverError extends Error {
|
|
62
|
-
|
|
62
|
+
constructor(message: string);
|
|
63
63
|
}
|
|
64
64
|
/**
|
|
65
65
|
* @see https://github.com/mnasyrov/ditox#factory-lifetimes
|
|
@@ -76,43 +76,45 @@ type FactoryScope = 'scoped' | 'singleton' | 'transient';
|
|
|
76
76
|
* `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
|
|
77
77
|
*/
|
|
78
78
|
type FactoryOptions<T> = {
|
|
79
|
-
|
|
80
|
-
|
|
79
|
+
scope?: 'scoped' | 'singleton';
|
|
80
|
+
onRemoved?: (value: T) => void;
|
|
81
81
|
} | {
|
|
82
|
-
|
|
82
|
+
scope: 'transient';
|
|
83
83
|
};
|
|
84
84
|
/**
|
|
85
85
|
* Dependency container.
|
|
86
86
|
*/
|
|
87
87
|
type Container = {
|
|
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
|
-
|
|
88
|
+
/**
|
|
89
|
+
* Binds a value for the token
|
|
90
|
+
*/
|
|
91
|
+
bindValue<T>(token: Token<T>, value: T): void;
|
|
92
|
+
/**
|
|
93
|
+
* Binds a factory for the token.
|
|
94
|
+
*/
|
|
95
|
+
bindFactory<T>(token: Token<T>, factory: (container: Container) => T, options?: FactoryOptions<T>): void;
|
|
96
|
+
/**
|
|
97
|
+
* Checks if the token is registered in the container hierarchy.
|
|
98
|
+
*/
|
|
99
|
+
hasToken(token: Token<unknown>): boolean;
|
|
100
|
+
/**
|
|
101
|
+
* Returns a resolved value by the token, or returns `undefined` in case the token is not found.
|
|
102
|
+
*/
|
|
103
|
+
get<T>(token: Token<T>): T | undefined;
|
|
104
|
+
/**
|
|
105
|
+
* Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
|
|
106
|
+
*/
|
|
107
|
+
resolve<T>(token: Token<T>): T;
|
|
108
|
+
/**
|
|
109
|
+
* Removes a binding for the token.
|
|
110
|
+
*/
|
|
111
|
+
remove<T>(token: Token<T>): void;
|
|
112
|
+
/**
|
|
113
|
+
* Removes all bindings in the container.
|
|
114
|
+
* Continues after cleanup failures and rethrows the first thrown value unchanged
|
|
115
|
+
* after clearing bindings and restoring internal tokens. Later failures are discarded.
|
|
116
|
+
*/
|
|
117
|
+
removeAll(): void;
|
|
116
118
|
};
|
|
117
119
|
/**
|
|
118
120
|
* A subset of Container interface that provides read-only access to dependency resolution.
|
|
@@ -127,13 +129,131 @@ type ContainerResolver = Pick<Container, 'hasToken' | 'get' | 'resolve'>;
|
|
|
127
129
|
* @param parentArg - Optional parent container or an array of containers.
|
|
128
130
|
*/
|
|
129
131
|
declare function createContainer(parentArg?: ContainerResolver | ReadonlyArray<ContainerResolver>): Container;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
132
|
+
//#endregion
|
|
133
|
+
//#region src/modules.d.ts
|
|
134
|
+
type AnyObject = Record<string, any>;
|
|
135
|
+
type EmptyObject = Record<string, never>;
|
|
136
|
+
type ModuleController = {
|
|
137
|
+
/** Dispose the module and clean its resources */destroy?: () => void;
|
|
133
138
|
};
|
|
134
|
-
|
|
135
|
-
|
|
139
|
+
/**
|
|
140
|
+
* Dependency module
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* type LoggerModule = Module<{
|
|
145
|
+
* logger: Logger;
|
|
146
|
+
* }>;
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
150
|
+
type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
151
|
+
/**
|
|
152
|
+
* Description how to bind the module in declarative way.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
157
|
+
* token: LOGGER_MODULE_TOKEN,
|
|
158
|
+
* factory: (container) => {
|
|
159
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
160
|
+
* return {
|
|
161
|
+
* logger: { log: (message) => transport.write(message) },
|
|
162
|
+
* destroy: () => transport.close(),
|
|
163
|
+
* }
|
|
164
|
+
* },
|
|
165
|
+
* exports: {
|
|
166
|
+
* logger: LOGGER_TOKEN,
|
|
167
|
+
* },
|
|
168
|
+
* };
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
172
|
+
/** Token for the module */token: Token<T>; /** Modules for binding */
|
|
173
|
+
imports?: ReadonlyArray<ModuleBindingEntry>; /** Factory of the module */
|
|
174
|
+
factory: (container: Container) => T; /** Dictionary of module properties which are bound to tokens. */
|
|
175
|
+
exports?: { [K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]> }; /** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
176
|
+
beforeBinding?: (container: Container) => void; /** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
177
|
+
afterBinding?: (container: Container) => void;
|
|
178
|
+
/**
|
|
179
|
+
* Strategy for executing the factory:
|
|
180
|
+
* - `lazy` - **This is the default**. The factory is called when the module is resolved.
|
|
181
|
+
* - `eager` - The factory is called immediately after the module is bound to the container.
|
|
182
|
+
*/
|
|
183
|
+
strategy?: 'eager' | 'lazy';
|
|
184
|
+
};
|
|
185
|
+
type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
|
|
186
|
+
/**
|
|
187
|
+
* Options for module binding.
|
|
188
|
+
*
|
|
189
|
+
* `scope` types:
|
|
190
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
191
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
192
|
+
*/
|
|
193
|
+
type BindModuleOptions = {
|
|
194
|
+
scope?: 'scoped' | 'singleton';
|
|
195
|
+
};
|
|
196
|
+
type ModuleDeclarationWithOptions = {
|
|
197
|
+
module: ModuleDeclaration<AnyObject>;
|
|
198
|
+
options: BindModuleOptions;
|
|
136
199
|
};
|
|
200
|
+
type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
201
|
+
/**
|
|
202
|
+
* Binds the dependency module to the container
|
|
203
|
+
* @param container - Dependency container.
|
|
204
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
205
|
+
* @param options - Options for module binding.
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* ```ts
|
|
209
|
+
* bindModule(container, LOGGER_MODULE);
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
213
|
+
/**
|
|
214
|
+
* Binds dependency modules to the container
|
|
215
|
+
*
|
|
216
|
+
* @param container - Dependency container for binding
|
|
217
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
218
|
+
*/
|
|
219
|
+
declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
220
|
+
/**
|
|
221
|
+
* Declares a module binding
|
|
222
|
+
*
|
|
223
|
+
* @param declaration - a module declaration
|
|
224
|
+
* @param declaration.token - optional field
|
|
225
|
+
*
|
|
226
|
+
* @example
|
|
227
|
+
* ```ts
|
|
228
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
229
|
+
* factory: (container) => {
|
|
230
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
231
|
+
* return {
|
|
232
|
+
* logger: { log: (message) => transport.write(message) },
|
|
233
|
+
* destroy: () => transport.close(),
|
|
234
|
+
* }
|
|
235
|
+
* },
|
|
236
|
+
* exports: {
|
|
237
|
+
* logger: LOGGER_TOKEN,
|
|
238
|
+
* },
|
|
239
|
+
* });
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
declare function declareModule<T extends Module<AnyObject>>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
243
|
+
/**
|
|
244
|
+
* @deprecated Use `declareModule` instead
|
|
245
|
+
*
|
|
246
|
+
* Declares bindings of several modules
|
|
247
|
+
*
|
|
248
|
+
* @param modules - module declaration entries
|
|
249
|
+
*/
|
|
250
|
+
declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/utils.d.ts
|
|
253
|
+
type ValuesProps = {
|
|
254
|
+
[key: string]: unknown;
|
|
255
|
+
};
|
|
256
|
+
type TokenProps<Props extends ValuesProps> = { [K in keyof Props]: Token<Props[K]> };
|
|
137
257
|
/**
|
|
138
258
|
* Checks if a value is the token
|
|
139
259
|
*/
|
|
@@ -163,8 +283,8 @@ declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyAr
|
|
|
163
283
|
* ```
|
|
164
284
|
*/
|
|
165
285
|
declare function tryResolveValue<Tokens extends Token<unknown> | {
|
|
166
|
-
|
|
167
|
-
}, Values extends Tokens extends Token<infer V> ? V | undefined : Tokens extends TokenProps<infer Props> ? Partial<Props> : never>(container: Container, token: Tokens): Values;
|
|
286
|
+
[key: string]: Token<unknown>;
|
|
287
|
+
}, Values extends (Tokens extends Token<infer V> ? V | undefined : Tokens extends TokenProps<infer Props> ? Partial<Props> : never)>(container: Container, token: Tokens): Values;
|
|
168
288
|
/**
|
|
169
289
|
* Returns an array of resolved values or objects with resolved values.
|
|
170
290
|
*
|
|
@@ -183,10 +303,8 @@ declare function tryResolveValue<Tokens extends Token<unknown> | {
|
|
|
183
303
|
* ```
|
|
184
304
|
*/
|
|
185
305
|
declare function tryResolveValues<Tokens extends (Token<unknown> | {
|
|
186
|
-
|
|
187
|
-
})[], Values extends {
|
|
188
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V | undefined : Tokens[K] extends TokenProps<infer Props> ? Partial<Props> : never;
|
|
189
|
-
}>(container: Container, ...tokens: Tokens): Values;
|
|
306
|
+
[key: string]: Token<unknown>;
|
|
307
|
+
})[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V | undefined : Tokens[K] extends TokenProps<infer Props> ? Partial<Props> : never }>(container: Container, ...tokens: Tokens): Values;
|
|
190
308
|
/**
|
|
191
309
|
* Resolves a value by the provided token.
|
|
192
310
|
*
|
|
@@ -205,8 +323,8 @@ declare function tryResolveValues<Tokens extends (Token<unknown> | {
|
|
|
205
323
|
* ```
|
|
206
324
|
*/
|
|
207
325
|
declare function resolveValue<Tokens extends Token<unknown> | {
|
|
208
|
-
|
|
209
|
-
}, Values extends Tokens extends Token<infer V> ? V : Tokens extends TokenProps<infer Props> ? Props : never>(container: Container, token: Tokens): Values;
|
|
326
|
+
[key: string]: Token<unknown>;
|
|
327
|
+
}, Values extends (Tokens extends Token<infer V> ? V : Tokens extends TokenProps<infer Props> ? Props : never)>(container: Container, token: Tokens): Values;
|
|
210
328
|
/**
|
|
211
329
|
* Returns an array of resolved values or objects with resolved values.
|
|
212
330
|
*
|
|
@@ -225,10 +343,8 @@ declare function resolveValue<Tokens extends Token<unknown> | {
|
|
|
225
343
|
* ```
|
|
226
344
|
*/
|
|
227
345
|
declare function resolveValues<Tokens extends (Token<unknown> | {
|
|
228
|
-
|
|
229
|
-
})[], Values extends {
|
|
230
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
|
|
231
|
-
}>(container: Container, ...tokens: Tokens): Values;
|
|
346
|
+
[key: string]: Token<unknown>;
|
|
347
|
+
})[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never }>(container: Container, ...tokens: Tokens): Values;
|
|
232
348
|
/**
|
|
233
349
|
* Decorates a factory by passing resolved values as factory arguments.
|
|
234
350
|
*
|
|
@@ -241,10 +357,8 @@ declare function resolveValues<Tokens extends (Token<unknown> | {
|
|
|
241
357
|
* @return Decorated factory which takes a dependency container as a single argument.
|
|
242
358
|
*/
|
|
243
359
|
declare function injectable<Tokens extends (Token<unknown> | {
|
|
244
|
-
|
|
245
|
-
})[], Values extends {
|
|
246
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
|
|
247
|
-
}, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
|
|
360
|
+
[key: string]: Token<unknown>;
|
|
361
|
+
})[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
|
|
248
362
|
/**
|
|
249
363
|
* Decorates a class by passing resolved values as arguments to its constructor.
|
|
250
364
|
*
|
|
@@ -258,127 +372,8 @@ declare function injectable<Tokens extends (Token<unknown> | {
|
|
|
258
372
|
* and returns a new created class.
|
|
259
373
|
*/
|
|
260
374
|
declare function injectableClass<Tokens extends (Token<unknown> | {
|
|
261
|
-
|
|
262
|
-
})[], Values extends {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
type AnyObject = Record<string, any>;
|
|
267
|
-
type EmptyObject = Record<string, never>;
|
|
268
|
-
type ModuleController = {
|
|
269
|
-
/** Dispose the module and clean its resources */
|
|
270
|
-
destroy?: () => void;
|
|
271
|
-
};
|
|
272
|
-
/**
|
|
273
|
-
* Dependency module
|
|
274
|
-
*
|
|
275
|
-
* @example
|
|
276
|
-
* ```ts
|
|
277
|
-
* type LoggerModule = Module<{
|
|
278
|
-
* logger: Logger;
|
|
279
|
-
* }>;
|
|
280
|
-
* ```
|
|
281
|
-
*/
|
|
282
|
-
type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
283
|
-
type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
284
|
-
/**
|
|
285
|
-
* Description how to bind the module in declarative way.
|
|
286
|
-
*
|
|
287
|
-
* @example
|
|
288
|
-
* ```ts
|
|
289
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
290
|
-
* token: LOGGER_MODULE_TOKEN,
|
|
291
|
-
* factory: (container) => {
|
|
292
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
293
|
-
* return {
|
|
294
|
-
* logger: { log: (message) => transport.write(message) },
|
|
295
|
-
* destroy: () => transport.close(),
|
|
296
|
-
* }
|
|
297
|
-
* },
|
|
298
|
-
* exports: {
|
|
299
|
-
* logger: LOGGER_TOKEN,
|
|
300
|
-
* },
|
|
301
|
-
* };
|
|
302
|
-
* ```
|
|
303
|
-
*/
|
|
304
|
-
type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
305
|
-
/** Token for the module */
|
|
306
|
-
token: Token<T>;
|
|
307
|
-
/** Modules for binding */
|
|
308
|
-
imports?: ReadonlyArray<ModuleBindingEntry>;
|
|
309
|
-
/** Factory of the module */
|
|
310
|
-
factory: (container: Container) => T;
|
|
311
|
-
/** Dictionary of module properties which are bound to tokens. */
|
|
312
|
-
exports?: {
|
|
313
|
-
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
314
|
-
};
|
|
315
|
-
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
316
|
-
beforeBinding?: (container: Container) => void;
|
|
317
|
-
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
318
|
-
afterBinding?: (container: Container) => void;
|
|
319
|
-
};
|
|
320
|
-
/**
|
|
321
|
-
* Options for module binding.
|
|
322
|
-
*
|
|
323
|
-
* `scope` types:
|
|
324
|
-
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
325
|
-
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
326
|
-
*/
|
|
327
|
-
type BindModuleOptions = {
|
|
328
|
-
scope?: 'scoped' | 'singleton';
|
|
329
|
-
};
|
|
330
|
-
type ModuleDeclarationWithOptions = {
|
|
331
|
-
module: ModuleDeclaration<AnyObject>;
|
|
332
|
-
options: BindModuleOptions;
|
|
333
|
-
};
|
|
334
|
-
type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
335
|
-
/**
|
|
336
|
-
* Binds the dependency module to the container
|
|
337
|
-
* @param container - Dependency container.
|
|
338
|
-
* @param moduleDeclaration - Declaration of the dependency module.
|
|
339
|
-
* @param options - Options for module binding.
|
|
340
|
-
*
|
|
341
|
-
* @example
|
|
342
|
-
* ```ts
|
|
343
|
-
* bindModule(container, LOGGER_MODULE);
|
|
344
|
-
* ```
|
|
345
|
-
*/
|
|
346
|
-
declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
347
|
-
/**
|
|
348
|
-
* Binds dependency modules to the container
|
|
349
|
-
*
|
|
350
|
-
* @param container - Dependency container for binding
|
|
351
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
352
|
-
*/
|
|
353
|
-
declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
354
|
-
/**
|
|
355
|
-
* Declares a module binding
|
|
356
|
-
*
|
|
357
|
-
* @param declaration - a module declaration
|
|
358
|
-
* @param declaration.token - optional field
|
|
359
|
-
*
|
|
360
|
-
* @example
|
|
361
|
-
* ```ts
|
|
362
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
363
|
-
* factory: (container) => {
|
|
364
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
365
|
-
* return {
|
|
366
|
-
* logger: { log: (message) => transport.write(message) },
|
|
367
|
-
* destroy: () => transport.close(),
|
|
368
|
-
* }
|
|
369
|
-
* },
|
|
370
|
-
* exports: {
|
|
371
|
-
* logger: LOGGER_TOKEN,
|
|
372
|
-
* },
|
|
373
|
-
* });
|
|
374
|
-
* ```
|
|
375
|
-
*/
|
|
376
|
-
declare function declareModule<T extends Module<AnyObject>>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
377
|
-
/**
|
|
378
|
-
* Declares bindings of several modules
|
|
379
|
-
*
|
|
380
|
-
* @param modules - module declaration entries
|
|
381
|
-
*/
|
|
382
|
-
declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
383
|
-
|
|
384
|
-
export { BindModuleOptions, Container, ContainerResolver, FactoryOptions, FactoryScope, Module, ModuleBindingEntry, ModuleDeclaration, OptionalToken, RequiredToken, ResolverError, Token, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, injectable, injectableClass, isToken, optional, resolveValue, resolveValues, token, tryResolveValue, tryResolveValues };
|
|
375
|
+
[key: string]: Token<unknown>;
|
|
376
|
+
})[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never }, Result>(this: unknown, constructor: new (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
|
|
377
|
+
//#endregion
|
|
378
|
+
export { type AnyModuleDeclaration, type BindModuleOptions, type Container, type ContainerResolver, type FactoryOptions, type FactoryScope, type Module, type ModuleBindingEntry, type ModuleDeclaration, type OptionalToken, type RequiredToken, ResolverError, type Token, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, injectable, injectableClass, isToken, optional, resolveValue, resolveValues, token, tryResolveValue, tryResolveValues };
|
|
379
|
+
//# sourceMappingURL=index.d.cts.map
|