ditox 3.2.0 → 3.3.0
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 +49 -27
- package/dist/index.cjs +518 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{umd/index.d.ts → index.d.cts} +189 -196
- package/dist/{browser/index.d.ts → index.d.ts} +189 -196
- package/dist/index.js +501 -0
- package/dist/index.js.map +1 -0
- package/dist/index.umd.js +524 -0
- package/dist/index.umd.js.map +1 -0
- package/package.json +22 -19
- package/src/container.test.ts +34 -33
- package/src/container.ts +2 -2
- package/src/index.ts +22 -24
- package/src/modules.test.ts +172 -37
- package/src/modules.ts +22 -5
- 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,43 @@ 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
|
+
*/
|
|
115
|
+
removeAll(): void;
|
|
116
116
|
};
|
|
117
117
|
/**
|
|
118
118
|
* A subset of Container interface that provides read-only access to dependency resolution.
|
|
@@ -127,13 +127,131 @@ type ContainerResolver = Pick<Container, 'hasToken' | 'get' | 'resolve'>;
|
|
|
127
127
|
* @param parentArg - Optional parent container or an array of containers.
|
|
128
128
|
*/
|
|
129
129
|
declare function createContainer(parentArg?: ContainerResolver | ReadonlyArray<ContainerResolver>): Container;
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
130
|
+
//#endregion
|
|
131
|
+
//#region src/modules.d.ts
|
|
132
|
+
type AnyObject = Record<string, any>;
|
|
133
|
+
type EmptyObject = Record<string, never>;
|
|
134
|
+
type ModuleController = {
|
|
135
|
+
/** Dispose the module and clean its resources */destroy?: () => void;
|
|
133
136
|
};
|
|
134
|
-
|
|
135
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Dependency module
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* type LoggerModule = Module<{
|
|
143
|
+
* logger: Logger;
|
|
144
|
+
* }>;
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
148
|
+
type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
149
|
+
/**
|
|
150
|
+
* Description how to bind the module in declarative way.
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```ts
|
|
154
|
+
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
155
|
+
* token: LOGGER_MODULE_TOKEN,
|
|
156
|
+
* factory: (container) => {
|
|
157
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
158
|
+
* return {
|
|
159
|
+
* logger: { log: (message) => transport.write(message) },
|
|
160
|
+
* destroy: () => transport.close(),
|
|
161
|
+
* }
|
|
162
|
+
* },
|
|
163
|
+
* exports: {
|
|
164
|
+
* logger: LOGGER_TOKEN,
|
|
165
|
+
* },
|
|
166
|
+
* };
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
169
|
+
type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
170
|
+
/** Token for the module */token: Token<T>; /** Modules for binding */
|
|
171
|
+
imports?: ReadonlyArray<ModuleBindingEntry>; /** Factory of the module */
|
|
172
|
+
factory: (container: Container) => T; /** Dictionary of module properties which are bound to tokens. */
|
|
173
|
+
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. */
|
|
174
|
+
beforeBinding?: (container: Container) => void; /** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
175
|
+
afterBinding?: (container: Container) => void;
|
|
176
|
+
/**
|
|
177
|
+
* Strategy for executing the factory:
|
|
178
|
+
* - `lazy` - **This is the default**. The factory is called when the module is resolved.
|
|
179
|
+
* - `eager` - The factory is called immediately after the module is bound to the container.
|
|
180
|
+
*/
|
|
181
|
+
strategy?: 'eager' | 'lazy';
|
|
182
|
+
};
|
|
183
|
+
type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
|
|
184
|
+
/**
|
|
185
|
+
* Options for module binding.
|
|
186
|
+
*
|
|
187
|
+
* `scope` types:
|
|
188
|
+
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
189
|
+
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
190
|
+
*/
|
|
191
|
+
type BindModuleOptions = {
|
|
192
|
+
scope?: 'scoped' | 'singleton';
|
|
193
|
+
};
|
|
194
|
+
type ModuleDeclarationWithOptions = {
|
|
195
|
+
module: ModuleDeclaration<AnyObject>;
|
|
196
|
+
options: BindModuleOptions;
|
|
136
197
|
};
|
|
198
|
+
type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
199
|
+
/**
|
|
200
|
+
* Binds the dependency module to the container
|
|
201
|
+
* @param container - Dependency container.
|
|
202
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
203
|
+
* @param options - Options for module binding.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* bindModule(container, LOGGER_MODULE);
|
|
208
|
+
* ```
|
|
209
|
+
*/
|
|
210
|
+
declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
211
|
+
/**
|
|
212
|
+
* Binds dependency modules to the container
|
|
213
|
+
*
|
|
214
|
+
* @param container - Dependency container for binding
|
|
215
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
216
|
+
*/
|
|
217
|
+
declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
218
|
+
/**
|
|
219
|
+
* Declares a module binding
|
|
220
|
+
*
|
|
221
|
+
* @param declaration - a module declaration
|
|
222
|
+
* @param declaration.token - optional field
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```ts
|
|
226
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
227
|
+
* factory: (container) => {
|
|
228
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
229
|
+
* return {
|
|
230
|
+
* logger: { log: (message) => transport.write(message) },
|
|
231
|
+
* destroy: () => transport.close(),
|
|
232
|
+
* }
|
|
233
|
+
* },
|
|
234
|
+
* exports: {
|
|
235
|
+
* logger: LOGGER_TOKEN,
|
|
236
|
+
* },
|
|
237
|
+
* });
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
declare function declareModule<T extends Module<AnyObject>>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
241
|
+
/**
|
|
242
|
+
* @deprecated Use `declareModule` instead
|
|
243
|
+
*
|
|
244
|
+
* Declares bindings of several modules
|
|
245
|
+
*
|
|
246
|
+
* @param modules - module declaration entries
|
|
247
|
+
*/
|
|
248
|
+
declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/utils.d.ts
|
|
251
|
+
type ValuesProps = {
|
|
252
|
+
[key: string]: unknown;
|
|
253
|
+
};
|
|
254
|
+
type TokenProps<Props extends ValuesProps> = { [K in keyof Props]: Token<Props[K]> };
|
|
137
255
|
/**
|
|
138
256
|
* Checks if a value is the token
|
|
139
257
|
*/
|
|
@@ -163,8 +281,8 @@ declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyAr
|
|
|
163
281
|
* ```
|
|
164
282
|
*/
|
|
165
283
|
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;
|
|
284
|
+
[key: string]: Token<unknown>;
|
|
285
|
+
}, Values extends (Tokens extends Token<infer V> ? V | undefined : Tokens extends TokenProps<infer Props> ? Partial<Props> : never)>(container: Container, token: Tokens): Values;
|
|
168
286
|
/**
|
|
169
287
|
* Returns an array of resolved values or objects with resolved values.
|
|
170
288
|
*
|
|
@@ -183,10 +301,8 @@ declare function tryResolveValue<Tokens extends Token<unknown> | {
|
|
|
183
301
|
* ```
|
|
184
302
|
*/
|
|
185
303
|
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;
|
|
304
|
+
[key: string]: Token<unknown>;
|
|
305
|
+
})[], 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
306
|
/**
|
|
191
307
|
* Resolves a value by the provided token.
|
|
192
308
|
*
|
|
@@ -205,8 +321,8 @@ declare function tryResolveValues<Tokens extends (Token<unknown> | {
|
|
|
205
321
|
* ```
|
|
206
322
|
*/
|
|
207
323
|
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;
|
|
324
|
+
[key: string]: Token<unknown>;
|
|
325
|
+
}, Values extends (Tokens extends Token<infer V> ? V : Tokens extends TokenProps<infer Props> ? Props : never)>(container: Container, token: Tokens): Values;
|
|
210
326
|
/**
|
|
211
327
|
* Returns an array of resolved values or objects with resolved values.
|
|
212
328
|
*
|
|
@@ -225,10 +341,8 @@ declare function resolveValue<Tokens extends Token<unknown> | {
|
|
|
225
341
|
* ```
|
|
226
342
|
*/
|
|
227
343
|
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;
|
|
344
|
+
[key: string]: Token<unknown>;
|
|
345
|
+
})[], 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
346
|
/**
|
|
233
347
|
* Decorates a factory by passing resolved values as factory arguments.
|
|
234
348
|
*
|
|
@@ -241,10 +355,8 @@ declare function resolveValues<Tokens extends (Token<unknown> | {
|
|
|
241
355
|
* @return Decorated factory which takes a dependency container as a single argument.
|
|
242
356
|
*/
|
|
243
357
|
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;
|
|
358
|
+
[key: string]: Token<unknown>;
|
|
359
|
+
})[], 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
360
|
/**
|
|
249
361
|
* Decorates a class by passing resolved values as arguments to its constructor.
|
|
250
362
|
*
|
|
@@ -258,127 +370,8 @@ declare function injectable<Tokens extends (Token<unknown> | {
|
|
|
258
370
|
* and returns a new created class.
|
|
259
371
|
*/
|
|
260
372
|
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 };
|
|
373
|
+
[key: string]: Token<unknown>;
|
|
374
|
+
})[], 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;
|
|
375
|
+
//#endregion
|
|
376
|
+
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 };
|
|
377
|
+
//# sourceMappingURL=index.d.ts.map
|