ditox 3.1.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 +135 -32
- package/dist/index.cjs +518 -0
- package/dist/index.cjs.map +1 -0
- package/dist/{umd/index.d.ts → index.d.cts} +195 -197
- package/dist/{browser/index.d.ts → index.d.ts} +195 -197
- 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 +12 -6
- package/src/index.ts +26 -23
- 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 -88
- 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 -88
- 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,44 +76,49 @@ 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
|
+
/**
|
|
118
|
+
* A subset of Container interface that provides read-only access to dependency resolution.
|
|
119
|
+
* This type is used for parent containers to allow token resolution without exposing mutation methods.
|
|
120
|
+
*/
|
|
121
|
+
type ContainerResolver = Pick<Container, 'hasToken' | 'get' | 'resolve'>;
|
|
117
122
|
/**
|
|
118
123
|
* Creates a new dependency container.
|
|
119
124
|
*
|
|
@@ -121,14 +126,132 @@ type Container = {
|
|
|
121
126
|
*
|
|
122
127
|
* @param parentArg - Optional parent container or an array of containers.
|
|
123
128
|
*/
|
|
124
|
-
declare function createContainer(parentArg?:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
129
|
+
declare function createContainer(parentArg?: ContainerResolver | ReadonlyArray<ContainerResolver>): Container;
|
|
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;
|
|
128
136
|
};
|
|
129
|
-
|
|
130
|
-
|
|
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';
|
|
131
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;
|
|
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]> };
|
|
132
255
|
/**
|
|
133
256
|
* Checks if a value is the token
|
|
134
257
|
*/
|
|
@@ -158,8 +281,8 @@ declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyAr
|
|
|
158
281
|
* ```
|
|
159
282
|
*/
|
|
160
283
|
declare function tryResolveValue<Tokens extends Token<unknown> | {
|
|
161
|
-
|
|
162
|
-
}, 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;
|
|
163
286
|
/**
|
|
164
287
|
* Returns an array of resolved values or objects with resolved values.
|
|
165
288
|
*
|
|
@@ -178,10 +301,8 @@ declare function tryResolveValue<Tokens extends Token<unknown> | {
|
|
|
178
301
|
* ```
|
|
179
302
|
*/
|
|
180
303
|
declare function tryResolveValues<Tokens extends (Token<unknown> | {
|
|
181
|
-
|
|
182
|
-
})[], Values extends {
|
|
183
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V | undefined : Tokens[K] extends TokenProps<infer Props> ? Partial<Props> : never;
|
|
184
|
-
}>(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;
|
|
185
306
|
/**
|
|
186
307
|
* Resolves a value by the provided token.
|
|
187
308
|
*
|
|
@@ -200,8 +321,8 @@ declare function tryResolveValues<Tokens extends (Token<unknown> | {
|
|
|
200
321
|
* ```
|
|
201
322
|
*/
|
|
202
323
|
declare function resolveValue<Tokens extends Token<unknown> | {
|
|
203
|
-
|
|
204
|
-
}, 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;
|
|
205
326
|
/**
|
|
206
327
|
* Returns an array of resolved values or objects with resolved values.
|
|
207
328
|
*
|
|
@@ -220,10 +341,8 @@ declare function resolveValue<Tokens extends Token<unknown> | {
|
|
|
220
341
|
* ```
|
|
221
342
|
*/
|
|
222
343
|
declare function resolveValues<Tokens extends (Token<unknown> | {
|
|
223
|
-
|
|
224
|
-
})[], Values extends {
|
|
225
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
|
|
226
|
-
}>(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;
|
|
227
346
|
/**
|
|
228
347
|
* Decorates a factory by passing resolved values as factory arguments.
|
|
229
348
|
*
|
|
@@ -236,10 +355,8 @@ declare function resolveValues<Tokens extends (Token<unknown> | {
|
|
|
236
355
|
* @return Decorated factory which takes a dependency container as a single argument.
|
|
237
356
|
*/
|
|
238
357
|
declare function injectable<Tokens extends (Token<unknown> | {
|
|
239
|
-
|
|
240
|
-
})[], Values extends {
|
|
241
|
-
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
|
|
242
|
-
}, 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;
|
|
243
360
|
/**
|
|
244
361
|
* Decorates a class by passing resolved values as arguments to its constructor.
|
|
245
362
|
*
|
|
@@ -253,127 +370,8 @@ declare function injectable<Tokens extends (Token<unknown> | {
|
|
|
253
370
|
* and returns a new created class.
|
|
254
371
|
*/
|
|
255
372
|
declare function injectableClass<Tokens extends (Token<unknown> | {
|
|
256
|
-
|
|
257
|
-
})[], Values extends {
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
type AnyObject = Record<string, any>;
|
|
262
|
-
type EmptyObject = Record<string, never>;
|
|
263
|
-
type ModuleController = {
|
|
264
|
-
/** Dispose the module and clean its resources */
|
|
265
|
-
destroy?: () => void;
|
|
266
|
-
};
|
|
267
|
-
/**
|
|
268
|
-
* Dependency module
|
|
269
|
-
*
|
|
270
|
-
* @example
|
|
271
|
-
* ```ts
|
|
272
|
-
* type LoggerModule = Module<{
|
|
273
|
-
* logger: Logger;
|
|
274
|
-
* }>;
|
|
275
|
-
* ```
|
|
276
|
-
*/
|
|
277
|
-
type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
278
|
-
type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
279
|
-
/**
|
|
280
|
-
* Description how to bind the module in declarative way.
|
|
281
|
-
*
|
|
282
|
-
* @example
|
|
283
|
-
* ```ts
|
|
284
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
285
|
-
* token: LOGGER_MODULE_TOKEN,
|
|
286
|
-
* factory: (container) => {
|
|
287
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
288
|
-
* return {
|
|
289
|
-
* logger: { log: (message) => transport.write(message) },
|
|
290
|
-
* destroy: () => transport.close(),
|
|
291
|
-
* }
|
|
292
|
-
* },
|
|
293
|
-
* exports: {
|
|
294
|
-
* logger: LOGGER_TOKEN,
|
|
295
|
-
* },
|
|
296
|
-
* };
|
|
297
|
-
* ```
|
|
298
|
-
*/
|
|
299
|
-
type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
300
|
-
/** Token for the module */
|
|
301
|
-
token: Token<T>;
|
|
302
|
-
/** Modules for binding */
|
|
303
|
-
imports?: ReadonlyArray<ModuleBindingEntry>;
|
|
304
|
-
/** Factory of the module */
|
|
305
|
-
factory: (container: Container) => T;
|
|
306
|
-
/** Dictionary of module properties which are bound to tokens. */
|
|
307
|
-
exports?: {
|
|
308
|
-
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
309
|
-
};
|
|
310
|
-
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
311
|
-
beforeBinding?: (container: Container) => void;
|
|
312
|
-
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
313
|
-
afterBinding?: (container: Container) => void;
|
|
314
|
-
};
|
|
315
|
-
/**
|
|
316
|
-
* Options for module binding.
|
|
317
|
-
*
|
|
318
|
-
* `scope` types:
|
|
319
|
-
* - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
|
|
320
|
-
* - `scoped` - The value is created and cached by the nearest container which owns the factory function.
|
|
321
|
-
*/
|
|
322
|
-
type BindModuleOptions = {
|
|
323
|
-
scope?: 'scoped' | 'singleton';
|
|
324
|
-
};
|
|
325
|
-
type ModuleDeclarationWithOptions = {
|
|
326
|
-
module: ModuleDeclaration<AnyObject>;
|
|
327
|
-
options: BindModuleOptions;
|
|
328
|
-
};
|
|
329
|
-
type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
|
|
330
|
-
/**
|
|
331
|
-
* Binds the dependency module to the container
|
|
332
|
-
* @param container - Dependency container.
|
|
333
|
-
* @param moduleDeclaration - Declaration of the dependency module.
|
|
334
|
-
* @param options - Options for module binding.
|
|
335
|
-
*
|
|
336
|
-
* @example
|
|
337
|
-
* ```ts
|
|
338
|
-
* bindModule(container, LOGGER_MODULE);
|
|
339
|
-
* ```
|
|
340
|
-
*/
|
|
341
|
-
declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
342
|
-
/**
|
|
343
|
-
* Binds dependency modules to the container
|
|
344
|
-
*
|
|
345
|
-
* @param container - Dependency container for binding
|
|
346
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
347
|
-
*/
|
|
348
|
-
declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
349
|
-
/**
|
|
350
|
-
* Declares a module binding
|
|
351
|
-
*
|
|
352
|
-
* @param declaration - a module declaration
|
|
353
|
-
* @param declaration.token - optional field
|
|
354
|
-
*
|
|
355
|
-
* @example
|
|
356
|
-
* ```ts
|
|
357
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
358
|
-
* factory: (container) => {
|
|
359
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
360
|
-
* return {
|
|
361
|
-
* logger: { log: (message) => transport.write(message) },
|
|
362
|
-
* destroy: () => transport.close(),
|
|
363
|
-
* }
|
|
364
|
-
* },
|
|
365
|
-
* exports: {
|
|
366
|
-
* logger: LOGGER_TOKEN,
|
|
367
|
-
* },
|
|
368
|
-
* });
|
|
369
|
-
* ```
|
|
370
|
-
*/
|
|
371
|
-
declare function declareModule<T extends Module<AnyObject>>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
372
|
-
/**
|
|
373
|
-
* Declares bindings of several modules
|
|
374
|
-
*
|
|
375
|
-
* @param modules - module declaration entries
|
|
376
|
-
*/
|
|
377
|
-
declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
378
|
-
|
|
379
|
-
export { BindModuleOptions, Container, 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
|