ditox 1.4.0 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/dist/{index.d.ts → browser/index.d.ts} +133 -13
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/cjs/ditox.d.ts +125 -0
- package/dist/cjs/ditox.js +210 -0
- package/dist/cjs/ditox.js.map +1 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/modules.d.ts +124 -0
- package/dist/cjs/modules.js +110 -0
- package/dist/cjs/modules.js.map +1 -0
- package/dist/cjs/utils-deprecated.d.ts +59 -0
- package/dist/cjs/utils-deprecated.js +66 -0
- package/dist/cjs/utils-deprecated.js.map +1 -0
- package/dist/cjs/utils.d.ts +136 -0
- package/dist/cjs/utils.js +156 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/ditox.d.ts +125 -0
- package/{lib/index.js → dist/esm/ditox.js} +10 -201
- package/dist/esm/ditox.js.map +1 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/modules.d.ts +124 -0
- package/dist/esm/modules.js +103 -0
- package/dist/esm/modules.js.map +1 -0
- package/dist/esm/utils-deprecated.d.ts +59 -0
- package/dist/esm/utils-deprecated.js +59 -0
- package/dist/esm/utils-deprecated.js.map +1 -0
- package/dist/esm/utils.d.ts +136 -0
- package/dist/esm/utils.js +145 -0
- package/dist/esm/utils.js.map +1 -0
- package/{lib → dist/umd}/index.d.ts +133 -13
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +364 -98
- package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
- package/package.json +16 -17
- package/src/ditox.test.ts +10 -1
- package/src/index.ts +18 -11
- package/src/modules.test.ts +32 -0
- package/src/modules.ts +4 -4
- package/src/utils-deprecated.test.ts +286 -0
- package/src/utils-deprecated.ts +87 -0
- package/src/utils.test.ts +173 -58
- package/src/utils.ts +164 -66
- package/CHANGELOG.md +0 -110
- package/dist/index.browser.js +0 -2
- package/dist/index.browser.js.flow +0 -4
- package/dist/index.browser.js.map +0 -1
- package/dist/index.js +0 -411
- package/dist/index.js.flow +0 -4
- package/dist/index.js.map +0 -1
- package/dist/index.module.d.ts +0 -301
- package/dist/index.module.js +0 -393
- package/dist/index.module.js.flow +0 -4
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.d.ts +0 -301
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.flow +0 -4
- package/dist/index.umd.js.map +0 -1
- package/lib/index.js.flow +0 -4
- package/lib/index.js.map +0 -1
- package/mod.ts +0 -2
- package/src/ditox.js.flow +0 -131
- package/src/index.js.flow +0 -34
- package/src/modules.js.flow +0 -156
- package/src/utils.js.flow +0 -113
|
@@ -8,25 +8,25 @@ ResolverError is thrown by the resolver when a token is not found in a container
|
|
|
8
8
|
|
|
9
9
|
- `Error`
|
|
10
10
|
|
|
11
|
-
↳
|
|
11
|
+
↳ **`ResolverError`**
|
|
12
12
|
|
|
13
13
|
## Table of contents
|
|
14
14
|
|
|
15
15
|
### Constructors
|
|
16
16
|
|
|
17
|
-
- [constructor](
|
|
17
|
+
- [constructor](ResolverError.md#constructor)
|
|
18
18
|
|
|
19
19
|
### Properties
|
|
20
20
|
|
|
21
|
-
- [message](
|
|
22
|
-
- [name](
|
|
23
|
-
- [stack](
|
|
24
|
-
- [prepareStackTrace](
|
|
25
|
-
- [stackTraceLimit](
|
|
21
|
+
- [message](ResolverError.md#message)
|
|
22
|
+
- [name](ResolverError.md#name)
|
|
23
|
+
- [stack](ResolverError.md#stack)
|
|
24
|
+
- [prepareStackTrace](ResolverError.md#preparestacktrace)
|
|
25
|
+
- [stackTraceLimit](ResolverError.md#stacktracelimit)
|
|
26
26
|
|
|
27
27
|
### Methods
|
|
28
28
|
|
|
29
|
-
- [captureStackTrace](
|
|
29
|
+
- [captureStackTrace](ResolverError.md#capturestacktrace)
|
|
30
30
|
|
|
31
31
|
## Constructors
|
|
32
32
|
|
|
@@ -46,7 +46,7 @@ Error.constructor
|
|
|
46
46
|
|
|
47
47
|
#### Defined in
|
|
48
48
|
|
|
49
|
-
[packages/ditox/src/ditox.ts:
|
|
49
|
+
[packages/ditox/src/ditox.ts:61](https://github.com/mnasyrov/ditox/blob/aea7a8f/packages/ditox/src/ditox.ts#L61)
|
|
50
50
|
|
|
51
51
|
## Properties
|
|
52
52
|
|
|
@@ -60,7 +60,7 @@ Error.message
|
|
|
60
60
|
|
|
61
61
|
#### Defined in
|
|
62
62
|
|
|
63
|
-
node_modules/typescript/lib/lib.es5.d.ts:
|
|
63
|
+
node_modules/typescript/lib/lib.es5.d.ts:1023
|
|
64
64
|
|
|
65
65
|
___
|
|
66
66
|
|
|
@@ -74,7 +74,7 @@ Error.name
|
|
|
74
74
|
|
|
75
75
|
#### Defined in
|
|
76
76
|
|
|
77
|
-
node_modules/typescript/lib/lib.es5.d.ts:
|
|
77
|
+
node_modules/typescript/lib/lib.es5.d.ts:1022
|
|
78
78
|
|
|
79
79
|
___
|
|
80
80
|
|
|
@@ -88,7 +88,7 @@ Error.stack
|
|
|
88
88
|
|
|
89
89
|
#### Defined in
|
|
90
90
|
|
|
91
|
-
node_modules/typescript/lib/lib.es5.d.ts:
|
|
91
|
+
node_modules/typescript/lib/lib.es5.d.ts:1024
|
|
92
92
|
|
|
93
93
|
___
|
|
94
94
|
|
|
@@ -102,6 +102,8 @@ ___
|
|
|
102
102
|
|
|
103
103
|
Optional override for formatting stack traces
|
|
104
104
|
|
|
105
|
+
**`see`** https://v8.dev/docs/stack-trace-api#customizing-stack-traces
|
|
106
|
+
|
|
105
107
|
##### Parameters
|
|
106
108
|
|
|
107
109
|
| Name | Type |
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ditox",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Powerful dependency injection container for building modular apps.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Mikhail Nasyrov <mikhail@mnasyrov.com> (https://github.com/mnasyrov)",
|
|
7
7
|
"homepage": "https://github.com/mnasyrov/ditox",
|
|
@@ -22,31 +22,30 @@
|
|
|
22
22
|
"engines": {
|
|
23
23
|
"node": ">=12"
|
|
24
24
|
},
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"types": "dist/index.d.ts",
|
|
25
|
+
"main": "dist/cjs/index.js",
|
|
26
|
+
"module": "dist/esm/index.js",
|
|
27
|
+
"jsnext:main": "dist/esm/index.js",
|
|
28
|
+
"unpkg": "dist/umd/index.js",
|
|
29
|
+
"umd:main": "dist/umd/index.js",
|
|
30
|
+
"browser": "dist/browser/index.js",
|
|
31
|
+
"react-native": "dist/browser/index.js",
|
|
32
|
+
"types": "dist/esm/index.d.ts",
|
|
34
33
|
"source": "src/index.ts",
|
|
35
34
|
"sideEffects": false,
|
|
36
35
|
"files": [
|
|
37
36
|
"dist",
|
|
38
37
|
"docs",
|
|
39
|
-
"lib",
|
|
40
38
|
"src",
|
|
41
39
|
"LICENSE",
|
|
42
|
-
"mod.ts",
|
|
43
40
|
"README.md"
|
|
44
41
|
],
|
|
45
42
|
"scripts": {
|
|
46
43
|
"clean": "shx rm -rf dist lib",
|
|
47
|
-
"test-deno": "deno test test-deno",
|
|
48
|
-
"build": "rollup
|
|
49
|
-
"
|
|
44
|
+
"test-deno": "npm run build:rollup && deno test test-deno",
|
|
45
|
+
"build": "npm run build:cjs && npm run build:esm && npm run build:rollup",
|
|
46
|
+
"build:cjs": "tsc -p tsconfig.build.json --outDir dist/cjs --module commonjs",
|
|
47
|
+
"build:esm": "tsc -p tsconfig.build.json --outDir dist/esm --module es2015",
|
|
48
|
+
"build:rollup": "rollup -c"
|
|
50
49
|
},
|
|
51
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "8e8e4d79b6aaf3fa8662e51ecce0511f53e10ea8"
|
|
52
51
|
}
|
package/src/ditox.test.ts
CHANGED
|
@@ -508,7 +508,16 @@ describe('Container', () => {
|
|
|
508
508
|
|
|
509
509
|
it('should throw ResolverError in case a value is not provided', () => {
|
|
510
510
|
const container = createContainer();
|
|
511
|
-
expect(() => container.resolve(
|
|
511
|
+
expect(() => container.resolve(token('test'))).toThrowError(
|
|
512
|
+
new ResolverError(`Token "test" is not provided`),
|
|
513
|
+
);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it('should throw ResolverError with an empty name in case a token is not resolved', () => {
|
|
517
|
+
const container = createContainer();
|
|
518
|
+
expect(() => container.resolve(token())).toThrowError(
|
|
519
|
+
new ResolverError(`Token "" is not provided`),
|
|
520
|
+
);
|
|
512
521
|
});
|
|
513
522
|
|
|
514
523
|
it('should resolve a value from the parent container', () => {
|
package/src/index.ts
CHANGED
|
@@ -1,33 +1,40 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {token, optional, ResolverError, createContainer} from './ditox';
|
|
2
|
+
export type {
|
|
2
3
|
RequiredToken,
|
|
3
4
|
OptionalToken,
|
|
4
5
|
Token,
|
|
5
|
-
token,
|
|
6
|
-
optional,
|
|
7
|
-
ResolverError,
|
|
8
6
|
FactoryScope,
|
|
9
7
|
FactoryOptions,
|
|
10
8
|
Container,
|
|
11
|
-
createContainer,
|
|
12
9
|
} from './ditox';
|
|
13
10
|
|
|
14
11
|
export {
|
|
12
|
+
isToken,
|
|
15
13
|
bindMultiValue,
|
|
16
|
-
|
|
14
|
+
tryResolveValue,
|
|
15
|
+
tryResolveValues,
|
|
16
|
+
resolveValue,
|
|
17
17
|
resolveValues,
|
|
18
18
|
injectable,
|
|
19
|
+
injectableClass,
|
|
20
|
+
} from './utils';
|
|
21
|
+
|
|
22
|
+
export {
|
|
23
|
+
getValues,
|
|
19
24
|
getProps,
|
|
20
25
|
resolveProps,
|
|
21
26
|
injectableProps,
|
|
22
|
-
} from './utils';
|
|
27
|
+
} from './utils-deprecated';
|
|
23
28
|
|
|
24
29
|
export {
|
|
25
|
-
Module,
|
|
26
|
-
ModuleDeclaration,
|
|
27
|
-
BindModuleOptions,
|
|
28
|
-
ModuleBindingEntry,
|
|
29
30
|
bindModule,
|
|
30
31
|
bindModules,
|
|
31
32
|
declareModule,
|
|
32
33
|
declareModuleBindings,
|
|
33
34
|
} from './modules';
|
|
35
|
+
export type {
|
|
36
|
+
Module,
|
|
37
|
+
ModuleDeclaration,
|
|
38
|
+
BindModuleOptions,
|
|
39
|
+
ModuleBindingEntry,
|
|
40
|
+
} from './modules';
|
package/src/modules.test.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
Module,
|
|
8
8
|
ModuleDeclaration,
|
|
9
9
|
} from './modules';
|
|
10
|
+
import {injectable} from './utils';
|
|
10
11
|
|
|
11
12
|
describe('bindModule()', () => {
|
|
12
13
|
type TestQueries = {getValue: () => number};
|
|
@@ -196,6 +197,35 @@ describe('bindModule()', () => {
|
|
|
196
197
|
expect(container.get(MODULE1_TOKEN)?.value).toBe(1);
|
|
197
198
|
expect(container.get(MODULE2_TOKEN)?.value).toBe(22);
|
|
198
199
|
});
|
|
200
|
+
|
|
201
|
+
it('should call beforeBinding() before importing modules from "imports"', () => {
|
|
202
|
+
const ARG_TOKEN = token<string>('arg');
|
|
203
|
+
const RESULT_TOKEN = token<string>('result');
|
|
204
|
+
|
|
205
|
+
type TestModule = Module<{value: string}>;
|
|
206
|
+
|
|
207
|
+
const MODULE1: ModuleDeclaration<TestModule> = declareModule({
|
|
208
|
+
beforeBinding: (container) =>
|
|
209
|
+
container.bindValue(ARG_TOKEN, container.resolve(ARG_TOKEN) + '2'),
|
|
210
|
+
factory: injectable((arg) => ({value: arg + '3'}), ARG_TOKEN),
|
|
211
|
+
exports: {value: RESULT_TOKEN},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
const container = createContainer();
|
|
215
|
+
bindModule(
|
|
216
|
+
container,
|
|
217
|
+
declareModule({
|
|
218
|
+
factory: () => ({}),
|
|
219
|
+
beforeBinding: (container) => {
|
|
220
|
+
container.bindValue(ARG_TOKEN, '1');
|
|
221
|
+
},
|
|
222
|
+
imports: [MODULE1],
|
|
223
|
+
}),
|
|
224
|
+
);
|
|
225
|
+
|
|
226
|
+
expect(container.resolve(ARG_TOKEN)).toBe('12');
|
|
227
|
+
expect(container.resolve(RESULT_TOKEN)).toBe('123');
|
|
228
|
+
});
|
|
199
229
|
});
|
|
200
230
|
|
|
201
231
|
describe('bindModules()', () => {
|
|
@@ -289,5 +319,7 @@ describe('declareModuleBindings()', () => {
|
|
|
289
319
|
bindModule(container, MODULE_BINDINGS);
|
|
290
320
|
expect(container.get(VALUE1_TOKEN)).toBe(1);
|
|
291
321
|
expect(container.get(VALUE2_TOKEN)).toBe(2);
|
|
322
|
+
|
|
323
|
+
expect(container.get(MODULE_BINDINGS.token)).toEqual({});
|
|
292
324
|
});
|
|
293
325
|
});
|
package/src/modules.ts
CHANGED
|
@@ -113,14 +113,14 @@ export function bindModule<T extends Module<AnyObject>>(
|
|
|
113
113
|
|
|
114
114
|
const scope = options?.scope;
|
|
115
115
|
|
|
116
|
-
if (imports) {
|
|
117
|
-
bindModules(container, imports);
|
|
118
|
-
}
|
|
119
|
-
|
|
120
116
|
if (beforeBinding) {
|
|
121
117
|
beforeBinding(container);
|
|
122
118
|
}
|
|
123
119
|
|
|
120
|
+
if (imports) {
|
|
121
|
+
bindModules(container, imports);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
124
|
const exportedValueTokens = new Set<Token<unknown>>();
|
|
125
125
|
|
|
126
126
|
if (exports) {
|
|
@@ -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
|
+
}
|