ditox 1.4.1 → 2.2.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 (69) hide show
  1. package/README.md +6 -5
  2. package/dist/{index.d.ts → browser/index.d.ts} +98 -42
  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 +5 -0
  9. package/dist/cjs/index.js +23 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/modules.d.ts +118 -0
  12. package/dist/cjs/modules.js +109 -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 +5 -0
  24. package/dist/esm/index.js +4 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/modules.d.ts +118 -0
  27. package/dist/esm/modules.js +102 -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 +98 -42
  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 +250 -164
  40. package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
  41. package/package.json +15 -16
  42. package/src/ditox.test.ts +10 -1
  43. package/src/index.ts +13 -13
  44. package/src/modules.test.ts +2 -0
  45. package/src/modules.ts +1 -8
  46. package/src/utils.test.ts +173 -58
  47. package/src/utils.ts +164 -66
  48. package/CHANGELOG.md +0 -121
  49. package/dist/index.browser.js +0 -2
  50. package/dist/index.browser.js.flow +0 -4
  51. package/dist/index.browser.js.map +0 -1
  52. package/dist/index.js +0 -411
  53. package/dist/index.js.flow +0 -4
  54. package/dist/index.js.map +0 -1
  55. package/dist/index.module.d.ts +0 -301
  56. package/dist/index.module.js +0 -393
  57. package/dist/index.module.js.flow +0 -4
  58. package/dist/index.module.js.map +0 -1
  59. package/dist/index.umd.d.ts +0 -301
  60. package/dist/index.umd.js +0 -2
  61. package/dist/index.umd.js.flow +0 -4
  62. package/dist/index.umd.js.map +0 -1
  63. package/lib/index.js.flow +0 -4
  64. package/lib/index.js.map +0 -1
  65. package/mod.ts +0 -2
  66. package/src/ditox.js.flow +0 -131
  67. package/src/index.js.flow +0 -34
  68. package/src/modules.js.flow +0 -156
  69. package/src/utils.js.flow +0 -113
@@ -0,0 +1,59 @@
1
+ import type { Container, Token } from './ditox';
2
+ declare type ValuesProps = {
3
+ [key: string]: unknown;
4
+ };
5
+ declare type TokenProps<Props extends ValuesProps> = {
6
+ [K in keyof Props]: Token<Props[K]>;
7
+ };
8
+ /**
9
+ * Returns an array of resolved values by the specified token.
10
+ * If a token is not found, then `undefined` value is used.
11
+ *
12
+ * @deprecated Use `tryResolveValues()` function.
13
+ */
14
+ export declare function getValues<Tokens extends Token<unknown>[], Values extends {
15
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
16
+ }>(container: Container, ...tokens: Tokens): Values;
17
+ /**
18
+ * Returns an object with resolved properties which are specified by token properties.
19
+ * If a token is not found, then `undefined` value is used.
20
+ *
21
+ * @deprecated Use `tryResolveValue()` function.
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const props = getProps(container, {a: tokenA, b: tokenB});
26
+ * console.log(props); // {a: 1, b: 2}
27
+ * ```
28
+ */
29
+ export declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
30
+ /**
31
+ * Returns an object with resolved properties which are specified by token properties.
32
+ * If a token is not found, then `ResolverError` is thrown.
33
+ *
34
+ * @deprecated Use `resolveValue()` function.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const props = resolveProps(container, {a: tokenA, b: tokenB});
39
+ * console.log(props); // {a: 1, b: 2}
40
+ * ```
41
+ */
42
+ export declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
43
+ /**
44
+ * Decorates a factory by passing a resolved object with tokens as the first argument.
45
+ * @param factory - A factory.
46
+ * @param tokens - Object with tokens.
47
+ * @return Decorated factory which takes a dependency container as a single argument.
48
+ *
49
+ * @deprecated Use `injectable()` function.
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * const factory = ({a, b}: {a: number, b: number}) => a + b;
54
+ * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
55
+ * const result = decoratedFactory(container);
56
+ * ```
57
+ */
58
+ export declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
59
+ export {};
@@ -0,0 +1,59 @@
1
+ import { injectable, resolveValue, tryResolveValue, tryResolveValues, } from './utils';
2
+ /**
3
+ * Returns an array of resolved values by the specified token.
4
+ * If a token is not found, then `undefined` value is used.
5
+ *
6
+ * @deprecated Use `tryResolveValues()` function.
7
+ */
8
+ export function getValues(container, ...tokens) {
9
+ return tryResolveValues(container, ...tokens);
10
+ }
11
+ /**
12
+ * Returns an object with resolved properties which are specified by token properties.
13
+ * If a token is not found, then `undefined` value is used.
14
+ *
15
+ * @deprecated Use `tryResolveValue()` function.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * const props = getProps(container, {a: tokenA, b: tokenB});
20
+ * console.log(props); // {a: 1, b: 2}
21
+ * ```
22
+ */
23
+ export function getProps(container, tokens) {
24
+ return tryResolveValue(container, tokens);
25
+ }
26
+ /**
27
+ * Returns an object with resolved properties which are specified by token properties.
28
+ * If a token is not found, then `ResolverError` is thrown.
29
+ *
30
+ * @deprecated Use `resolveValue()` function.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const props = resolveProps(container, {a: tokenA, b: tokenB});
35
+ * console.log(props); // {a: 1, b: 2}
36
+ * ```
37
+ */
38
+ export function resolveProps(container, tokens) {
39
+ return resolveValue(container, tokens);
40
+ }
41
+ /**
42
+ * Decorates a factory by passing a resolved object with tokens as the first argument.
43
+ * @param factory - A factory.
44
+ * @param tokens - Object with tokens.
45
+ * @return Decorated factory which takes a dependency container as a single argument.
46
+ *
47
+ * @deprecated Use `injectable()` function.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const factory = ({a, b}: {a: number, b: number}) => a + b;
52
+ * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
53
+ * const result = decoratedFactory(container);
54
+ * ```
55
+ */
56
+ export function injectableProps(factory, tokens) {
57
+ return injectable(factory, tokens);
58
+ }
59
+ //# sourceMappingURL=utils-deprecated.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils-deprecated.js","sourceRoot":"","sources":["../../src/utils-deprecated.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,YAAY,EACZ,eAAe,EACf,gBAAgB,GACjB,MAAM,SAAS,CAAC;AAOjB;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAKvB,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,gBAAgB,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CACtB,SAAoB,EACpB,MAAyB;IAEzB,OAAO,eAAe,CAAC,SAAS,EAAE,MAAM,CAAmB,CAAC;AAC9D,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAC1B,SAAoB,EACpB,MAAyB;IAEzB,OAAO,YAAY,CAAC,SAAS,EAAE,MAAM,CAAU,CAAC;AAClD,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAiC,EACjC,MAAyB;IAEzB,OAAO,UAAU,CAAC,OAAc,EAAE,MAAM,CAAC,CAAC;AAC5C,CAAC"}
@@ -0,0 +1,136 @@
1
+ import type { Container, Token } from './ditox';
2
+ declare type ValuesProps = {
3
+ [key: string]: unknown;
4
+ };
5
+ declare type TokenProps<Props extends ValuesProps> = {
6
+ [K in keyof Props]: Token<Props[K]>;
7
+ };
8
+ /**
9
+ * Checks if a value is the token
10
+ */
11
+ export declare function isToken<T>(value: unknown): value is Token<T>;
12
+ /**
13
+ * Rebinds the array by the token with added new value.
14
+ * @param container - Dependency container.
15
+ * @param token - Token for an array of values.
16
+ * @param value - New value which is added to the end of the array.
17
+ */
18
+ export declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
19
+ /**
20
+ * Tries to resolve a value by the provided token.
21
+ *
22
+ * If an argument is an object which has tokens as its properties,
23
+ * then returns an object containing resolved values as properties.
24
+
25
+ * If a token is not found, then `undefined` value is used.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * const value = tryResolveValue(container, tokenA);
30
+ * console.log(value); // 1
31
+ *
32
+ * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
33
+ * console.log(props); // {a: 1, b: 2}
34
+ * ```
35
+ */
36
+ export declare function tryResolveValue<Tokens extends Token<unknown> | {
37
+ [key: string]: Token<unknown>;
38
+ }, Values extends Tokens extends Token<infer V> ? V | undefined : Tokens extends TokenProps<infer Props> ? Partial<Props> : never>(container: Container, token: Tokens): Values;
39
+ /**
40
+ * Returns an array of resolved values or objects with resolved values.
41
+ *
42
+ * If an item of the array is an object which has tokens as its properties,
43
+ * then returns an object containing resolved values as properties.
44
+
45
+ * If a token is not found, then `undefined` value is used.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const items1 = tryResolveValues(container, tokenA);
50
+ * console.log(items1); // [1]
51
+ *
52
+ * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
53
+ * console.log(items2); // [1, {a: 1, b: 2}]
54
+ * ```
55
+ */
56
+ export declare function tryResolveValues<Tokens extends (Token<unknown> | {
57
+ [key: string]: Token<unknown>;
58
+ })[], Values extends {
59
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V | undefined : Tokens[K] extends TokenProps<infer Props> ? Partial<Props> : never;
60
+ }>(container: Container, ...tokens: Tokens): Values;
61
+ /**
62
+ * Resolves a value by the provided token.
63
+ *
64
+ * If an argument is an object which has tokens as its properties,
65
+ * then returns an object containing resolved values as properties.
66
+
67
+ * If a value is not found by the token, then `ResolverError` is thrown.
68
+ *
69
+ * @example
70
+ * ```ts
71
+ * const value = resolveValue(container, tokenA);
72
+ * console.log(value); // 1
73
+ *
74
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
75
+ * console.log(props); // {a: 1, b: 2}
76
+ * ```
77
+ */
78
+ export declare function resolveValue<Tokens extends Token<unknown> | {
79
+ [key: string]: Token<unknown>;
80
+ }, Values extends Tokens extends Token<infer V> ? V : Tokens extends TokenProps<infer Props> ? Props : never>(container: Container, token: Tokens): Values;
81
+ /**
82
+ * Returns an array of resolved values or objects with resolved values.
83
+ *
84
+ * If an item of the array is an object which has tokens as its properties,
85
+ * then returns an object containing resolved values as properties.
86
+
87
+ * If a token is not found, then `ResolverError` is thrown.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const items1 = resolveValues(container, tokenA);
92
+ * console.log(items1); // [1]
93
+ *
94
+ * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
95
+ * console.log(items2); // [1, {a: 1, b: 2}]
96
+ * ```
97
+ */
98
+ export declare function resolveValues<Tokens extends (Token<unknown> | {
99
+ [key: string]: Token<unknown>;
100
+ })[], Values extends {
101
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
102
+ }>(container: Container, ...tokens: Tokens): Values;
103
+ /**
104
+ * Decorates a factory by passing resolved values as factory arguments.
105
+ *
106
+ * If an argument is an object which has tokens as its properties,
107
+ * then returns an object containing resolved values as properties.
108
+ *
109
+ * @param factory - A factory.
110
+ * @param tokens - Tokens which correspond to factory arguments.
111
+ *
112
+ * @return Decorated factory which takes a dependency container as a single argument.
113
+ */
114
+ export declare function injectable<Tokens extends (Token<unknown> | {
115
+ [key: string]: Token<unknown>;
116
+ })[], Values extends {
117
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
118
+ }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
119
+ /**
120
+ * Decorates a class by passing resolved values as arguments to its constructor.
121
+ *
122
+ * If an argument is an object which has tokens as its properties,
123
+ * then returns an object containing resolved values as properties.
124
+ *
125
+ * @param constructor - Constructor of a class
126
+ * @param tokens - Tokens which correspond to constructor arguments
127
+ *
128
+ * @return A factory function which takes a dependency container as a single argument
129
+ * and returns a new created class.
130
+ */
131
+ export declare function injectableClass<Tokens extends (Token<unknown> | {
132
+ [key: string]: Token<unknown>;
133
+ })[], Values extends {
134
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
135
+ }, Result>(this: unknown, constructor: new (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
136
+ export {};
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Checks if a value is the token
3
+ */
4
+ export function isToken(value) {
5
+ return (value !== undefined &&
6
+ value !== null &&
7
+ typeof value === 'object' &&
8
+ 'symbol' in value &&
9
+ typeof value.symbol === 'symbol');
10
+ }
11
+ /**
12
+ * Rebinds the array by the token with added new value.
13
+ * @param container - Dependency container.
14
+ * @param token - Token for an array of values.
15
+ * @param value - New value which is added to the end of the array.
16
+ */
17
+ export function bindMultiValue(container, token, value) {
18
+ var _a;
19
+ const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
20
+ const nextValues = [...prevValues, value];
21
+ container.bindValue(token, nextValues);
22
+ }
23
+ /**
24
+ * Tries to resolve a value by the provided token.
25
+ *
26
+ * If an argument is an object which has tokens as its properties,
27
+ * then returns an object containing resolved values as properties.
28
+
29
+ * If a token is not found, then `undefined` value is used.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const value = tryResolveValue(container, tokenA);
34
+ * console.log(value); // 1
35
+ *
36
+ * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
37
+ * console.log(props); // {a: 1, b: 2}
38
+ * ```
39
+ */
40
+ export function tryResolveValue(container, token) {
41
+ if (isToken(token)) {
42
+ return container.get(token);
43
+ }
44
+ const obj = {};
45
+ Object.keys(token).forEach((key) => (obj[key] = container.get(token[key])));
46
+ return obj;
47
+ }
48
+ /**
49
+ * Returns an array of resolved values or objects with resolved values.
50
+ *
51
+ * If an item of the array is an object which has tokens as its properties,
52
+ * then returns an object containing resolved values as properties.
53
+
54
+ * If a token is not found, then `undefined` value is used.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * const items1 = tryResolveValues(container, tokenA);
59
+ * console.log(items1); // [1]
60
+ *
61
+ * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
62
+ * console.log(items2); // [1, {a: 1, b: 2}]
63
+ * ```
64
+ */
65
+ export function tryResolveValues(container, ...tokens) {
66
+ return tokens.map((item) => tryResolveValue(container, item));
67
+ }
68
+ /**
69
+ * Resolves a value by the provided token.
70
+ *
71
+ * If an argument is an object which has tokens as its properties,
72
+ * then returns an object containing resolved values as properties.
73
+
74
+ * If a value is not found by the token, then `ResolverError` is thrown.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const value = resolveValue(container, tokenA);
79
+ * console.log(value); // 1
80
+ *
81
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
82
+ * console.log(props); // {a: 1, b: 2}
83
+ * ```
84
+ */
85
+ export function resolveValue(container, token) {
86
+ if (isToken(token)) {
87
+ return container.resolve(token);
88
+ }
89
+ const obj = {};
90
+ Object.keys(token).forEach((key) => (obj[key] = container.resolve(token[key])));
91
+ return obj;
92
+ }
93
+ /**
94
+ * Returns an array of resolved values or objects with resolved values.
95
+ *
96
+ * If an item of the array is an object which has tokens as its properties,
97
+ * then returns an object containing resolved values as properties.
98
+
99
+ * If a token is not found, then `ResolverError` is thrown.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * const items1 = resolveValues(container, tokenA);
104
+ * console.log(items1); // [1]
105
+ *
106
+ * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
107
+ * console.log(items2); // [1, {a: 1, b: 2}]
108
+ * ```
109
+ */
110
+ export function resolveValues(container, ...tokens) {
111
+ return tokens.map((item) => resolveValue(container, item));
112
+ }
113
+ /**
114
+ * Decorates a factory by passing resolved values as factory arguments.
115
+ *
116
+ * If an argument is an object which has tokens as its properties,
117
+ * then returns an object containing resolved values as properties.
118
+ *
119
+ * @param factory - A factory.
120
+ * @param tokens - Tokens which correspond to factory arguments.
121
+ *
122
+ * @return Decorated factory which takes a dependency container as a single argument.
123
+ */
124
+ export function injectable(factory, ...tokens) {
125
+ return (container) => {
126
+ const values = resolveValues(container, ...tokens);
127
+ return factory.apply(this, values);
128
+ };
129
+ }
130
+ /**
131
+ * Decorates a class by passing resolved values as arguments to its constructor.
132
+ *
133
+ * If an argument is an object which has tokens as its properties,
134
+ * then returns an object containing resolved values as properties.
135
+ *
136
+ * @param constructor - Constructor of a class
137
+ * @param tokens - Tokens which correspond to constructor arguments
138
+ *
139
+ * @return A factory function which takes a dependency container as a single argument
140
+ * and returns a new created class.
141
+ */
142
+ export function injectableClass(constructor, ...tokens) {
143
+ return injectable((...values) => new constructor(...values), ...tokens);
144
+ }
145
+ //# sourceMapp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"}
@@ -108,72 +108,134 @@ declare type ValuesProps = {
108
108
  declare type TokenProps<Props extends ValuesProps> = {
109
109
  [K in keyof Props]: Token<Props[K]>;
110
110
  };
111
+ /**
112
+ * Checks if a value is the token
113
+ */
114
+ declare function isToken<T>(value: unknown): value is Token<T>;
111
115
  /**
112
116
  * Rebinds the array by the token with added new value.
113
117
  * @param container - Dependency container.
114
118
  * @param token - Token for an array of values.
115
119
  * @param value - New value which is added to the end of the array.
116
120
  */
117
- declare function bindMultiValue<T>(container: Container, token: Token<Array<T>>, value: T): void;
121
+ declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
118
122
  /**
119
- * Returns an array of resolved values by the specified token.
123
+ * Tries to resolve a value by the provided token.
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
+
120
128
  * If a token is not found, then `undefined` value is used.
129
+ *
130
+ * @example
131
+ * ```ts
132
+ * const value = tryResolveValue(container, tokenA);
133
+ * console.log(value); // 1
134
+ *
135
+ * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
136
+ * console.log(props); // {a: 1, b: 2}
137
+ * ```
121
138
  */
122
- declare function getValues<Tokens extends Token<unknown>[], Values extends {
123
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
124
- }>(container: Container, ...tokens: Tokens): Values;
139
+ declare function tryResolveValue<Tokens extends Token<unknown> | {
140
+ [key: string]: Token<unknown>;
141
+ }, Values extends Tokens extends Token<infer V> ? V | undefined : Tokens extends TokenProps<infer Props> ? Partial<Props> : never>(container: Container, token: Tokens): Values;
125
142
  /**
126
- * Returns an array of resolved values by the specified token.
127
- * If a token is not found, then `ResolverError` is thrown.
143
+ * Returns an array of resolved values or objects with resolved values.
144
+ *
145
+ * If an item of the array is an object which has tokens as its properties,
146
+ * then returns an object containing resolved values as properties.
147
+
148
+ * If a token is not found, then `undefined` value is used.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * const items1 = tryResolveValues(container, tokenA);
153
+ * console.log(items1); // [1]
154
+ *
155
+ * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
156
+ * console.log(items2); // [1, {a: 1, b: 2}]
157
+ * ```
128
158
  */
129
- declare function resolveValues<Tokens extends Token<unknown>[], Values extends {
130
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
159
+ declare function tryResolveValues<Tokens extends (Token<unknown> | {
160
+ [key: string]: Token<unknown>;
161
+ })[], Values extends {
162
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V | undefined : Tokens[K] extends TokenProps<infer Props> ? Partial<Props> : never;
131
163
  }>(container: Container, ...tokens: Tokens): Values;
132
164
  /**
133
- * Decorates a factory by passing resolved tokens as factory arguments.
134
- * @param factory - A factory.
135
- * @param tokens - Tokens which correspond to factory arguments.
136
- * @return Decorated factory which takes a dependency container as a single argument.
137
- */
138
- declare function injectable<Tokens extends Token<unknown>[], Values extends {
139
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
140
- }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
141
- /**
142
- * Returns an object with resolved properties which are specified by token properties.
143
- * If a token is not found, then `undefined` value is used.
165
+ * Resolves a value by the provided token.
166
+ *
167
+ * If an argument is an object which has tokens as its properties,
168
+ * then returns an object containing resolved values as properties.
169
+
170
+ * If a value is not found by the token, then `ResolverError` is thrown.
144
171
  *
145
172
  * @example
146
173
  * ```ts
147
- * const props = getProps(container, {a: tokenA, b: tokenB});
174
+ * const value = resolveValue(container, tokenA);
175
+ * console.log(value); // 1
176
+ *
177
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
148
178
  * console.log(props); // {a: 1, b: 2}
149
179
  * ```
150
180
  */
151
- declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
181
+ declare function resolveValue<Tokens extends Token<unknown> | {
182
+ [key: string]: Token<unknown>;
183
+ }, Values extends Tokens extends Token<infer V> ? V : Tokens extends TokenProps<infer Props> ? Props : never>(container: Container, token: Tokens): Values;
152
184
  /**
153
- * Returns an object with resolved properties which are specified by token properties.
185
+ * Returns an array of resolved values or objects with resolved values.
186
+ *
187
+ * If an item of the array is an object which has tokens as its properties,
188
+ * then returns an object containing resolved values as properties.
189
+
154
190
  * If a token is not found, then `ResolverError` is thrown.
155
191
  *
156
192
  * @example
157
193
  * ```ts
158
- * const props = resolveProps(container, {a: tokenA, b: tokenB});
159
- * console.log(props); // {a: 1, b: 2}
194
+ * const items1 = resolveValues(container, tokenA);
195
+ * console.log(items1); // [1]
196
+ *
197
+ * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
198
+ * console.log(items2); // [1, {a: 1, b: 2}]
160
199
  * ```
161
200
  */
162
- declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
201
+ declare function resolveValues<Tokens extends (Token<unknown> | {
202
+ [key: string]: Token<unknown>;
203
+ })[], Values extends {
204
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
205
+ }>(container: Container, ...tokens: Tokens): Values;
163
206
  /**
164
- * Decorates a factory by passing a resolved object with tokens as the first argument.
207
+ * Decorates a factory by passing resolved values as factory arguments.
208
+ *
209
+ * If an argument is an object which has tokens as its properties,
210
+ * then returns an object containing resolved values as properties.
211
+ *
165
212
  * @param factory - A factory.
166
- * @param tokens - Object with tokens.
213
+ * @param tokens - Tokens which correspond to factory arguments.
214
+ *
167
215
  * @return Decorated factory which takes a dependency container as a single argument.
216
+ */
217
+ declare function injectable<Tokens extends (Token<unknown> | {
218
+ [key: string]: Token<unknown>;
219
+ })[], Values extends {
220
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
221
+ }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
222
+ /**
223
+ * Decorates a class by passing resolved values as arguments to its constructor.
168
224
  *
169
- * @example
170
- * ```ts
171
- * const factory = ({a, b}: {a: number, b: number}) => a + b;
172
- * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
173
- * const result = decoratedFactory(container);
174
- * ```
225
+ * If an argument is an object which has tokens as its properties,
226
+ * then returns an object containing resolved values as properties.
227
+ *
228
+ * @param constructor - Constructor of a class
229
+ * @param tokens - Tokens which correspond to constructor arguments
230
+ *
231
+ * @return A factory function which takes a dependency container as a single argument
232
+ * and returns a new created class.
175
233
  */
176
- declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
234
+ declare function injectableClass<Tokens extends (Token<unknown> | {
235
+ [key: string]: Token<unknown>;
236
+ })[], Values extends {
237
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : Tokens[K] extends TokenProps<infer Props> ? Props : never;
238
+ }, Result>(this: unknown, constructor: new (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
177
239
 
178
240
  declare type AnyObject = Record<string, any>;
179
241
  declare type EmptyObject = Record<string, never>;
@@ -224,12 +286,6 @@ declare type ModuleDeclaration<T extends Module<AnyObject>> = {
224
286
  exports?: {
225
287
  [K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
226
288
  };
227
- /**
228
- * Dictionary of module properties which are bound to tokens.
229
- *
230
- * @deprecated Use `exports` property.
231
- */
232
- exportedProps?: ModuleDeclaration<T>['exports'];
233
289
  /** Callback could be used to prepare an environment. It is called before binding the module. */
234
290
  beforeBinding?: (container: Container) => void;
235
291
  /** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
@@ -298,4 +354,4 @@ declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token
298
354
  */
299
355
  declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
300
356
 
301
- export { BindModuleOptions, Container, FactoryOptions, FactoryScope, Module, ModuleBindingEntry, ModuleDeclaration, OptionalToken, RequiredToken, ResolverError, Token, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, getProps, getValues, injectable, injectableProps, optional, resolveProps, resolveValues, token };
357
+ 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 };
@@ -0,0 +1,2 @@
1
+ !function(o,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((o="undefined"!=typeof globalThis?globalThis:o||self).Ditox={})}(this,(function(o){"use strict";var n=function(o,t){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(o,n){o.__proto__=n}||function(o,n){for(var t in n)Object.prototype.hasOwnProperty.call(n,t)&&(o[t]=n[t])},n(o,t)};var t=function(){return t=Object.assign||function(o){for(var n,t=1,e=arguments.length;t<e;t++)for(var r in n=arguments[t])Object.prototype.hasOwnProperty.call(n,r)&&(o[r]=n[r]);return o},t.apply(this,arguments)};function e(o,n,t){if(t||2===arguments.length)for(var e,r=0,i=n.length;r<i;r++)!e&&r in n||(e||(e=Array.prototype.slice.call(n,0,r)),e[r]=n[r]);return o.concat(e||Array.prototype.slice.call(n))}function r(o){return{symbol:Symbol(o)}}var i=function(o){function t(n){var t=this.constructor,e=o.call(this,n)||this;return Object.setPrototypeOf(e,t.prototype),e.name="ResolverError",e}return function(o,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function e(){this.constructor=o}n(o,t),o.prototype=null===t?Object.create(t):(e.prototype=t.prototype,new e)}(t,o),t}(Error),l=r("ditox.Container"),u=r("ditox.ParentContainer"),a=r("ditox.Resolver"),s=Symbol(),c=function(){throw new Error("FAKE_FACTORY")},f=r("ditox.FactoriesMap");function p(o){return o.symbol===l.symbol||o.symbol===u.symbol||o.symbol===a.symbol}function y(o){return null!=o&&"object"==typeof o&&"symbol"in o&&"symbol"==typeof o.symbol}function d(o,n){if(y(n))return o.get(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.get(n[e])})),t}function v(o,n){if(y(n))return o.resolve(n);var t={};return Object.keys(n).forEach((function(e){return t[e]=o.resolve(n[e])})),t}function b(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return v(o,n)}))}function m(o){for(var n=this,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return function(r){var i=b.apply(void 0,e([r],t,!1));return o.apply(n,i)}}function h(o,n,t){var e=n.token,r=n.imports,i=n.factory,l=n.beforeBinding,u=n.afterBinding,a=n.exports,s=null==t?void 0:t.scope;l&&l(o),r&&g(o,r);var c=new Set;a&&Object.keys(a).forEach((function(n){var t=a[n];t&&(c.add(t),o.bindFactory(t,m((function(o){return o[n]}),e),{scope:s}))}));o.bindFactory(e,i,{scope:s,onRemoved:function(n){n.destroy&&n.destroy(),c.forEach((function(n){return o.remove(n)})),c.clear()}}),u&&u(o)}function g(o,n){n.forEach((function(n){"module"in n?h(o,n.module,n.options):h(o,n)}))}function O(o){var n;return t(t({},o),{token:null!==(n=o.token)&&void 0!==n?n:r()})}o.ResolverError=i,o.bindModule=h,o.bindModules=g,o.bindMultiValue=function(o,n,t){var r,i=e(e([],null!==(r=o.get(n))&&void 0!==r?r:[],!0),[t],!1);o.bindValue(n,i)},o.createContainer=function(o){var n=new Map,t=new Map,e={bindValue:function(o,t){p(o)||n.set(o.symbol,t)},bindFactory:function(o,n,e){p(o)||t.set(o.symbol,{factory:n,options:e})},remove:function(o){var e;if(!p(o)){var r=null===(e=t.get(o.symbol))||void 0===e?void 0:e.options;r&&y(o.symbol,r),n.delete(o.symbol),t.delete(o.symbol)}},removeAll:function(){t.forEach((function(o,n){o.options&&y(n,o.options)})),n.clear(),t.clear(),d()},hasToken:function(e){var r;return n.has(e.symbol)||t.has(e.symbol)||null!==(r=null==o?void 0:o.hasToken(e))&&void 0!==r&&r},get:function(o){var n=r(o,e);return n!==s?n:o.isOptional?o.optionalValue:void 0},resolve:function(o){var n,t=r(o,e);if(t!==s)return t;if(o.isOptional)return o.optionalValue;throw new i('Token "'.concat(null!==(n=o.symbol.description)&&void 0!==n?n:"",'" is not provided'))}};function r(r,i){var l=n.get(r.symbol),u=void 0!==l||n.has(r.symbol);if(u&&i===e)return l;var f,p,y=t.get(r.symbol);if(y&&y.factory!==c)switch(f=y.options,null!==(p=null==f?void 0:f.scope)&&void 0!==p?p:"singleton"){case"singleton":if(u)return l;if(null==o?void 0:o.hasToken(r))break;var d=y.factory(e);return e.bindValue(r,d),d;case"scoped":var v=y.factory(i);return i.bindValue(r,v),i!==e&&i.bindFactory(r,c,y.options),v;case"transient":return y.factory(i)}if(u)return l;var b=null==o?void 0:o.get(a);return b?b(r,i):s}function y(o,t){var e=function(o){return void 0===o.scope||"scoped"===o.scope||"singleton"===o.scope?o.onRemoved:void 0}(t);if(e){var r=n.get(o);(void 0!==r||n.has(o))&&e(r)}}function d(){n.set(l.symbol,e),n.set(a.symbol,r),n.set(f.symbol,t),o&&n.set(u.symbol,o)}return d(),e},o.declareModule=O,o.declareModuleBindings=function(o){return O({factory:function(){return{}},imports:o})},o.injectable=m,o.injectableClass=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return m.apply(void 0,e([function(){for(var n=[],t=0;t<arguments.length;t++)n[t]=arguments[t];return new(o.bind.apply(o,e([void 0],n,!1)))}],n,!1))},o.isToken=y,o.optional=function(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}},o.resolveValue=v,o.resolveValues=b,o.token=r,o.tryResolveValue=d,o.tryResolveValues=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return d(o,n)}))},Object.defineProperty(o,"__esModule",{value:!0})}));
2
+ //# sourceMappingURL=index.js.map