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.
- package/README.md +6 -5
- package/dist/{index.d.ts → browser/index.d.ts} +98 -42
- 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 +5 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/modules.d.ts +118 -0
- package/dist/cjs/modules.js +109 -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 +5 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/modules.d.ts +118 -0
- package/dist/esm/modules.js +102 -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 +98 -42
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +250 -164
- package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
- package/package.json +15 -16
- package/src/ditox.test.ts +10 -1
- package/src/index.ts +13 -13
- package/src/modules.test.ts +2 -0
- package/src/modules.ts +1 -8
- package/src/utils.test.ts +173 -58
- package/src/utils.ts +164 -66
- package/CHANGELOG.md +0 -121
- 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/a821171/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,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ditox",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.2.0",
|
|
4
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)",
|
|
@@ -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": "f9d1e2d369a00fb12ad62cbb9aa6039104c4cbe3"
|
|
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,33 @@
|
|
|
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
|
-
|
|
20
|
-
resolveProps,
|
|
21
|
-
injectableProps,
|
|
19
|
+
injectableClass,
|
|
22
20
|
} from './utils';
|
|
23
21
|
|
|
24
22
|
export {
|
|
25
|
-
Module,
|
|
26
|
-
ModuleDeclaration,
|
|
27
|
-
BindModuleOptions,
|
|
28
|
-
ModuleBindingEntry,
|
|
29
23
|
bindModule,
|
|
30
24
|
bindModules,
|
|
31
25
|
declareModule,
|
|
32
26
|
declareModuleBindings,
|
|
33
27
|
} from './modules';
|
|
28
|
+
export type {
|
|
29
|
+
Module,
|
|
30
|
+
ModuleDeclaration,
|
|
31
|
+
BindModuleOptions,
|
|
32
|
+
ModuleBindingEntry,
|
|
33
|
+
} from './modules';
|
package/src/modules.test.ts
CHANGED
|
@@ -319,5 +319,7 @@ describe('declareModuleBindings()', () => {
|
|
|
319
319
|
bindModule(container, MODULE_BINDINGS);
|
|
320
320
|
expect(container.get(VALUE1_TOKEN)).toBe(1);
|
|
321
321
|
expect(container.get(VALUE2_TOKEN)).toBe(2);
|
|
322
|
+
|
|
323
|
+
expect(container.get(MODULE_BINDINGS.token)).toEqual({});
|
|
322
324
|
});
|
|
323
325
|
});
|
package/src/modules.ts
CHANGED
|
@@ -59,13 +59,6 @@ export type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
|
59
59
|
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
60
60
|
};
|
|
61
61
|
|
|
62
|
-
/**
|
|
63
|
-
* Dictionary of module properties which are bound to tokens.
|
|
64
|
-
*
|
|
65
|
-
* @deprecated Use `exports` property.
|
|
66
|
-
*/
|
|
67
|
-
exportedProps?: ModuleDeclaration<T>['exports'];
|
|
68
|
-
|
|
69
62
|
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
70
63
|
beforeBinding?: (container: Container) => void;
|
|
71
64
|
|
|
@@ -109,7 +102,7 @@ export function bindModule<T extends Module<AnyObject>>(
|
|
|
109
102
|
): void {
|
|
110
103
|
const {token, imports, factory, beforeBinding, afterBinding} =
|
|
111
104
|
moduleDeclaration;
|
|
112
|
-
const exports = moduleDeclaration.exports
|
|
105
|
+
const exports = moduleDeclaration.exports;
|
|
113
106
|
|
|
114
107
|
const scope = options?.scope;
|
|
115
108
|
|
package/src/utils.test.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
import {createContainer, optional, ResolverError, token} from './ditox';
|
|
2
2
|
import {
|
|
3
3
|
bindMultiValue,
|
|
4
|
-
getProps,
|
|
5
|
-
getValues,
|
|
6
4
|
injectable,
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
injectableClass,
|
|
6
|
+
resolveValue,
|
|
9
7
|
resolveValues,
|
|
8
|
+
tryResolveValue,
|
|
9
|
+
tryResolveValues,
|
|
10
10
|
} from './utils';
|
|
11
11
|
|
|
12
12
|
const NUMBER = token<number>('number');
|
|
13
13
|
const STRING = token<string>('string');
|
|
14
14
|
|
|
15
|
-
describe('bindMultiValue', () => {
|
|
15
|
+
describe('bindMultiValue()', () => {
|
|
16
16
|
const NUMBERS = token<Array<number>>('numbers');
|
|
17
17
|
|
|
18
18
|
it('should append a value to an array declared by a token', () => {
|
|
@@ -53,13 +53,53 @@ describe('bindMultiValue', () => {
|
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
55
|
|
|
56
|
-
describe('
|
|
56
|
+
describe('tryResolveValue()', () => {
|
|
57
|
+
it('should return an object with values by the tokens', () => {
|
|
58
|
+
const container = createContainer();
|
|
59
|
+
container.bindValue(NUMBER, 1);
|
|
60
|
+
container.bindValue(STRING, 'abc');
|
|
61
|
+
|
|
62
|
+
const props: {a: number; b: string} = tryResolveValue(container, {
|
|
63
|
+
a: NUMBER,
|
|
64
|
+
b: STRING,
|
|
65
|
+
});
|
|
66
|
+
expect(props).toEqual({a: 1, b: 'abc'});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('should return "undefined" item in case a value is not provided', () => {
|
|
70
|
+
const container = createContainer();
|
|
71
|
+
container.bindValue(NUMBER, 1);
|
|
72
|
+
|
|
73
|
+
const props = tryResolveValue(container, {a: NUMBER, b: STRING});
|
|
74
|
+
expect(props).toEqual({a: 1, b: undefined});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should return values of optional tokens in case they are not provided', () => {
|
|
78
|
+
const container = createContainer();
|
|
79
|
+
container.bindValue(NUMBER, 1);
|
|
80
|
+
|
|
81
|
+
const OPTIONAL_STRING = optional(STRING, 'value');
|
|
82
|
+
|
|
83
|
+
const props = tryResolveValue(container, {
|
|
84
|
+
a: NUMBER,
|
|
85
|
+
b: STRING,
|
|
86
|
+
c: OPTIONAL_STRING,
|
|
87
|
+
});
|
|
88
|
+
expect(props).toEqual({a: 1, b: undefined, c: 'value'});
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('tryResolveValues()', () => {
|
|
57
93
|
it('should return values for the tokens', () => {
|
|
58
94
|
const container = createContainer();
|
|
59
95
|
container.bindValue(NUMBER, 1);
|
|
60
96
|
container.bindValue(STRING, 'abc');
|
|
61
97
|
|
|
62
|
-
const values: [number, string] =
|
|
98
|
+
const values: [number, string] = tryResolveValues(
|
|
99
|
+
container,
|
|
100
|
+
NUMBER,
|
|
101
|
+
STRING,
|
|
102
|
+
);
|
|
63
103
|
expect(values).toEqual([1, 'abc']);
|
|
64
104
|
});
|
|
65
105
|
|
|
@@ -67,7 +107,7 @@ describe('getValues', () => {
|
|
|
67
107
|
const container = createContainer();
|
|
68
108
|
container.bindValue(NUMBER, 1);
|
|
69
109
|
|
|
70
|
-
const values =
|
|
110
|
+
const values = tryResolveValues(container, NUMBER, STRING);
|
|
71
111
|
expect(values).toEqual([1, undefined]);
|
|
72
112
|
});
|
|
73
113
|
|
|
@@ -77,7 +117,7 @@ describe('getValues', () => {
|
|
|
77
117
|
|
|
78
118
|
const OPTIONAL_STRING = optional(STRING, 'value');
|
|
79
119
|
|
|
80
|
-
const values =
|
|
120
|
+
const values = tryResolveValues(container, NUMBER, STRING, OPTIONAL_STRING);
|
|
81
121
|
expect(values).toEqual([1, undefined, 'value']);
|
|
82
122
|
});
|
|
83
123
|
|
|
@@ -85,7 +125,7 @@ describe('getValues', () => {
|
|
|
85
125
|
const container = createContainer();
|
|
86
126
|
container.bindValue(NUMBER, 1);
|
|
87
127
|
|
|
88
|
-
const values =
|
|
128
|
+
const values = tryResolveValues(
|
|
89
129
|
container,
|
|
90
130
|
NUMBER,
|
|
91
131
|
NUMBER,
|
|
@@ -102,7 +142,43 @@ describe('getValues', () => {
|
|
|
102
142
|
});
|
|
103
143
|
});
|
|
104
144
|
|
|
105
|
-
describe('
|
|
145
|
+
describe('resolveValue()', () => {
|
|
146
|
+
it('should an object with resolved values from the container for the specified token props', () => {
|
|
147
|
+
const container = createContainer();
|
|
148
|
+
container.bindValue(NUMBER, 1);
|
|
149
|
+
container.bindValue(STRING, 'abc');
|
|
150
|
+
|
|
151
|
+
const props: {a: number; b: string} = resolveValue(container, {
|
|
152
|
+
a: NUMBER,
|
|
153
|
+
b: STRING,
|
|
154
|
+
});
|
|
155
|
+
expect(props).toEqual({a: 1, b: 'abc'});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('should throw an error in case a value is not provided', () => {
|
|
159
|
+
const container = createContainer();
|
|
160
|
+
container.bindValue(NUMBER, 1);
|
|
161
|
+
|
|
162
|
+
expect(() => {
|
|
163
|
+
resolveValue(container, {a: NUMBER, b: STRING});
|
|
164
|
+
}).toThrowError(
|
|
165
|
+
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it('should resolve values of optional tokens in case they are not provided', () => {
|
|
170
|
+
const container = createContainer();
|
|
171
|
+
container.bindValue(NUMBER, 1);
|
|
172
|
+
|
|
173
|
+
const props = resolveValue(container, {
|
|
174
|
+
a: NUMBER,
|
|
175
|
+
b: optional(STRING, 'value'),
|
|
176
|
+
});
|
|
177
|
+
expect(props).toEqual({a: 1, b: 'value'});
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe('resolveValues()', () => {
|
|
106
182
|
it('should resolve tokens from the container', () => {
|
|
107
183
|
const container = createContainer();
|
|
108
184
|
container.bindValue(NUMBER, 1);
|
|
@@ -127,12 +203,30 @@ describe('resolveValues', () => {
|
|
|
127
203
|
const container = createContainer();
|
|
128
204
|
container.bindValue(NUMBER, 1);
|
|
129
205
|
|
|
130
|
-
const values =
|
|
206
|
+
const values = resolveValues(container, NUMBER, optional(STRING, 'value'));
|
|
131
207
|
expect(values).toEqual([1, 'value']);
|
|
132
208
|
});
|
|
209
|
+
|
|
210
|
+
it('should resolve a dictionary of tokens', () => {
|
|
211
|
+
const container = createContainer();
|
|
212
|
+
container.bindValue(NUMBER, 1);
|
|
213
|
+
|
|
214
|
+
const values = resolveValue(container, {
|
|
215
|
+
a: NUMBER,
|
|
216
|
+
b: optional(STRING, 'value'),
|
|
217
|
+
});
|
|
218
|
+
expect(values).toEqual({
|
|
219
|
+
a: 1,
|
|
220
|
+
b: 'value',
|
|
221
|
+
});
|
|
222
|
+
});
|
|
133
223
|
});
|
|
134
224
|
|
|
135
225
|
describe('injectable()', () => {
|
|
226
|
+
function join({a, b}: {a: number; b: string}): string {
|
|
227
|
+
return `${a}/${b}`;
|
|
228
|
+
}
|
|
229
|
+
|
|
136
230
|
it('should inject values from Container as arguments of the factory', () => {
|
|
137
231
|
const container = createContainer();
|
|
138
232
|
container.bindValue(NUMBER, 1);
|
|
@@ -174,83 +268,104 @@ describe('injectable()', () => {
|
|
|
174
268
|
|
|
175
269
|
expect(decoratedFactory(container)).toBe('1value');
|
|
176
270
|
});
|
|
177
|
-
});
|
|
178
271
|
|
|
179
|
-
|
|
180
|
-
it('should return an object with values by the tokens', () => {
|
|
272
|
+
it('should inject values from Container as an object for the first argument of the factory', () => {
|
|
181
273
|
const container = createContainer();
|
|
182
274
|
container.bindValue(NUMBER, 1);
|
|
183
|
-
container.bindValue(STRING, '
|
|
275
|
+
container.bindValue(STRING, '2');
|
|
184
276
|
|
|
185
|
-
const
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
});
|
|
189
|
-
expect(props).toEqual({a: 1, b: 'abc'});
|
|
277
|
+
const decoratedFactory = injectable(join, {a: NUMBER, b: STRING});
|
|
278
|
+
|
|
279
|
+
expect(decoratedFactory(container)).toBe('1/2');
|
|
190
280
|
});
|
|
191
281
|
|
|
192
|
-
it('should
|
|
282
|
+
it('should throw ResolverError error in case a value is not provided', () => {
|
|
193
283
|
const container = createContainer();
|
|
194
284
|
container.bindValue(NUMBER, 1);
|
|
195
285
|
|
|
196
|
-
const
|
|
197
|
-
|
|
286
|
+
const decoratedFactory = injectable(join, {a: NUMBER, b: STRING});
|
|
287
|
+
|
|
288
|
+
expect(() => decoratedFactory(container)).toThrowError(
|
|
289
|
+
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
|
290
|
+
);
|
|
198
291
|
});
|
|
199
292
|
|
|
200
|
-
it('should
|
|
293
|
+
it('should inject values of optional tokens in case values are not provided', () => {
|
|
201
294
|
const container = createContainer();
|
|
202
295
|
container.bindValue(NUMBER, 1);
|
|
203
296
|
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
const props = getProps(container, {
|
|
297
|
+
const decoratedFactory = injectable(join, {
|
|
207
298
|
a: NUMBER,
|
|
208
|
-
b: STRING,
|
|
209
|
-
c: OPTIONAL_STRING,
|
|
299
|
+
b: optional(STRING, 'value'),
|
|
210
300
|
});
|
|
211
|
-
|
|
301
|
+
|
|
302
|
+
expect(decoratedFactory(container)).toBe('1/value');
|
|
212
303
|
});
|
|
213
304
|
});
|
|
214
305
|
|
|
215
|
-
describe('
|
|
216
|
-
|
|
306
|
+
describe('injectableClass()', () => {
|
|
307
|
+
class TestClass {
|
|
308
|
+
a: number;
|
|
309
|
+
b: string;
|
|
310
|
+
|
|
311
|
+
constructor(a: number, b: string) {
|
|
312
|
+
this.a = a;
|
|
313
|
+
this.b = b;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
concat(): string {
|
|
317
|
+
return this.a + this.b;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
it('should inject values from Container as arguments of the constructor', () => {
|
|
217
322
|
const container = createContainer();
|
|
218
323
|
container.bindValue(NUMBER, 1);
|
|
219
|
-
container.bindValue(STRING, '
|
|
324
|
+
container.bindValue(STRING, '2');
|
|
220
325
|
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
});
|
|
225
|
-
expect(props).toEqual({a: 1, b: 'abc'});
|
|
326
|
+
const factory = injectableClass(TestClass, NUMBER, STRING);
|
|
327
|
+
|
|
328
|
+
expect(factory(container).concat()).toBe('12');
|
|
226
329
|
});
|
|
227
330
|
|
|
228
|
-
it('should throw
|
|
331
|
+
it('should throw ResolverError error in case a value is not provided', () => {
|
|
229
332
|
const container = createContainer();
|
|
230
333
|
container.bindValue(NUMBER, 1);
|
|
231
334
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
335
|
+
const factory = injectableClass(TestClass, NUMBER, STRING);
|
|
336
|
+
|
|
337
|
+
expect(() => factory(container)).toThrowError(
|
|
235
338
|
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
|
236
339
|
);
|
|
237
340
|
});
|
|
238
341
|
|
|
239
|
-
it('should
|
|
342
|
+
it('should inject values of optional tokens in case values are not provided', () => {
|
|
240
343
|
const container = createContainer();
|
|
241
344
|
container.bindValue(NUMBER, 1);
|
|
242
345
|
|
|
243
|
-
const
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
346
|
+
const factory = injectableClass(
|
|
347
|
+
TestClass,
|
|
348
|
+
NUMBER,
|
|
349
|
+
optional(STRING, 'value'),
|
|
350
|
+
);
|
|
351
|
+
|
|
352
|
+
expect(factory(container).concat()).toBe('1value');
|
|
248
353
|
});
|
|
249
354
|
});
|
|
250
355
|
|
|
251
|
-
describe('
|
|
252
|
-
|
|
253
|
-
|
|
356
|
+
describe('injectableClass()', () => {
|
|
357
|
+
class TestClass {
|
|
358
|
+
a: number;
|
|
359
|
+
b: string;
|
|
360
|
+
|
|
361
|
+
constructor(props: {a: number; b: string}) {
|
|
362
|
+
this.a = props.a;
|
|
363
|
+
this.b = props.b;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
join(): string {
|
|
367
|
+
return `${this.a}/${this.b}`;
|
|
368
|
+
}
|
|
254
369
|
}
|
|
255
370
|
|
|
256
371
|
it('should inject values from Container as an object for the first argument of the factory', () => {
|
|
@@ -258,18 +373,18 @@ describe('injectableProps()', () => {
|
|
|
258
373
|
container.bindValue(NUMBER, 1);
|
|
259
374
|
container.bindValue(STRING, '2');
|
|
260
375
|
|
|
261
|
-
const
|
|
376
|
+
const factory = injectableClass(TestClass, {a: NUMBER, b: STRING});
|
|
262
377
|
|
|
263
|
-
expect(
|
|
378
|
+
expect(factory(container).join()).toBe('1/2');
|
|
264
379
|
});
|
|
265
380
|
|
|
266
381
|
it('should throw ResolverError error in case a value is not provided', () => {
|
|
267
382
|
const container = createContainer();
|
|
268
383
|
container.bindValue(NUMBER, 1);
|
|
269
384
|
|
|
270
|
-
const
|
|
385
|
+
const factory = injectableClass(TestClass, {a: NUMBER, b: STRING});
|
|
271
386
|
|
|
272
|
-
expect(() =>
|
|
387
|
+
expect(() => factory(container)).toThrowError(
|
|
273
388
|
new ResolverError(`Token "${STRING.symbol.description}" is not provided`),
|
|
274
389
|
);
|
|
275
390
|
});
|
|
@@ -278,11 +393,11 @@ describe('injectableProps()', () => {
|
|
|
278
393
|
const container = createContainer();
|
|
279
394
|
container.bindValue(NUMBER, 1);
|
|
280
395
|
|
|
281
|
-
const
|
|
396
|
+
const factory = injectableClass(TestClass, {
|
|
282
397
|
a: NUMBER,
|
|
283
398
|
b: optional(STRING, 'value'),
|
|
284
399
|
});
|
|
285
400
|
|
|
286
|
-
expect(
|
|
401
|
+
expect(factory(container).join()).toBe('1/value');
|
|
287
402
|
});
|
|
288
403
|
});
|