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.
package/src/index.ts CHANGED
@@ -9,14 +9,22 @@ export type {
9
9
  } from './ditox';
10
10
 
11
11
  export {
12
+ isToken,
12
13
  bindMultiValue,
13
- getValues,
14
+ tryResolveValue,
15
+ tryResolveValues,
16
+ resolveValue,
14
17
  resolveValues,
15
18
  injectable,
19
+ injectableClass,
20
+ } from './utils';
21
+
22
+ export {
23
+ getValues,
16
24
  getProps,
17
25
  resolveProps,
18
26
  injectableProps,
19
- } from './utils';
27
+ } from './utils-deprecated';
20
28
 
21
29
  export {
22
30
  bindModule,
@@ -0,0 +1,286 @@
1
+ import {createContainer, optional, ResolverError, token} from './ditox';
2
+ import {bindMultiValue, injectable, resolveValues} from './utils';
3
+ import {
4
+ getProps,
5
+ getValues,
6
+ injectableProps,
7
+ resolveProps,
8
+ } from './utils-deprecated';
9
+
10
+ const NUMBER = token<number>('number');
11
+ const STRING = token<string>('string');
12
+
13
+ describe('bindMultiValue', () => {
14
+ const NUMBERS = token<Array<number>>('numbers');
15
+
16
+ it('should append a value to an array declared by a token', () => {
17
+ const container = createContainer();
18
+
19
+ bindMultiValue(container, NUMBERS, 1);
20
+ bindMultiValue(container, NUMBERS, 2);
21
+
22
+ const values: Array<number> = container.resolve(NUMBERS);
23
+ expect(values).toEqual([1, 2]);
24
+ });
25
+
26
+ it('should append new values to an array declared by a token', () => {
27
+ const container = createContainer();
28
+
29
+ bindMultiValue(container, NUMBERS, 1);
30
+ bindMultiValue(container, NUMBERS, 2);
31
+ expect(container.resolve(NUMBERS)).toEqual([1, 2]);
32
+
33
+ bindMultiValue(container, NUMBERS, 3);
34
+ bindMultiValue(container, NUMBERS, 4);
35
+ expect(container.resolve(NUMBERS)).toEqual([1, 2, 3, 4]);
36
+ });
37
+
38
+ it('should add new values to a copy of array from the parent container', () => {
39
+ const parent = createContainer();
40
+ parent.bindValue(NUMBERS, [1, 2]);
41
+ const container = createContainer(parent);
42
+
43
+ bindMultiValue(container, NUMBERS, 3);
44
+ bindMultiValue(container, NUMBERS, 4);
45
+ expect(parent.resolve(NUMBERS)).toEqual([1, 2]);
46
+ expect(container.resolve(NUMBERS)).toEqual([1, 2, 3, 4]);
47
+
48
+ container.remove(NUMBERS);
49
+ expect(parent.resolve(NUMBERS)).toEqual([1, 2]);
50
+ expect(container.resolve(NUMBERS)).toEqual([1, 2]);
51
+ });
52
+ });
53
+
54
+ describe('getValues', () => {
55
+ it('should return values for the tokens', () => {
56
+ const container = createContainer();
57
+ container.bindValue(NUMBER, 1);
58
+ container.bindValue(STRING, 'abc');
59
+
60
+ const values: [number, string] = getValues(container, NUMBER, STRING);
61
+ expect(values).toEqual([1, 'abc']);
62
+ });
63
+
64
+ it('should return "undefined" item in case a value is not provided', () => {
65
+ const container = createContainer();
66
+ container.bindValue(NUMBER, 1);
67
+
68
+ const values = getValues(container, NUMBER, STRING);
69
+ expect(values).toEqual([1, undefined]);
70
+ });
71
+
72
+ it('should return values of optional tokens in case they are not provided', () => {
73
+ const container = createContainer();
74
+ container.bindValue(NUMBER, 1);
75
+
76
+ const OPTIONAL_STRING = optional(STRING, 'value');
77
+
78
+ const values = getValues(container, NUMBER, STRING, OPTIONAL_STRING);
79
+ expect(values).toEqual([1, undefined, 'value']);
80
+ });
81
+
82
+ it('should works with a big arity', () => {
83
+ const container = createContainer();
84
+ container.bindValue(NUMBER, 1);
85
+
86
+ const values = getValues(
87
+ container,
88
+ NUMBER,
89
+ NUMBER,
90
+ NUMBER,
91
+ NUMBER,
92
+ NUMBER,
93
+ NUMBER,
94
+ NUMBER,
95
+ NUMBER,
96
+ NUMBER,
97
+ NUMBER,
98
+ );
99
+ expect(values).toEqual([1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
100
+ });
101
+ });
102
+
103
+ describe('resolveValues', () => {
104
+ it('should resolve tokens from the container', () => {
105
+ const container = createContainer();
106
+ container.bindValue(NUMBER, 1);
107
+ container.bindValue(STRING, 'abc');
108
+
109
+ const values: [number, string] = resolveValues(container, NUMBER, STRING);
110
+ expect(values).toEqual([1, 'abc']);
111
+ });
112
+
113
+ it('should throw an error in case a value is not provided', () => {
114
+ const container = createContainer();
115
+ container.bindValue(NUMBER, 1);
116
+
117
+ expect(() => {
118
+ resolveValues(container, NUMBER, STRING);
119
+ }).toThrowError(
120
+ new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
121
+ );
122
+ });
123
+
124
+ it('should resolve values of optional tokens in case they are not provided', () => {
125
+ const container = createContainer();
126
+ container.bindValue(NUMBER, 1);
127
+
128
+ const values = getValues(container, NUMBER, optional(STRING, 'value'));
129
+ expect(values).toEqual([1, 'value']);
130
+ });
131
+ });
132
+
133
+ describe('injectable()', () => {
134
+ it('should inject values from Container as arguments of the factory', () => {
135
+ const container = createContainer();
136
+ container.bindValue(NUMBER, 1);
137
+ container.bindValue(STRING, '2');
138
+
139
+ const decoratedFactory = injectable(
140
+ (a: number, b: string) => a + b,
141
+ NUMBER,
142
+ STRING,
143
+ );
144
+
145
+ expect(decoratedFactory(container)).toBe('12');
146
+ });
147
+
148
+ it('should throw ResolverError error in case a value is not provided', () => {
149
+ const container = createContainer();
150
+ container.bindValue(NUMBER, 1);
151
+
152
+ const decoratedFactory = injectable(
153
+ (a: number, b: string) => a + b,
154
+ NUMBER,
155
+ STRING,
156
+ );
157
+
158
+ expect(() => decoratedFactory(container)).toThrowError(
159
+ new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
160
+ );
161
+ });
162
+
163
+ it('should inject values of optional tokens in case values are not provided', () => {
164
+ const container = createContainer();
165
+ container.bindValue(NUMBER, 1);
166
+
167
+ const decoratedFactory = injectable(
168
+ (a, b) => a + b,
169
+ NUMBER,
170
+ optional(STRING, 'value'),
171
+ );
172
+
173
+ expect(decoratedFactory(container)).toBe('1value');
174
+ });
175
+ });
176
+
177
+ describe('getProps', () => {
178
+ it('should return an object with values by the tokens', () => {
179
+ const container = createContainer();
180
+ container.bindValue(NUMBER, 1);
181
+ container.bindValue(STRING, 'abc');
182
+
183
+ const props: {a?: number; b?: string} = getProps(container, {
184
+ a: NUMBER,
185
+ b: STRING,
186
+ });
187
+ expect(props).toEqual({a: 1, b: 'abc'});
188
+ });
189
+
190
+ it('should return "undefined" item in case a value is not provided', () => {
191
+ const container = createContainer();
192
+ container.bindValue(NUMBER, 1);
193
+
194
+ const props = getProps(container, {a: NUMBER, b: STRING});
195
+ expect(props).toEqual({a: 1, b: undefined});
196
+ });
197
+
198
+ it('should return values of optional tokens in case they are not provided', () => {
199
+ const container = createContainer();
200
+ container.bindValue(NUMBER, 1);
201
+
202
+ const OPTIONAL_STRING = optional(STRING, 'value');
203
+
204
+ const props = getProps(container, {
205
+ a: NUMBER,
206
+ b: STRING,
207
+ c: OPTIONAL_STRING,
208
+ });
209
+ expect(props).toEqual({a: 1, b: undefined, c: 'value'});
210
+ });
211
+ });
212
+
213
+ describe('resolveProps', () => {
214
+ it('should an object with resolved values from the container for the specified token props', () => {
215
+ const container = createContainer();
216
+ container.bindValue(NUMBER, 1);
217
+ container.bindValue(STRING, 'abc');
218
+
219
+ const props: {a: number; b: string} = resolveProps(container, {
220
+ a: NUMBER,
221
+ b: STRING,
222
+ });
223
+ expect(props).toEqual({a: 1, b: 'abc'});
224
+ });
225
+
226
+ it('should throw an error in case a value is not provided', () => {
227
+ const container = createContainer();
228
+ container.bindValue(NUMBER, 1);
229
+
230
+ expect(() => {
231
+ resolveProps(container, {a: NUMBER, b: STRING});
232
+ }).toThrowError(
233
+ new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
234
+ );
235
+ });
236
+
237
+ it('should resolve values of optional tokens in case they are not provided', () => {
238
+ const container = createContainer();
239
+ container.bindValue(NUMBER, 1);
240
+
241
+ const props = resolveProps(container, {
242
+ a: NUMBER,
243
+ b: optional(STRING, 'value'),
244
+ });
245
+ expect(props).toEqual({a: 1, b: 'value'});
246
+ });
247
+ });
248
+
249
+ describe('injectableProps()', () => {
250
+ function join({a, b}: {a: number; b: string}): string {
251
+ return `${a}/${b}`;
252
+ }
253
+
254
+ it('should inject values from Container as an object for the first argument of the factory', () => {
255
+ const container = createContainer();
256
+ container.bindValue(NUMBER, 1);
257
+ container.bindValue(STRING, '2');
258
+
259
+ const decoratedFactory = injectableProps(join, {a: NUMBER, b: STRING});
260
+
261
+ expect(decoratedFactory(container)).toBe('1/2');
262
+ });
263
+
264
+ it('should throw ResolverError error in case a value is not provided', () => {
265
+ const container = createContainer();
266
+ container.bindValue(NUMBER, 1);
267
+
268
+ const decoratedFactory = injectableProps(join, {a: NUMBER, b: STRING});
269
+
270
+ expect(() => decoratedFactory(container)).toThrowError(
271
+ new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
272
+ );
273
+ });
274
+
275
+ it('should inject values of optional tokens in case values are not provided', () => {
276
+ const container = createContainer();
277
+ container.bindValue(NUMBER, 1);
278
+
279
+ const decoratedFactory = injectableProps(join, {
280
+ a: NUMBER,
281
+ b: optional(STRING, 'value'),
282
+ });
283
+
284
+ expect(decoratedFactory(container)).toBe('1/value');
285
+ });
286
+ });
@@ -0,0 +1,87 @@
1
+ import type {Container, Token} from './ditox';
2
+ import {
3
+ injectable,
4
+ resolveValue,
5
+ tryResolveValue,
6
+ tryResolveValues,
7
+ } from './utils';
8
+
9
+ type ValuesProps = {[key: string]: unknown};
10
+ type TokenProps<Props extends ValuesProps> = {
11
+ [K in keyof Props]: Token<Props[K]>;
12
+ };
13
+
14
+ /**
15
+ * Returns an array of resolved values by the specified token.
16
+ * If a token is not found, then `undefined` value is used.
17
+ *
18
+ * @deprecated Use `tryResolveValues()` function.
19
+ */
20
+ export function getValues<
21
+ Tokens extends Token<unknown>[],
22
+ Values extends {
23
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
24
+ },
25
+ >(container: Container, ...tokens: Tokens): Values {
26
+ return tryResolveValues(container, ...tokens);
27
+ }
28
+
29
+ /**
30
+ * Returns an object with resolved properties which are specified by token properties.
31
+ * If a token is not found, then `undefined` value is used.
32
+ *
33
+ * @deprecated Use `tryResolveValue()` function.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * const props = getProps(container, {a: tokenA, b: tokenB});
38
+ * console.log(props); // {a: 1, b: 2}
39
+ * ```
40
+ */
41
+ export function getProps<Props extends ValuesProps>(
42
+ container: Container,
43
+ tokens: TokenProps<Props>,
44
+ ): Partial<Props> {
45
+ return tryResolveValue(container, tokens) as Partial<Props>;
46
+ }
47
+
48
+ /**
49
+ * Returns an object with resolved properties which are specified by token properties.
50
+ * If a token is not found, then `ResolverError` is thrown.
51
+ *
52
+ * @deprecated Use `resolveValue()` function.
53
+ *
54
+ * @example
55
+ * ```ts
56
+ * const props = resolveProps(container, {a: tokenA, b: tokenB});
57
+ * console.log(props); // {a: 1, b: 2}
58
+ * ```
59
+ */
60
+ export function resolveProps<Props extends ValuesProps>(
61
+ container: Container,
62
+ tokens: TokenProps<Props>,
63
+ ): Props {
64
+ return resolveValue(container, tokens) as Props;
65
+ }
66
+
67
+ /**
68
+ * Decorates a factory by passing a resolved object with tokens as the first argument.
69
+ * @param factory - A factory.
70
+ * @param tokens - Object with tokens.
71
+ * @return Decorated factory which takes a dependency container as a single argument.
72
+ *
73
+ * @deprecated Use `injectable()` function.
74
+ *
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
+ * ```
81
+ */
82
+ export function injectableProps<Props extends ValuesProps, Result>(
83
+ factory: (props: Props) => Result,
84
+ tokens: TokenProps<Props>,
85
+ ): (container: Container) => Result {
86
+ return injectable(factory as any, tokens);
87
+ }