ditox 1.4.0 → 2.1.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.
Files changed (71) hide show
  1. package/README.md +7 -6
  2. package/dist/{index.d.ts → browser/index.d.ts} +133 -13
  3. package/dist/browser/index.js +2 -0
  4. package/dist/browser/index.js.map +1 -0
  5. package/dist/cjs/ditox.d.ts +125 -0
  6. package/dist/cjs/ditox.js +210 -0
  7. package/dist/cjs/ditox.js.map +1 -0
  8. package/dist/cjs/index.d.ts +6 -0
  9. package/dist/cjs/index.js +28 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/modules.d.ts +124 -0
  12. package/dist/cjs/modules.js +110 -0
  13. package/dist/cjs/modules.js.map +1 -0
  14. package/dist/cjs/utils-deprecated.d.ts +59 -0
  15. package/dist/cjs/utils-deprecated.js +66 -0
  16. package/dist/cjs/utils-deprecated.js.map +1 -0
  17. package/dist/cjs/utils.d.ts +136 -0
  18. package/dist/cjs/utils.js +156 -0
  19. package/dist/cjs/utils.js.map +1 -0
  20. package/dist/esm/ditox.d.ts +125 -0
  21. package/{lib/index.js → dist/esm/ditox.js} +10 -201
  22. package/dist/esm/ditox.js.map +1 -0
  23. package/dist/esm/index.d.ts +6 -0
  24. package/dist/esm/index.js +5 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/modules.d.ts +124 -0
  27. package/dist/esm/modules.js +103 -0
  28. package/dist/esm/modules.js.map +1 -0
  29. package/dist/esm/utils-deprecated.d.ts +59 -0
  30. package/dist/esm/utils-deprecated.js +59 -0
  31. package/dist/esm/utils-deprecated.js.map +1 -0
  32. package/dist/esm/utils.d.ts +136 -0
  33. package/dist/esm/utils.js +145 -0
  34. package/dist/esm/utils.js.map +1 -0
  35. package/{lib → dist/umd}/index.d.ts +133 -13
  36. package/dist/umd/index.js +2 -0
  37. package/dist/umd/index.js.map +1 -0
  38. package/docs/.nojekyll +1 -0
  39. package/docs/README.md +364 -98
  40. package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
  41. package/package.json +16 -17
  42. package/src/ditox.test.ts +10 -1
  43. package/src/index.ts +18 -11
  44. package/src/modules.test.ts +32 -0
  45. package/src/modules.ts +4 -4
  46. package/src/utils-deprecated.test.ts +286 -0
  47. package/src/utils-deprecated.ts +87 -0
  48. package/src/utils.test.ts +173 -58
  49. package/src/utils.ts +164 -66
  50. package/CHANGELOG.md +0 -110
  51. package/dist/index.browser.js +0 -2
  52. package/dist/index.browser.js.flow +0 -4
  53. package/dist/index.browser.js.map +0 -1
  54. package/dist/index.js +0 -411
  55. package/dist/index.js.flow +0 -4
  56. package/dist/index.js.map +0 -1
  57. package/dist/index.module.d.ts +0 -301
  58. package/dist/index.module.js +0 -393
  59. package/dist/index.module.js.flow +0 -4
  60. package/dist/index.module.js.map +0 -1
  61. package/dist/index.umd.d.ts +0 -301
  62. package/dist/index.umd.js +0 -2
  63. package/dist/index.umd.js.flow +0 -4
  64. package/dist/index.umd.js.map +0 -1
  65. package/lib/index.js.flow +0 -4
  66. package/lib/index.js.map +0 -1
  67. package/mod.ts +0 -2
  68. package/src/ditox.js.flow +0 -131
  69. package/src/index.js.flow +0 -34
  70. package/src/modules.js.flow +0 -156
  71. package/src/utils.js.flow +0 -113
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.injectableClass = exports.injectable = exports.resolveValues = exports.resolveValue = exports.tryResolveValues = exports.tryResolveValue = exports.bindMultiValue = exports.isToken = void 0;
4
+ /**
5
+ * Checks if a value is the token
6
+ */
7
+ function isToken(value) {
8
+ return (value !== undefined &&
9
+ value !== null &&
10
+ typeof value === 'object' &&
11
+ 'symbol' in value &&
12
+ typeof value.symbol === 'symbol');
13
+ }
14
+ exports.isToken = isToken;
15
+ /**
16
+ * Rebinds the array by the token with added new value.
17
+ * @param container - Dependency container.
18
+ * @param token - Token for an array of values.
19
+ * @param value - New value which is added to the end of the array.
20
+ */
21
+ function bindMultiValue(container, token, value) {
22
+ var _a;
23
+ const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
24
+ const nextValues = [...prevValues, value];
25
+ container.bindValue(token, nextValues);
26
+ }
27
+ exports.bindMultiValue = bindMultiValue;
28
+ /**
29
+ * Tries to resolve a value by the provided token.
30
+ *
31
+ * If an argument is an object which has tokens as its properties,
32
+ * then returns an object containing resolved values as properties.
33
+
34
+ * If a token is not found, then `undefined` value is used.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const value = tryResolveValue(container, tokenA);
39
+ * console.log(value); // 1
40
+ *
41
+ * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
42
+ * console.log(props); // {a: 1, b: 2}
43
+ * ```
44
+ */
45
+ function tryResolveValue(container, token) {
46
+ if (isToken(token)) {
47
+ return container.get(token);
48
+ }
49
+ const obj = {};
50
+ Object.keys(token).forEach((key) => (obj[key] = container.get(token[key])));
51
+ return obj;
52
+ }
53
+ exports.tryResolveValue = tryResolveValue;
54
+ /**
55
+ * Returns an array of resolved values or objects with resolved values.
56
+ *
57
+ * If an item of the array is an object which has tokens as its properties,
58
+ * then returns an object containing resolved values as properties.
59
+
60
+ * If a token is not found, then `undefined` value is used.
61
+ *
62
+ * @example
63
+ * ```ts
64
+ * const items1 = tryResolveValues(container, tokenA);
65
+ * console.log(items1); // [1]
66
+ *
67
+ * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
68
+ * console.log(items2); // [1, {a: 1, b: 2}]
69
+ * ```
70
+ */
71
+ function tryResolveValues(container, ...tokens) {
72
+ return tokens.map((item) => tryResolveValue(container, item));
73
+ }
74
+ exports.tryResolveValues = tryResolveValues;
75
+ /**
76
+ * Resolves a value by the provided token.
77
+ *
78
+ * If an argument is an object which has tokens as its properties,
79
+ * then returns an object containing resolved values as properties.
80
+
81
+ * If a value is not found by the token, then `ResolverError` is thrown.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const value = resolveValue(container, tokenA);
86
+ * console.log(value); // 1
87
+ *
88
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
89
+ * console.log(props); // {a: 1, b: 2}
90
+ * ```
91
+ */
92
+ function resolveValue(container, token) {
93
+ if (isToken(token)) {
94
+ return container.resolve(token);
95
+ }
96
+ const obj = {};
97
+ Object.keys(token).forEach((key) => (obj[key] = container.resolve(token[key])));
98
+ return obj;
99
+ }
100
+ exports.resolveValue = resolveValue;
101
+ /**
102
+ * Returns an array of resolved values or objects with resolved values.
103
+ *
104
+ * If an item of the array is an object which has tokens as its properties,
105
+ * then returns an object containing resolved values as properties.
106
+
107
+ * If a token is not found, then `ResolverError` is thrown.
108
+ *
109
+ * @example
110
+ * ```ts
111
+ * const items1 = resolveValues(container, tokenA);
112
+ * console.log(items1); // [1]
113
+ *
114
+ * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
115
+ * console.log(items2); // [1, {a: 1, b: 2}]
116
+ * ```
117
+ */
118
+ function resolveValues(container, ...tokens) {
119
+ return tokens.map((item) => resolveValue(container, item));
120
+ }
121
+ exports.resolveValues = resolveValues;
122
+ /**
123
+ * Decorates a factory by passing resolved values as factory arguments.
124
+ *
125
+ * If an argument is an object which has tokens as its properties,
126
+ * then returns an object containing resolved values as properties.
127
+ *
128
+ * @param factory - A factory.
129
+ * @param tokens - Tokens which correspond to factory arguments.
130
+ *
131
+ * @return Decorated factory which takes a dependency container as a single argument.
132
+ */
133
+ function injectable(factory, ...tokens) {
134
+ return (container) => {
135
+ const values = resolveValues(container, ...tokens);
136
+ return factory.apply(this, values);
137
+ };
138
+ }
139
+ exports.injectable = injectable;
140
+ /**
141
+ * Decorates a class by passing resolved values as arguments to its constructor.
142
+ *
143
+ * If an argument is an object which has tokens as its properties,
144
+ * then returns an object containing resolved values as properties.
145
+ *
146
+ * @param constructor - Constructor of a class
147
+ * @param tokens - Tokens which correspond to constructor arguments
148
+ *
149
+ * @return A factory function which takes a dependency container as a single argument
150
+ * and returns a new created class.
151
+ */
152
+ function injectableClass(constructor, ...tokens) {
153
+ return injectable((...values) => new constructor(...values), ...tokens);
154
+ }
155
+ exports.injectableClass = injectableClass;
156
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAOA;;GAEG;AACH,SAAgB,OAAO,CAAI,KAAc;IACvC,OAAO,CACL,KAAK,KAAK,SAAS;QACnB,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,QAAQ,IAAI,KAAK;QACjB,OAAQ,KAAa,CAAC,MAAM,KAAK,QAAQ,CAC1C,CAAC;AACJ,CAAC;AARD,0BAQC;AAED;;;;;GAKG;AACH,SAAgB,cAAc,CAC5B,SAAoB,EACpB,KAA8B,EAC9B,KAAQ;;IAER,MAAM,UAAU,GAAG,MAAA,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,mCAAI,EAAE,CAAC;IAC9C,MAAM,UAAU,GAAG,CAAC,GAAG,UAAU,EAAE,KAAK,CAAC,CAAC;IAC1C,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;AACzC,CAAC;AARD,wCAQC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,SAAgB,eAAe,CAO7B,SAAoB,EAAE,KAAa;IACnC,IAAI,OAAO,CAAC,KAAK,CAAC,EAAE;QAClB,OAAO,SAAS,CAAC,GAAG,CAAC,KAAK,CAAW,CAAC;KACvC;IAED,MAAM,GAAG,GAAQ,EAAE,CAAC;IACpB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5E,OAAO,GAAG,CAAC;AACb,CAAC;AAfD,0CAeC;AAED;;;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e,CAY7B,WAA8C,EAC9C,GAAG,MAAc;IAEjB,OAAO,UAAU,CACf,CAAC,GAAG,MAAM,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,GAAG,MAAM,CAAC,EACzC,GAAG,MAAM,CACV,CAAC;AACJ,CAAC;AAnBD,0CAmBC"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @ignore
3
+ * Binding token for mandatory value
4
+ */
5
+ export declare type RequiredToken<T> = {
6
+ symbol: symbol;
7
+ type?: T;
8
+ isOptional?: false;
9
+ };
10
+ /**
11
+ * @ignore
12
+ * Binding token for optional value
13
+ */
14
+ export declare type OptionalToken<T> = {
15
+ symbol: symbol;
16
+ type?: T;
17
+ isOptional: true;
18
+ optionalValue: T;
19
+ };
20
+ /**
21
+ * Binding token.
22
+ */
23
+ export declare type Token<T> = RequiredToken<T> | OptionalToken<T>;
24
+ /**
25
+ * Creates a new binding token.
26
+ * @param description - Token description for better error messages.
27
+ */
28
+ export declare function token<T>(description?: string): Token<T>;
29
+ /**
30
+ * Decorate a token with an optional value.
31
+ * This value is be used as default value in case a container does not have registered token.
32
+ * @param token - Existed token.
33
+ * @param optionalValue - Default value for the resolver.
34
+ */
35
+ export declare function optional<T>(token: Token<T>, optionalValue: T): OptionalToken<T>;
36
+ export declare function optional<T>(token: Token<T>): OptionalToken<T | undefined>;
37
+ /**
38
+ * ResolverError is thrown by the resolver when a token is not found in a container.
39
+ */
40
+ export declare class ResolverError extends Error {
41
+ constructor(message: string);
42
+ }
43
+ /**
44
+ * @see https://github.com/mnasyrov/ditox#factory-lifetimes
45
+ */
46
+ export declare type FactoryScope = 'scoped' | 'singleton' | 'transient';
47
+ /**
48
+ * Options for factory binding.
49
+ *
50
+ * `scope` types:
51
+ * - `singleton` - **This is the default**. The value is created and cached by the container which registered the factory.
52
+ * - `scoped` - The value is created and cached by the container which starts resolving.
53
+ * - `transient` - The value is created every time it is resolved.
54
+ *
55
+ * `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
56
+ */
57
+ export declare type FactoryOptions<T> = {
58
+ scope?: 'scoped' | 'singleton';
59
+ onRemoved?: (value: T) => void;
60
+ } | {
61
+ scope: 'transient';
62
+ };
63
+ /**
64
+ * Dependency container.
65
+ */
66
+ export declare type Container = {
67
+ /**
68
+ * Binds a value for the token
69
+ */
70
+ bindValue<T>(token: Token<T>, value: T): void;
71
+ /**
72
+ * Binds a factory for the token.
73
+ */
74
+ bindFactory<T>(token: Token<T>, factory: (container: Container) => T, options?: FactoryOptions<T>): void;
75
+ /**
76
+ * Checks if the token is registered in the container hierarchy.
77
+ */
78
+ hasToken(token: Token<unknown>): boolean;
79
+ /**
80
+ * Returns a resolved value by the token, or returns `undefined` in case the token is not found.
81
+ */
82
+ get<T>(token: Token<T>): T | undefined;
83
+ /**
84
+ * Returns a resolved value by the token, or throws `ResolverError` in case the token is not found.
85
+ */
86
+ resolve<T>(token: Token<T>): T;
87
+ /**
88
+ * Removes a binding for the token.
89
+ */
90
+ remove<T>(token: Token<T>): void;
91
+ /**
92
+ * Removes all bindings in the container.
93
+ */
94
+ removeAll(): void;
95
+ };
96
+ /** @internal */
97
+ export declare const CONTAINER: Token<Container>;
98
+ /** @internal */
99
+ export declare const PARENT_CONTAINER: Token<Container>;
100
+ /** @internal */
101
+ export declare const RESOLVER: Token<Resolver>;
102
+ /** @internal */
103
+ declare const NOT_FOUND: unique symbol;
104
+ /** @internal */
105
+ export declare const FAKE_FACTORY: () => never;
106
+ /** @internal */
107
+ declare type FactoryContext<T> = {
108
+ factory: (container: Container) => T;
109
+ options?: FactoryOptions<T>;
110
+ };
111
+ /** @internal */
112
+ export declare type FactoriesMap = Map<symbol, FactoryContext<any>>;
113
+ /** @internal */
114
+ export declare const FACTORIES_MAP: Token<FactoriesMap>;
115
+ /** @internal */
116
+ declare type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
117
+ /**
118
+ * Creates a new dependency container.
119
+ *
120
+ * Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
121
+ *
122
+ * @param parentContainer - Optional parent container.
123
+ */
124
+ export declare function createContainer(parentContainer?: Container): Container;
125
+ export {};
@@ -2,10 +2,10 @@
2
2
  * Creates a new binding token.
3
3
  * @param description - Token description for better error messages.
4
4
  */
5
- function token(description) {
5
+ export function token(description) {
6
6
  return { symbol: Symbol(description) };
7
7
  }
8
- function optional(token, optionalValue) {
8
+ export function optional(token, optionalValue) {
9
9
  return {
10
10
  symbol: token.symbol,
11
11
  isOptional: true,
@@ -15,7 +15,7 @@ function optional(token, optionalValue) {
15
15
  /**
16
16
  * ResolverError is thrown by the resolver when a token is not found in a container.
17
17
  */
18
- class ResolverError extends Error {
18
+ export class ResolverError extends Error {
19
19
  constructor(message) {
20
20
  super(message);
21
21
  Object.setPrototypeOf(this, new.target.prototype);
@@ -23,21 +23,21 @@ class ResolverError extends Error {
23
23
  }
24
24
  }
25
25
  /** @internal */
26
- const CONTAINER = token('ditox.Container');
26
+ export const CONTAINER = token('ditox.Container');
27
27
  /** @internal */
28
- const PARENT_CONTAINER = token('ditox.ParentContainer');
28
+ export const PARENT_CONTAINER = token('ditox.ParentContainer');
29
29
  /** @internal */
30
- const RESOLVER = token('ditox.Resolver');
30
+ export const RESOLVER = token('ditox.Resolver');
31
31
  /** @internal */
32
32
  const NOT_FOUND = Symbol();
33
33
  /** @internal */
34
- const FAKE_FACTORY = () => {
34
+ export const FAKE_FACTORY = () => {
35
35
  throw new Error('FAKE_FACTORY');
36
36
  };
37
37
  /** @internal */
38
38
  const DEFAULT_SCOPE = 'singleton';
39
39
  /** @internal */
40
- const FACTORIES_MAP = token('ditox.FactoriesMap');
40
+ export const FACTORIES_MAP = token('ditox.FactoriesMap');
41
41
  /** @internal */
42
42
  function getScope(options) {
43
43
  var _a;
@@ -64,7 +64,7 @@ function isInternalToken(token) {
64
64
  *
65
65
  * @param parentContainer - Optional parent container.
66
66
  */
67
- function createContainer(parentContainer) {
67
+ export function createContainer(parentContainer) {
68
68
  const values = new Map();
69
69
  const factories = new Map();
70
70
  const container = {
@@ -199,195 +199,4 @@ function createContainer(parentContainer) {
199
199
  bindInternalTokens();
200
200
  return container;
201
201
  }
202
-
203
- /**
204
- * Rebinds the array by the token with added new value.
205
- * @param container - Dependency container.
206
- * @param token - Token for an array of values.
207
- * @param value - New value which is added to the end of the array.
208
- */
209
- function bindMultiValue(container, token, value) {
210
- var _a;
211
- const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
212
- const nextValues = [...prevValues, value];
213
- container.bindValue(token, nextValues);
214
- }
215
- /**
216
- * Returns an array of resolved values by the specified token.
217
- * If a token is not found, then `undefined` value is used.
218
- */
219
- function getValues(container, ...tokens) {
220
- return tokens.map(container.get);
221
- }
222
- /**
223
- * Returns an array of resolved values by the specified token.
224
- * If a token is not found, then `ResolverError` is thrown.
225
- */
226
- function resolveValues(container, ...tokens) {
227
- return tokens.map(container.resolve);
228
- }
229
- /**
230
- * Decorates a factory by passing resolved tokens as factory arguments.
231
- * @param factory - A factory.
232
- * @param tokens - Tokens which correspond to factory arguments.
233
- * @return Decorated factory which takes a dependency container as a single argument.
234
- */
235
- function injectable(factory, ...tokens) {
236
- return (container) => {
237
- const values = resolveValues(container, ...tokens);
238
- return factory.apply(this, values);
239
- };
240
- }
241
- /**
242
- * Returns an object with resolved properties which are specified by token properties.
243
- * If a token is not found, then `undefined` value is used.
244
- *
245
- * @example
246
- * ```ts
247
- * const props = getProps(container, {a: tokenA, b: tokenB});
248
- * console.log(props); // {a: 1, b: 2}
249
- * ```
250
- */
251
- function getProps(container, tokens) {
252
- const obj = { ...tokens };
253
- Object.keys(obj).forEach((key) => (obj[key] = container.get(obj[key])));
254
- return obj;
255
- }
256
- /**
257
- * Returns an object with resolved properties which are specified by token properties.
258
- * If a token is not found, then `ResolverError` is thrown.
259
- *
260
- * @example
261
- * ```ts
262
- * const props = resolveProps(container, {a: tokenA, b: tokenB});
263
- * console.log(props); // {a: 1, b: 2}
264
- * ```
265
- */
266
- function resolveProps(container, tokens) {
267
- const obj = { ...tokens };
268
- Object.keys(obj).forEach((key) => (obj[key] = container.resolve(obj[key])));
269
- return obj;
270
- }
271
- /**
272
- * Decorates a factory by passing a resolved object with tokens as the first argument.
273
- * @param factory - A factory.
274
- * @param tokens - Object with tokens.
275
- * @return Decorated factory which takes a dependency container as a single argument.
276
- *
277
- * @example
278
- * ```ts
279
- * const factory = ({a, b}: {a: number, b: number}) => a + b;
280
- * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
281
- * const result = decoratedFactory(container);
282
- * ```
283
- */
284
- function injectableProps(factory, tokens) {
285
- return (container) => {
286
- const values = resolveProps(container, tokens);
287
- return factory(values);
288
- };
289
- }
290
-
291
- /**
292
- * Binds the dependency module to the container
293
- * @param container - Dependency container.
294
- * @param moduleDeclaration - Declaration of the dependency module.
295
- * @param options - Options for module binding.
296
- *
297
- * @example
298
- * ```ts
299
- * bindModule(container, LOGGER_MODULE);
300
- * ```
301
- */
302
- function bindModule(container, moduleDeclaration, options) {
303
- var _a;
304
- const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
305
- const exports = (_a = moduleDeclaration.exports) !== null && _a !== void 0 ? _a : moduleDeclaration.exportedProps;
306
- const scope = options === null || options === void 0 ? void 0 : options.scope;
307
- if (imports) {
308
- bindModules(container, imports);
309
- }
310
- if (beforeBinding) {
311
- beforeBinding(container);
312
- }
313
- const exportedValueTokens = new Set();
314
- if (exports) {
315
- const keys = Object.keys(exports);
316
- keys.forEach((valueKey) => {
317
- const valueToken = exports[valueKey];
318
- if (valueToken) {
319
- exportedValueTokens.add(valueToken);
320
- container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
321
- }
322
- });
323
- }
324
- container.bindFactory(token, factory, {
325
- scope,
326
- onRemoved: (module) => {
327
- if (module.destroy) {
328
- module.destroy();
329
- }
330
- exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
331
- exportedValueTokens.clear();
332
- },
333
- });
334
- if (afterBinding) {
335
- afterBinding(container);
336
- }
337
- }
338
- /**
339
- * Binds dependency modules to the container
340
- *
341
- * @param container - Dependency container for binding
342
- * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
343
- */
344
- function bindModules(container, modules) {
345
- modules.forEach((entry) => {
346
- if ('module' in entry) {
347
- bindModule(container, entry.module, entry.options);
348
- }
349
- else {
350
- bindModule(container, entry);
351
- }
352
- });
353
- }
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
- function declareModule(declaration) {
377
- var _a;
378
- return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
379
- }
380
- /**
381
- * Declares bindings of several modules
382
- *
383
- * @param modules - module declaration entries
384
- */
385
- function declareModuleBindings(modules) {
386
- return declareModule({
387
- factory: () => ({}),
388
- imports: modules,
389
- });
390
- }
391
-
392
- export { ResolverError, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, getProps, getValues, injectable, injectableProps, optional, resolveProps, resolveValues, token };
393
- //# sourceMappingURL=index.js.map
202
+ //# sourceMappingURL=ditox.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ditox.js","sourceRoot":"","sources":["../../src/ditox.ts"],"names":[],"mappings":"AA0BA;;;GAGG;AACH,MAAM,UAAU,KAAK,CAAI,WAAoB;IAC3C,OAAO,EAAC,MAAM,EAAE,MAAM,CAAC,WAAW,CAAC,EAAC,CAAC;AACvC,CAAC;AAaD,MAAM,UAAU,QAAQ,CACtB,KAAe,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@@ -0,0 +1,6 @@
1
+ export { token, optional, ResolverError, createContainer } from './ditox';
2
+ export type { RequiredToken, OptionalToken, Token, FactoryScope, FactoryOptions, Container, } from './ditox';
3
+ export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
4
+ export { getValues, getProps, resolveProps, injectableProps, } from './utils-deprecated';
5
+ export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
6
+ export type { Module, ModuleDeclaration, BindModuleOptions, ModuleBindingEntry, } from './modules';
@@ -0,0 +1,5 @@
1
+ export { token, optional, ResolverError, createContainer } from './ditox';
2
+ export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
3
+ export { getValues, getProps, resolveProps, injectableProps, } from './utils-deprecated';
4
+ export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,SAAS,CAAC;AAUxE,OAAO,EACL,OAAO,EACP,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,UAAU,EACV,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,UAAU,EACV,WAAW,EACX,aAAa,EACb,qBAAqB,GACtB,MAAM,WAAW,CAAC"}