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.
Files changed (53) hide show
  1. package/README.md +135 -32
  2. package/dist/index.cjs +518 -0
  3. package/dist/index.cjs.map +1 -0
  4. package/dist/{umd/index.d.ts → index.d.cts} +195 -197
  5. package/dist/{browser/index.d.ts → index.d.ts} +195 -197
  6. package/dist/index.js +501 -0
  7. package/dist/index.js.map +1 -0
  8. package/dist/index.umd.js +524 -0
  9. package/dist/index.umd.js.map +1 -0
  10. package/package.json +22 -19
  11. package/src/container.test.ts +34 -33
  12. package/src/container.ts +12 -6
  13. package/src/index.ts +26 -23
  14. package/src/modules.test.ts +172 -37
  15. package/src/modules.ts +22 -5
  16. package/src/tokens.test.ts +9 -8
  17. package/src/tokens.ts +2 -2
  18. package/src/utils.test.ts +18 -17
  19. package/src/utils.ts +21 -21
  20. package/dist/browser/index.js +0 -2
  21. package/dist/browser/index.js.map +0 -1
  22. package/dist/cjs/container.d.ts +0 -88
  23. package/dist/cjs/container.js +0 -216
  24. package/dist/cjs/container.js.map +0 -1
  25. package/dist/cjs/index.d.ts +0 -7
  26. package/dist/cjs/index.js +0 -24
  27. package/dist/cjs/index.js.map +0 -1
  28. package/dist/cjs/modules.d.ts +0 -121
  29. package/dist/cjs/modules.js +0 -143
  30. package/dist/cjs/modules.js.map +0 -1
  31. package/dist/cjs/tokens.d.ts +0 -56
  32. package/dist/cjs/tokens.js +0 -20
  33. package/dist/cjs/tokens.js.map +0 -1
  34. package/dist/cjs/utils.d.ts +0 -137
  35. package/dist/cjs/utils.js +0 -156
  36. package/dist/cjs/utils.js.map +0 -1
  37. package/dist/esm/container.d.ts +0 -88
  38. package/dist/esm/container.js +0 -211
  39. package/dist/esm/container.js.map +0 -1
  40. package/dist/esm/index.d.ts +0 -7
  41. package/dist/esm/index.js +0 -5
  42. package/dist/esm/index.js.map +0 -1
  43. package/dist/esm/modules.d.ts +0 -121
  44. package/dist/esm/modules.js +0 -136
  45. package/dist/esm/modules.js.map +0 -1
  46. package/dist/esm/tokens.d.ts +0 -56
  47. package/dist/esm/tokens.js +0 -15
  48. package/dist/esm/tokens.js.map +0 -1
  49. package/dist/esm/utils.d.ts +0 -137
  50. package/dist/esm/utils.js +0 -145
  51. package/dist/esm/utils.js.map +0 -1
  52. package/dist/umd/index.js +0 -2
  53. package/dist/umd/index.js.map +0 -1
package/dist/cjs/utils.js DELETED
@@ -1,156 +0,0 @@
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
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAQA;;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,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;;;;;;;;;;;;;GAgBG;AACH,SAAgB,aAAa,CAS3B,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,YAAY,CAAC,SAAS,EAAE,IAAI,CAAC,CAAW,CAAC;AACvE,CAAC;AAXD,sCAWC;AAED;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CAYxB,OAAsC,EACtC,GAAG,MAAc;IAEjB,OAAO,CAAC,SAAS,EAAE,EAAE;QACnB,MAAM,MAAM,GAAW,aAAa,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC;QAC3D,OAAO,OAAO,CAAC,KAAK,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;IACrC,CAAC,CAAC;AACJ,CAAC;AAnBD,gCAmBC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,eAAe,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"}
@@ -1,88 +0,0 @@
1
- import { Token } from './tokens';
2
- /**
3
- * ResolverError is thrown by the resolver when a token is not found in a container.
4
- */
5
- export declare class ResolverError extends Error {
6
- constructor(message: string);
7
- }
8
- /**
9
- * @see https://github.com/mnasyrov/ditox#factory-lifetimes
10
- */
11
- export type FactoryScope = 'scoped' | 'singleton' | 'transient';
12
- /**
13
- * Options for factory binding.
14
- *
15
- * `scope` types:
16
- * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
17
- * - `scoped` - The value is created and cached by the nearest container which owns the factory function.
18
- * - `transient` - The value is created every time it is resolved.
19
- *
20
- * `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
21
- */
22
- export type FactoryOptions<T> = {
23
- scope?: 'scoped' | 'singleton';
24
- onRemoved?: (value: T) => void;
25
- } | {
26
- scope: 'transient';
27
- };
28
- /**
29
- * Dependency container.
30
- */
31
- export type Container = {
32
- /**
33
- * Binds a value for the token
34
- */
35
- bindValue<T>(token: Token<T>, value: T): void;
36
- /**
37
- * Binds a factory for the token.
38
- */
39
- bindFactory<T>(token: Token<T>, factory: (container: Container) => T, options?: FactoryOptions<T>): void;
40
- /**
41
- * Checks if the token is registered in the container hierarchy.
42
- */
43
- hasToken(token: Token<unknown>): boolean;
44
- /**
45
- * Returns a resolved value by the token, or returns `undefined` in case the token is not found.
46
- */
47
- get<T>(token: Token<T>): T | undefined;
48
- /**
49
- * Returns a resolved value by the token or throws `ResolverError` in case the token is not found.
50
- */
51
- resolve<T>(token: Token<T>): T;
52
- /**
53
- * Removes a binding for the token.
54
- */
55
- remove<T>(token: Token<T>): void;
56
- /**
57
- * Removes all bindings in the container.
58
- */
59
- removeAll(): void;
60
- };
61
- /** @internal */
62
- export declare const CONTAINER: Token<Container>;
63
- /** @internal */
64
- export declare const PARENT_CONTAINERS: Token<ReadonlyArray<Container>>;
65
- /** @internal */
66
- export declare const RESOLVER: Token<Resolver>;
67
- /** @internal */
68
- declare const NOT_FOUND: unique symbol;
69
- /** @internal */
70
- type FactoryContext<T> = {
71
- factory: (container: Container) => T;
72
- options?: FactoryOptions<T>;
73
- };
74
- /** @internal */
75
- export type FactoriesMap = Map<symbol, FactoryContext<any>>;
76
- /** @internal */
77
- export declare const FACTORIES_MAP: Token<FactoriesMap>;
78
- /** @internal */
79
- type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
80
- /**
81
- * Creates a new dependency container.
82
- *
83
- * 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.
84
- *
85
- * @param parentArg - Optional parent container or an array of containers.
86
- */
87
- export declare function createContainer(parentArg?: Container | ReadonlyArray<Container>): Container;
88
- export {};
@@ -1,211 +0,0 @@
1
- import { token } from './tokens';
2
- /**
3
- * ResolverError is thrown by the resolver when a token is not found in a container.
4
- */
5
- export class ResolverError extends Error {
6
- constructor(message) {
7
- super(message);
8
- Object.setPrototypeOf(this, new.target.prototype);
9
- this.name = 'ResolverError';
10
- }
11
- }
12
- /** @internal */
13
- export const CONTAINER = token('ditox.Container');
14
- /** @internal */
15
- export const PARENT_CONTAINERS = token('ditox.ParentContainer');
16
- /** @internal */
17
- export const RESOLVER = token('ditox.Resolver');
18
- /** @internal */
19
- const NOT_FOUND = Symbol();
20
- /** @internal */
21
- const DEFAULT_SCOPE = 'singleton';
22
- /** @internal */
23
- export const FACTORIES_MAP = token('ditox.FactoriesMap');
24
- /** @internal */
25
- function getScope(options) {
26
- var _a;
27
- return (_a = options === null || options === void 0 ? void 0 : options.scope) !== null && _a !== void 0 ? _a : DEFAULT_SCOPE;
28
- }
29
- /** @internal */
30
- function getOnRemoved(options) {
31
- return options.scope === undefined ||
32
- options.scope === 'scoped' ||
33
- options.scope === 'singleton'
34
- ? options.onRemoved
35
- : undefined;
36
- }
37
- /** @internal */
38
- function isInternalToken(token) {
39
- return (token.symbol === CONTAINER.symbol ||
40
- token.symbol === PARENT_CONTAINERS.symbol ||
41
- token.symbol === RESOLVER.symbol);
42
- }
43
- /**
44
- * Creates a new dependency container.
45
- *
46
- * 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.
47
- *
48
- * @param parentArg - Optional parent container or an array of containers.
49
- */
50
- export function createContainer(parentArg) {
51
- const parents = parentArg
52
- ? Array.isArray(parentArg)
53
- ? [...parentArg]
54
- : [parentArg]
55
- : undefined;
56
- const values = new Map();
57
- const factories = new Map();
58
- const container = {
59
- bindValue(token, value) {
60
- if (isInternalToken(token)) {
61
- return;
62
- }
63
- values.set(token.symbol, value);
64
- },
65
- bindFactory(token, factory, options) {
66
- if (isInternalToken(token)) {
67
- return;
68
- }
69
- factories.set(token.symbol, { factory, options });
70
- },
71
- remove(token) {
72
- var _a;
73
- if (isInternalToken(token)) {
74
- return;
75
- }
76
- const options = (_a = factories.get(token.symbol)) === null || _a === void 0 ? void 0 : _a.options;
77
- if (options) {
78
- executeOnRemoved(token.symbol, options);
79
- }
80
- values.delete(token.symbol);
81
- factories.delete(token.symbol);
82
- },
83
- removeAll() {
84
- factories.forEach((context, tokenSymbol) => {
85
- if (context.options) {
86
- executeOnRemoved(tokenSymbol, context.options);
87
- }
88
- });
89
- values.clear();
90
- factories.clear();
91
- bindInternalTokens();
92
- },
93
- hasToken(token) {
94
- return (values.has(token.symbol) ||
95
- factories.has(token.symbol) ||
96
- parentContainersHaveToken(parents, token));
97
- },
98
- get(token) {
99
- const value = resolver(token, container);
100
- if (value !== NOT_FOUND) {
101
- return value;
102
- }
103
- if (token.isOptional) {
104
- return token.optionalValue;
105
- }
106
- return undefined;
107
- },
108
- resolve(token) {
109
- var _a;
110
- const value = resolver(token, container);
111
- if (value !== NOT_FOUND) {
112
- return value;
113
- }
114
- if (token.isOptional) {
115
- return token.optionalValue;
116
- }
117
- throw new ResolverError(`Token "${(_a = token.symbol.description) !== null && _a !== void 0 ? _a : ''}" is not provided`);
118
- },
119
- };
120
- function resolver(token, origin) {
121
- const value = values.get(token.symbol);
122
- const hasValue = value !== undefined || values.has(token.symbol);
123
- if (hasValue && origin === container) {
124
- return value;
125
- }
126
- const factoryContext = factories.get(token.symbol);
127
- if (factoryContext) {
128
- const scope = getScope(factoryContext.options);
129
- switch (scope) {
130
- case 'singleton': {
131
- if (hasValue) {
132
- return value;
133
- }
134
- else if (parentContainersHaveToken(parents, token)) {
135
- break;
136
- }
137
- else {
138
- // Cache the value in the same container where the factory is registered.
139
- const value = factoryContext.factory(container);
140
- container.bindValue(token, value);
141
- return value;
142
- }
143
- }
144
- case 'scoped': {
145
- if (hasValue) {
146
- return value;
147
- }
148
- else {
149
- // Create a value within the factory's container and cache it.
150
- const value = factoryContext.factory(container);
151
- container.bindValue(token, value);
152
- return value;
153
- }
154
- }
155
- case 'transient': {
156
- // Create a value within the origin container and don't cache it.
157
- return factoryContext.factory(origin);
158
- }
159
- }
160
- }
161
- if (hasValue) {
162
- return value;
163
- }
164
- if (parents) {
165
- return parentContainersResolveToken(parents, token, origin);
166
- }
167
- return NOT_FOUND;
168
- }
169
- function executeOnRemoved(tokenSymbol, options) {
170
- const onRemoved = getOnRemoved(options);
171
- if (onRemoved) {
172
- const value = values.get(tokenSymbol);
173
- if (value !== undefined || values.has(tokenSymbol)) {
174
- onRemoved(value);
175
- }
176
- }
177
- }
178
- function bindInternalTokens() {
179
- values.set(CONTAINER.symbol, container);
180
- values.set(RESOLVER.symbol, resolver);
181
- values.set(FACTORIES_MAP.symbol, factories);
182
- if (parents) {
183
- values.set(PARENT_CONTAINERS.symbol, parents);
184
- }
185
- }
186
- bindInternalTokens();
187
- return container;
188
- }
189
- function parentContainersHaveToken(parents, token) {
190
- if (!parents)
191
- return false;
192
- for (let i = 0; i < parents.length; i++) {
193
- if (parents[i].hasToken(token)) {
194
- return true;
195
- }
196
- }
197
- return false;
198
- }
199
- function parentContainersResolveToken(parents, token, origin) {
200
- for (const parentContainer of parents) {
201
- const parentResolver = parentContainer.get(RESOLVER);
202
- if (parentResolver) {
203
- const resolved = parentResolver(token, origin);
204
- if (resolved !== NOT_FOUND) {
205
- return resolved;
206
- }
207
- }
208
- }
209
- return NOT_FOUND;
210
- }
211
- //# sourceMappingURL=container.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"container.js","sourceRoot":"","sources":["../../src/container.ts"],"names":[],"mappings":"AAAA,OAAO,EAAQ,KAAK,EAAC,MAAM,UAAU,CAAC;AAEtC;;GAEG;AACH,MAAM,OAAO,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,MAAM,CAAC,cAAc,CAAC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAClD,IAAI,CAAC,IAAI,GAAG,eAAe,CAAC;IAC9B,CAAC;CACF;AAsED,gBAAgB;AAChB,MAAM,CAAC,MAAM,SAAS,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SAAS,EAAE;gBAC1B,OAAO,QAAQ,CAAC;aACjB;SACF;KACF;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -1,7 +0,0 @@
1
- export { token, optional } from './tokens';
2
- export type { RequiredToken, OptionalToken, Token } from './tokens';
3
- export { ResolverError, createContainer } from './container';
4
- export type { FactoryScope, FactoryOptions, Container } from './container';
5
- export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
6
- export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
7
- export type { Module, ModuleDeclaration, BindModuleOptions, ModuleBindingEntry, } from './modules';
package/dist/esm/index.js DELETED
@@ -1,5 +0,0 @@
1
- export { token, optional } from './tokens';
2
- export { ResolverError, createContainer } from './container';
3
- export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
4
- export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
5
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAC,MAAM,UAAU,CAAC;AAGzC,OAAO,EAAC,aAAa,EAAE,eAAe,EAAC,MAAM,aAAa,CAAC;AAG3D,OAAO,EACL,OAAO,EACP,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,UAAU,EACV,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,UAAU,EACV,WAAW,EACX,aAAa,EACb,qBAAqB,GACtB,MAAM,WAAW,CAAC"}
@@ -1,121 +0,0 @@
1
- import { Container } from './container';
2
- import { Token } from './tokens';
3
- type AnyObject = Record<string, any>;
4
- type EmptyObject = Record<string, never>;
5
- type ModuleController = {
6
- /** Dispose the module and clean its resources */
7
- destroy?: () => void;
8
- };
9
- /**
10
- * Dependency module
11
- *
12
- * @example
13
- * ```ts
14
- * type LoggerModule = Module<{
15
- * logger: Logger;
16
- * }>;
17
- * ```
18
- */
19
- export type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
20
- type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
21
- /**
22
- * Description how to bind the module in declarative way.
23
- *
24
- * @example
25
- * ```ts
26
- * const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
27
- * token: LOGGER_MODULE_TOKEN,
28
- * factory: (container) => {
29
- * const transport = container.resolve(TRANSPORT_TOKEN).open();
30
- * return {
31
- * logger: { log: (message) => transport.write(message) },
32
- * destroy: () => transport.close(),
33
- * }
34
- * },
35
- * exports: {
36
- * logger: LOGGER_TOKEN,
37
- * },
38
- * };
39
- * ```
40
- */
41
- export type ModuleDeclaration<T extends Module<AnyObject>> = {
42
- /** Token for the module */
43
- token: Token<T>;
44
- /** Modules for binding */
45
- imports?: ReadonlyArray<ModuleBindingEntry>;
46
- /** Factory of the module */
47
- factory: (container: Container) => T;
48
- /** Dictionary of module properties which are bound to tokens. */
49
- exports?: {
50
- [K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
51
- };
52
- /** Callback could be used to prepare an environment. It is called before binding the module. */
53
- beforeBinding?: (container: Container) => void;
54
- /** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
55
- afterBinding?: (container: Container) => void;
56
- };
57
- export type AnyModuleDeclaration = ModuleDeclaration<Module<AnyObject>>;
58
- /**
59
- * Options for module binding.
60
- *
61
- * `scope` types:
62
- * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function.
63
- * - `scoped` - The value is created and cached by the nearest container which owns the factory function.
64
- */
65
- export type BindModuleOptions = {
66
- scope?: 'scoped' | 'singleton';
67
- };
68
- type ModuleDeclarationWithOptions = {
69
- module: ModuleDeclaration<AnyObject>;
70
- options: BindModuleOptions;
71
- };
72
- export type ModuleBindingEntry = ModuleDeclaration<AnyObject> | ModuleDeclarationWithOptions;
73
- /**
74
- * Binds the dependency module to the container
75
- * @param container - Dependency container.
76
- * @param moduleDeclaration - Declaration of the dependency module.
77
- * @param options - Options for module binding.
78
- *
79
- * @example
80
- * ```ts
81
- * bindModule(container, LOGGER_MODULE);
82
- * ```
83
- */
84
- export declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
85
- /**
86
- * Binds dependency modules to the container
87
- *
88
- * @param container - Dependency container for binding
89
- * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
90
- */
91
- export declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
92
- /**
93
- * Declares a module binding
94
- *
95
- * @param declaration - a module declaration
96
- * @param declaration.token - optional field
97
- *
98
- * @example
99
- * ```ts
100
- * const LOGGER_MODULE = declareModule<LoggerModule>({
101
- * factory: (container) => {
102
- * const transport = container.resolve(TRANSPORT_TOKEN).open();
103
- * return {
104
- * logger: { log: (message) => transport.write(message) },
105
- * destroy: () => transport.close(),
106
- * }
107
- * },
108
- * exports: {
109
- * logger: LOGGER_TOKEN,
110
- * },
111
- * });
112
- * ```
113
- */
114
- export declare function declareModule<T extends Module<AnyObject>>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
115
- /**
116
- * Declares bindings of several modules
117
- *
118
- * @param modules - module declaration entries
119
- */
120
- export declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
121
- export {};