ditox 2.0.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.
@@ -5,6 +5,10 @@ declare type ValuesProps = {
5
5
  declare type TokenProps<Props extends ValuesProps> = {
6
6
  [K in keyof Props]: Token<Props[K]>;
7
7
  };
8
+ /**
9
+ * Checks if a value is the token
10
+ */
11
+ export declare function isToken<T>(value: unknown): value is Token<T>;
8
12
  /**
9
13
  * Rebinds the array by the token with added new value.
10
14
  * @param container - Dependency container.
@@ -13,62 +17,120 @@ declare type TokenProps<Props extends ValuesProps> = {
13
17
  */
14
18
  export declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
15
19
  /**
16
- * Returns an array of resolved values by the specified token.
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
+
17
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
+ * ```
18
35
  */
19
- export declare function getValues<Tokens extends Token<unknown>[], Values extends {
20
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
21
- }>(container: Container, ...tokens: Tokens): Values;
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;
22
39
  /**
23
- * Returns an array of resolved values by the specified token.
24
- * If a token is not found, then `ResolverError` is thrown.
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
+ * ```
25
55
  */
26
- export declare function resolveValues<Tokens extends Token<unknown>[], Values extends {
27
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
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;
28
60
  }>(container: Container, ...tokens: Tokens): Values;
29
61
  /**
30
- * Decorates a factory by passing resolved tokens as factory arguments.
31
- * @param factory - A factory.
32
- * @param tokens - Tokens which correspond to factory arguments.
33
- * @return Decorated factory which takes a dependency container as a single argument.
34
- */
35
- export declare function injectable<Tokens extends Token<unknown>[], Values extends {
36
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
37
- }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
38
- /**
39
- * Returns an object with resolved properties which are specified by token properties.
40
- * If a token is not found, then `undefined` value is used.
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.
41
68
  *
42
69
  * @example
43
70
  * ```ts
44
- * const props = getProps(container, {a: tokenA, b: tokenB});
71
+ * const value = resolveValue(container, tokenA);
72
+ * console.log(value); // 1
73
+ *
74
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
45
75
  * console.log(props); // {a: 1, b: 2}
46
76
  * ```
47
77
  */
48
- export declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
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;
49
81
  /**
50
- * Returns an object with resolved properties which are specified by token properties.
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
+
51
87
  * If a token is not found, then `ResolverError` is thrown.
52
88
  *
53
89
  * @example
54
90
  * ```ts
55
- * const props = resolveProps(container, {a: tokenA, b: tokenB});
56
- * console.log(props); // {a: 1, b: 2}
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}]
57
96
  * ```
58
97
  */
59
- export declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
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;
60
103
  /**
61
- * Decorates a factory by passing a resolved object with tokens as the first argument.
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
+ *
62
109
  * @param factory - A factory.
63
- * @param tokens - Object with tokens.
110
+ * @param tokens - Tokens which correspond to factory arguments.
111
+ *
64
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.
65
121
  *
66
- * @example
67
- * ```ts
68
- * const factory = ({a, b}: {a: number, b: number}) => a + b;
69
- * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
70
- * const result = decoratedFactory(container);
71
- * ```
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.
72
130
  */
73
- export declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
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;
74
136
  export {};
package/dist/esm/utils.js CHANGED
@@ -1,3 +1,13 @@
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
+ }
1
11
  /**
2
12
  * Rebinds the array by the token with added new value.
3
13
  * @param container - Dependency container.
@@ -11,78 +21,125 @@ export function bindMultiValue(container, token, value) {
11
21
  container.bindValue(token, nextValues);
12
22
  }
13
23
  /**
14
- * Returns an array of resolved values by the specified token.
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
+
15
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
+ * ```
16
39
  */
17
- export function getValues(container, ...tokens) {
18
- return tokens.map(container.get);
19
- }
20
- /**
21
- * Returns an array of resolved values by the specified token.
22
- * If a token is not found, then `ResolverError` is thrown.
23
- */
24
- export function resolveValues(container, ...tokens) {
25
- return tokens.map(container.resolve);
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;
26
47
  }
27
48
  /**
28
- * Decorates a factory by passing resolved tokens as factory arguments.
29
- * @param factory - A factory.
30
- * @param tokens - Tokens which correspond to factory arguments.
31
- * @return Decorated factory which takes a dependency container as a single argument.
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
+ * ```
32
64
  */
33
- export function injectable(factory, ...tokens) {
34
- return (container) => {
35
- const values = resolveValues(container, ...tokens);
36
- return factory.apply(this, values);
37
- };
65
+ export function tryResolveValues(container, ...tokens) {
66
+ return tokens.map((item) => tryResolveValue(container, item));
38
67
  }
39
68
  /**
40
- * Returns an object with resolved properties which are specified by token properties.
41
- * If a token is not found, then `undefined` value is used.
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.
42
75
  *
43
76
  * @example
44
77
  * ```ts
45
- * const props = getProps(container, {a: tokenA, b: tokenB});
78
+ * const value = resolveValue(container, tokenA);
79
+ * console.log(value); // 1
80
+ *
81
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
46
82
  * console.log(props); // {a: 1, b: 2}
47
83
  * ```
48
84
  */
49
- export function getProps(container, tokens) {
50
- const obj = { ...tokens };
51
- Object.keys(obj).forEach((key) => (obj[key] = container.get(obj[key])));
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])));
52
91
  return obj;
53
92
  }
54
93
  /**
55
- * Returns an object with resolved properties which are specified by token properties.
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
+
56
99
  * If a token is not found, then `ResolverError` is thrown.
57
100
  *
58
101
  * @example
59
102
  * ```ts
60
- * const props = resolveProps(container, {a: tokenA, b: tokenB});
61
- * console.log(props); // {a: 1, b: 2}
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}]
62
108
  * ```
63
109
  */
64
- export function resolveProps(container, tokens) {
65
- const obj = { ...tokens };
66
- Object.keys(obj).forEach((key) => (obj[key] = container.resolve(obj[key])));
67
- return obj;
110
+ export function resolveValues(container, ...tokens) {
111
+ return tokens.map((item) => resolveValue(container, item));
68
112
  }
69
113
  /**
70
- * Decorates a factory by passing a resolved object with tokens as the first argument.
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
+ *
71
119
  * @param factory - A factory.
72
- * @param tokens - Object with tokens.
73
- * @return Decorated factory which takes a dependency container as a single argument.
120
+ * @param tokens - Tokens which correspond to factory arguments.
74
121
  *
75
- * @example
76
- * ```ts
77
- * const factory = ({a, b}: {a: number, b: number}) => a + b;
78
- * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
79
- * const result = decoratedFactory(container);
80
- * ```
122
+ * @return Decorated factory which takes a dependency container as a single argument.
81
123
  */
82
- export function injectableProps(factory, tokens) {
124
+ export function injectable(factory, ...tokens) {
83
125
  return (container) => {
84
- const values = resolveProps(container, tokens);
85
- return factory(values);
126
+ const values = resolveValues(container, ...tokens);
127
+ return factory.apply(this, values);
86
128
  };
87
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
+ }
88
145
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAOA;;;;;GAKG;AACH,MAAM,UAAU,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;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS,CAKvB,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAW,CAAC;AAC7C,CAAC;AAED;;;GAGG;AACH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AAU,CACf,CAAC,GAAG,MAAM,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,GAAG,MAAM,CAAC,EACzC,GAAG,MAAM,CACV,CAAC;AACJ,CAAC"}
@@ -102,12 +102,16 @@ declare type Container = {
102
102
  */
103
103
  declare function createContainer(parentContainer?: Container): Container;
104
104
 
105
- declare type ValuesProps = {
105
+ declare type ValuesProps$1 = {
106
106
  [key: string]: unknown;
107
107
  };
108
- declare type TokenProps<Props extends ValuesProps> = {
108
+ declare type TokenProps$1<Props extends ValuesProps$1> = {
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.
@@ -116,32 +120,144 @@ declare type TokenProps<Props extends ValuesProps> = {
116
120
  */
117
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;
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$1<infer Props> ? Partial<Props> : never>(container: Container, token: Tokens): Values;
142
+ /**
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
+ * ```
158
+ */
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$1<infer Props> ? Partial<Props> : never;
124
163
  }>(container: Container, ...tokens: Tokens): Values;
125
164
  /**
126
- * Returns an array of resolved values by the specified token.
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.
171
+ *
172
+ * @example
173
+ * ```ts
174
+ * const value = resolveValue(container, tokenA);
175
+ * console.log(value); // 1
176
+ *
177
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
178
+ * console.log(props); // {a: 1, b: 2}
179
+ * ```
180
+ */
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$1<infer Props> ? Props : never>(container: Container, token: Tokens): Values;
184
+ /**
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
+
127
190
  * If a token is not found, then `ResolverError` is thrown.
191
+ *
192
+ * @example
193
+ * ```ts
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}]
199
+ * ```
128
200
  */
129
- declare function resolveValues<Tokens extends Token<unknown>[], Values extends {
130
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
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$1<infer Props> ? Props : never;
131
205
  }>(container: Container, ...tokens: Tokens): Values;
132
206
  /**
133
- * Decorates a factory by passing resolved tokens as factory arguments.
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
+ *
134
212
  * @param factory - A factory.
135
213
  * @param tokens - Tokens which correspond to factory arguments.
214
+ *
136
215
  * @return Decorated factory which takes a dependency container as a single argument.
137
216
  */
138
- declare function injectable<Tokens extends Token<unknown>[], Values extends {
139
- [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
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$1<infer Props> ? Props : never;
140
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.
224
+ *
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.
233
+ */
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$1<infer Props> ? Props : never;
238
+ }, Result>(this: unknown, constructor: new (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
239
+
240
+ declare type ValuesProps = {
241
+ [key: string]: unknown;
242
+ };
243
+ declare type TokenProps<Props extends ValuesProps> = {
244
+ [K in keyof Props]: Token<Props[K]>;
245
+ };
246
+ /**
247
+ * Returns an array of resolved values by the specified token.
248
+ * If a token is not found, then `undefined` value is used.
249
+ *
250
+ * @deprecated Use `tryResolveValues()` function.
251
+ */
252
+ declare function getValues<Tokens extends Token<unknown>[], Values extends {
253
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
254
+ }>(container: Container, ...tokens: Tokens): Values;
141
255
  /**
142
256
  * Returns an object with resolved properties which are specified by token properties.
143
257
  * If a token is not found, then `undefined` value is used.
144
258
  *
259
+ * @deprecated Use `tryResolveValue()` function.
260
+ *
145
261
  * @example
146
262
  * ```ts
147
263
  * const props = getProps(container, {a: tokenA, b: tokenB});
@@ -153,6 +269,8 @@ declare function getProps<Props extends ValuesProps>(container: Container, token
153
269
  * Returns an object with resolved properties which are specified by token properties.
154
270
  * If a token is not found, then `ResolverError` is thrown.
155
271
  *
272
+ * @deprecated Use `resolveValue()` function.
273
+ *
156
274
  * @example
157
275
  * ```ts
158
276
  * const props = resolveProps(container, {a: tokenA, b: tokenB});
@@ -166,6 +284,8 @@ declare function resolveProps<Props extends ValuesProps>(container: Container, t
166
284
  * @param tokens - Object with tokens.
167
285
  * @return Decorated factory which takes a dependency container as a single argument.
168
286
  *
287
+ * @deprecated Use `injectable()` function.
288
+ *
169
289
  * @example
170
290
  * ```ts
171
291
  * const factory = ({a, b}: {a: number, b: number}) => a + b;
@@ -298,4 +418,4 @@ declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token
298
418
  */
299
419
  declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
300
420
 
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 };
421
+ export { BindModuleOptions, Container, FactoryOptions, FactoryScope, Module, ModuleBindingEntry, ModuleDeclaration, OptionalToken, RequiredToken, ResolverError, Token, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, getProps, getValues, injectable, injectableClass, injectableProps, isToken, optional, resolveProps, resolveValue, resolveValues, token, tryResolveValue, tryResolveValues };
package/dist/umd/index.js CHANGED
@@ -1,2 +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 d(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map(o.resolve)}function v(o){for(var n=this,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return function(r){var i=d.apply(void 0,e([r],t,!1));return o.apply(n,i)}}function y(o,n){var e=t({},n);return Object.keys(e).forEach((function(n){return e[n]=o.resolve(e[n])})),e}function b(o,n,t){var e,r=n.token,i=n.imports,l=n.factory,u=n.beforeBinding,a=n.afterBinding,s=null!==(e=n.exports)&&void 0!==e?e:n.exportedProps,c=null==t?void 0:t.scope;u&&u(o),i&&m(o,i);var f=new Set;s&&Object.keys(s).forEach((function(n){var t=s[n];t&&(f.add(t),o.bindFactory(t,v((function(o){return o[n]}),r),{scope:c}))}));o.bindFactory(r,l,{scope:c,onRemoved:function(n){n.destroy&&n.destroy(),f.forEach((function(n){return o.remove(n)})),f.clear()}}),a&&a(o)}function m(o,n){n.forEach((function(n){"module"in n?b(o,n.module,n.options):b(o,n)}))}function h(o){var n;return t(t({},o),{token:null!==(n=o.token)&&void 0!==n?n:r()})}o.ResolverError=i,o.bindModule=b,o.bindModules=m,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&&d(o.symbol,r),n.delete(o.symbol),t.delete(o.symbol)}},removeAll:function(){t.forEach((function(o,n){o.options&&d(n,o.options)})),n.clear(),t.clear(),v()},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,d=t.get(r.symbol);if(d&&d.factory!==c)switch(f=d.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 v=d.factory(e);return e.bindValue(r,v),v;case"scoped":var y=d.factory(i);return i.bindValue(r,y),i!==e&&i.bindFactory(r,c,d.options),y;case"transient":return d.factory(i)}if(u)return l;var b=null==o?void 0:o.get(a);return b?b(r,i):s}function d(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 v(){n.set(l.symbol,e),n.set(a.symbol,r),n.set(f.symbol,t),o&&n.set(u.symbol,o)}return v(),e},o.declareModule=h,o.declareModuleBindings=function(o){return h({factory:function(){return{}},imports:o})},o.getProps=function(o,n){var e=t({},n);return Object.keys(e).forEach((function(n){return e[n]=o.get(e[n])})),e},o.getValues=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map(o.get)},o.injectable=v,o.injectableProps=function(o,n){return function(t){var e=y(t,n);return o(e)}},o.optional=function(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}},o.resolveProps=y,o.resolveValues=d,o.token=r,Object.defineProperty(o,"__esModule",{value:!0})}));
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 v(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 d(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 b(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 m(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return n.map((function(n){return b(o,n)}))}function h(o){for(var n=this,t=[],r=1;r<arguments.length;r++)t[r-1]=arguments[r];return function(r){var i=m.apply(void 0,e([r],t,!1));return o.apply(n,i)}}function g(o,n,t){var e,r=n.token,i=n.imports,l=n.factory,u=n.beforeBinding,a=n.afterBinding,s=null!==(e=n.exports)&&void 0!==e?e:n.exportedProps,c=null==t?void 0:t.scope;u&&u(o),i&&O(o,i);var f=new Set;s&&Object.keys(s).forEach((function(n){var t=s[n];t&&(f.add(t),o.bindFactory(t,h((function(o){return o[n]}),r),{scope:c}))}));o.bindFactory(r,l,{scope:c,onRemoved:function(n){n.destroy&&n.destroy(),f.forEach((function(n){return o.remove(n)})),f.clear()}}),a&&a(o)}function O(o,n){n.forEach((function(n){"module"in n?g(o,n.module,n.options):g(o,n)}))}function j(o){var n;return t(t({},o),{token:null!==(n=o.token)&&void 0!==n?n:r()})}o.ResolverError=i,o.bindModule=g,o.bindModules=O,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(),v()},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 v=y.factory(e);return e.bindValue(r,v),v;case"scoped":var d=y.factory(i);return i.bindValue(r,d),i!==e&&i.bindFactory(r,c,y.options),d;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 v(){n.set(l.symbol,e),n.set(a.symbol,r),n.set(f.symbol,t),o&&n.set(u.symbol,o)}return v(),e},o.declareModule=j,o.declareModuleBindings=function(o){return j({factory:function(){return{}},imports:o})},o.getProps=function(o,n){return v(o,n)},o.getValues=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return d.apply(void 0,e([o],n,!1))},o.injectable=h,o.injectableClass=function(o){for(var n=[],t=1;t<arguments.length;t++)n[t-1]=arguments[t];return h.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.injectableProps=function(o,n){return h(o,n)},o.isToken=y,o.optional=function(o,n){return{symbol:o.symbol,isOptional:!0,optionalValue:n}},o.resolveProps=function(o,n){return b(o,n)},o.resolveValue=b,o.resolveValues=m,o.token=r,o.tryResolveValue=v,o.tryResolveValues=d,Object.defineProperty(o,"__esModule",{value:!0})}));
2
2
  //# sourceMappingURL=index.js.map