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/README.md +5 -4
- package/dist/browser/index.d.ts +132 -12
- package/dist/browser/index.js +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -1
- package/dist/cjs/index.js +11 -5
- package/dist/cjs/index.js.map +1 -1
- 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 +96 -34
- package/dist/cjs/utils.js +109 -51
- package/dist/cjs/utils.js.map +1 -1
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- 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 +96 -34
- package/dist/esm/utils.js +101 -44
- package/dist/esm/utils.js.map +1 -1
- package/dist/umd/index.d.ts +132 -12
- package/dist/umd/index.js +1 -1
- package/dist/umd/index.js.map +1 -1
- package/docs/README.md +281 -30
- package/docs/classes/ResolverError.md +1 -1
- package/package.json +2 -2
- package/src/index.ts +10 -2
- 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 +163 -65
package/dist/cjs/utils.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
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
|
|
20
|
-
[
|
|
21
|
-
}>(container: Container,
|
|
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
|
|
24
|
-
*
|
|
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
|
|
27
|
-
[
|
|
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
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
|
|
35
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
56
|
-
* console.log(
|
|
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
|
|
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
|
|
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 -
|
|
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
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
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
|
|
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/cjs/utils.js
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.injectableClass = exports.injectable = exports.resolveValues = exports.resolveValue = exports.tryResolveValues = exports.tryResolveValue = exports.bindMultiValue = exports.isToken = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Checks if a value is the token
|
|
6
|
+
*/
|
|
7
|
+
function isToken(value) {
|
|
8
|
+
return (value !== undefined &&
|
|
9
|
+
value !== null &&
|
|
10
|
+
typeof value === 'object' &&
|
|
11
|
+
'symbol' in value &&
|
|
12
|
+
typeof value.symbol === 'symbol');
|
|
13
|
+
}
|
|
14
|
+
exports.isToken = isToken;
|
|
4
15
|
/**
|
|
5
16
|
* Rebinds the array by the token with added new value.
|
|
6
17
|
* @param container - Dependency container.
|
|
@@ -15,84 +26,131 @@ function bindMultiValue(container, token, value) {
|
|
|
15
26
|
}
|
|
16
27
|
exports.bindMultiValue = bindMultiValue;
|
|
17
28
|
/**
|
|
18
|
-
*
|
|
29
|
+
* Tries to resolve a value by the provided token.
|
|
30
|
+
*
|
|
31
|
+
* If an argument is an object which has tokens as its properties,
|
|
32
|
+
* then returns an object containing resolved values as properties.
|
|
33
|
+
|
|
19
34
|
* If a token is not found, then `undefined` value is used.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const value = tryResolveValue(container, tokenA);
|
|
39
|
+
* console.log(value); // 1
|
|
40
|
+
*
|
|
41
|
+
* const props = tryResolveValue(container, {a: tokenA, b: tokenB});
|
|
42
|
+
* console.log(props); // {a: 1, b: 2}
|
|
43
|
+
* ```
|
|
20
44
|
*/
|
|
21
|
-
function
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*/
|
|
29
|
-
function resolveValues(container, ...tokens) {
|
|
30
|
-
return tokens.map(container.resolve);
|
|
45
|
+
function tryResolveValue(container, token) {
|
|
46
|
+
if (isToken(token)) {
|
|
47
|
+
return container.get(token);
|
|
48
|
+
}
|
|
49
|
+
const obj = {};
|
|
50
|
+
Object.keys(token).forEach((key) => (obj[key] = container.get(token[key])));
|
|
51
|
+
return obj;
|
|
31
52
|
}
|
|
32
|
-
exports.
|
|
53
|
+
exports.tryResolveValue = tryResolveValue;
|
|
33
54
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
55
|
+
* Returns an array of resolved values or objects with resolved values.
|
|
56
|
+
*
|
|
57
|
+
* If an item of the array is an object which has tokens as its properties,
|
|
58
|
+
* then returns an object containing resolved values as properties.
|
|
59
|
+
|
|
60
|
+
* If a token is not found, then `undefined` value is used.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const items1 = tryResolveValues(container, tokenA);
|
|
65
|
+
* console.log(items1); // [1]
|
|
66
|
+
*
|
|
67
|
+
* const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
|
|
68
|
+
* console.log(items2); // [1, {a: 1, b: 2}]
|
|
69
|
+
* ```
|
|
38
70
|
*/
|
|
39
|
-
function
|
|
40
|
-
return (
|
|
41
|
-
const values = resolveValues(container, ...tokens);
|
|
42
|
-
return factory.apply(this, values);
|
|
43
|
-
};
|
|
71
|
+
function tryResolveValues(container, ...tokens) {
|
|
72
|
+
return tokens.map((item) => tryResolveValue(container, item));
|
|
44
73
|
}
|
|
45
|
-
exports.
|
|
74
|
+
exports.tryResolveValues = tryResolveValues;
|
|
46
75
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
76
|
+
* Resolves a value by the provided token.
|
|
77
|
+
*
|
|
78
|
+
* If an argument is an object which has tokens as its properties,
|
|
79
|
+
* then returns an object containing resolved values as properties.
|
|
80
|
+
|
|
81
|
+
* If a value is not found by the token, then `ResolverError` is thrown.
|
|
49
82
|
*
|
|
50
83
|
* @example
|
|
51
84
|
* ```ts
|
|
52
|
-
* const
|
|
85
|
+
* const value = resolveValue(container, tokenA);
|
|
86
|
+
* console.log(value); // 1
|
|
87
|
+
*
|
|
88
|
+
* const props = resolveValue(container, {a: tokenA, b: tokenB});
|
|
53
89
|
* console.log(props); // {a: 1, b: 2}
|
|
54
90
|
* ```
|
|
55
91
|
*/
|
|
56
|
-
function
|
|
57
|
-
|
|
58
|
-
|
|
92
|
+
function resolveValue(container, token) {
|
|
93
|
+
if (isToken(token)) {
|
|
94
|
+
return container.resolve(token);
|
|
95
|
+
}
|
|
96
|
+
const obj = {};
|
|
97
|
+
Object.keys(token).forEach((key) => (obj[key] = container.resolve(token[key])));
|
|
59
98
|
return obj;
|
|
60
99
|
}
|
|
61
|
-
exports.
|
|
100
|
+
exports.resolveValue = resolveValue;
|
|
62
101
|
/**
|
|
63
|
-
* Returns an
|
|
102
|
+
* Returns an array of resolved values or objects with resolved values.
|
|
103
|
+
*
|
|
104
|
+
* If an item of the array is an object which has tokens as its properties,
|
|
105
|
+
* then returns an object containing resolved values as properties.
|
|
106
|
+
|
|
64
107
|
* If a token is not found, then `ResolverError` is thrown.
|
|
65
108
|
*
|
|
66
109
|
* @example
|
|
67
110
|
* ```ts
|
|
68
|
-
* const
|
|
69
|
-
* console.log(
|
|
111
|
+
* const items1 = resolveValues(container, tokenA);
|
|
112
|
+
* console.log(items1); // [1]
|
|
113
|
+
*
|
|
114
|
+
* const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
|
|
115
|
+
* console.log(items2); // [1, {a: 1, b: 2}]
|
|
70
116
|
* ```
|
|
71
117
|
*/
|
|
72
|
-
function
|
|
73
|
-
|
|
74
|
-
Object.keys(obj).forEach((key) => (obj[key] = container.resolve(obj[key])));
|
|
75
|
-
return obj;
|
|
118
|
+
function resolveValues(container, ...tokens) {
|
|
119
|
+
return tokens.map((item) => resolveValue(container, item));
|
|
76
120
|
}
|
|
77
|
-
exports.
|
|
121
|
+
exports.resolveValues = resolveValues;
|
|
78
122
|
/**
|
|
79
|
-
* Decorates a factory by passing
|
|
123
|
+
* Decorates a factory by passing resolved values as factory arguments.
|
|
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
|
+
*
|
|
80
128
|
* @param factory - A factory.
|
|
81
|
-
* @param tokens -
|
|
82
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
129
|
+
* @param tokens - Tokens which correspond to factory arguments.
|
|
83
130
|
*
|
|
84
|
-
* @
|
|
85
|
-
* ```ts
|
|
86
|
-
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
87
|
-
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
88
|
-
* const result = decoratedFactory(container);
|
|
89
|
-
* ```
|
|
131
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
90
132
|
*/
|
|
91
|
-
function
|
|
133
|
+
function injectable(factory, ...tokens) {
|
|
92
134
|
return (container) => {
|
|
93
|
-
const values =
|
|
94
|
-
return factory(values);
|
|
135
|
+
const values = resolveValues(container, ...tokens);
|
|
136
|
+
return factory.apply(this, values);
|
|
95
137
|
};
|
|
96
138
|
}
|
|
97
|
-
exports.
|
|
139
|
+
exports.injectable = injectable;
|
|
140
|
+
/**
|
|
141
|
+
* Decorates a class by passing resolved values as arguments to its constructor.
|
|
142
|
+
*
|
|
143
|
+
* If an argument is an object which has tokens as its properties,
|
|
144
|
+
* then returns an object containing resolved values as properties.
|
|
145
|
+
*
|
|
146
|
+
* @param constructor - Constructor of a class
|
|
147
|
+
* @param tokens - Tokens which correspond to constructor arguments
|
|
148
|
+
*
|
|
149
|
+
* @return A factory function which takes a dependency container as a single argument
|
|
150
|
+
* and returns a new created class.
|
|
151
|
+
*/
|
|
152
|
+
function injectableClass(constructor, ...tokens) {
|
|
153
|
+
return injectable((...values) => new constructor(...values), ...tokens);
|
|
154
|
+
}
|
|
155
|
+
exports.injectableClass = injectableClass;
|
|
98
156
|
//# sourceMappingURL=utils.js.map
|
package/dist/cjs/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAOA;;;;;GAKG;AACH,SAAgB,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,SAA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ACf,CAAC,GAAG,MAAM,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,GAAG,MAAM,CAAC,EACzC,GAAG,MAAM,CACV,CAAC;AACJ,CAAC;AAnBD,0CAmBC"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { token, optional, ResolverError, createContainer } from './ditox';
|
|
2
2
|
export type { RequiredToken, OptionalToken, Token, FactoryScope, FactoryOptions, Container, } from './ditox';
|
|
3
|
-
export { bindMultiValue,
|
|
3
|
+
export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
|
|
4
|
+
export { getValues, getProps, resolveProps, injectableProps, } from './utils-deprecated';
|
|
4
5
|
export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
|
|
5
6
|
export type { Module, ModuleDeclaration, BindModuleOptions, ModuleBindingEntry, } from './modules';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { token, optional, ResolverError, createContainer } from './ditox';
|
|
2
|
-
export { bindMultiValue,
|
|
2
|
+
export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
|
|
3
|
+
export { getValues, getProps, resolveProps, injectableProps, } from './utils-deprecated';
|
|
3
4
|
export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
|
|
4
5
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,SAAS,CAAC;AAUxE,OAAO,EACL,cAAc,EACd,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,KAAK,EAAE,QAAQ,EAAE,aAAa,EAAE,eAAe,EAAC,MAAM,SAAS,CAAC;AAUxE,OAAO,EACL,OAAO,EACP,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,YAAY,EACZ,aAAa,EACb,UAAU,EACV,eAAe,GAChB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,SAAS,EACT,QAAQ,EACR,YAAY,EACZ,eAAe,GAChB,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EACL,UAAU,EACV,WAAW,EACX,aAAa,EACb,qBAAqB,GACtB,MAAM,WAAW,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { Container, Token } from './ditox';
|
|
2
|
+
declare type ValuesProps = {
|
|
3
|
+
[key: string]: unknown;
|
|
4
|
+
};
|
|
5
|
+
declare type TokenProps<Props extends ValuesProps> = {
|
|
6
|
+
[K in keyof Props]: Token<Props[K]>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Returns an array of resolved values by the specified token.
|
|
10
|
+
* If a token is not found, then `undefined` value is used.
|
|
11
|
+
*
|
|
12
|
+
* @deprecated Use `tryResolveValues()` function.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getValues<Tokens extends Token<unknown>[], Values extends {
|
|
15
|
+
[K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
|
|
16
|
+
}>(container: Container, ...tokens: Tokens): Values;
|
|
17
|
+
/**
|
|
18
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
19
|
+
* If a token is not found, then `undefined` value is used.
|
|
20
|
+
*
|
|
21
|
+
* @deprecated Use `tryResolveValue()` function.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
26
|
+
* console.log(props); // {a: 1, b: 2}
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
|
|
30
|
+
/**
|
|
31
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
32
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
33
|
+
*
|
|
34
|
+
* @deprecated Use `resolveValue()` function.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
39
|
+
* console.log(props); // {a: 1, b: 2}
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
|
|
43
|
+
/**
|
|
44
|
+
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
45
|
+
* @param factory - A factory.
|
|
46
|
+
* @param tokens - Object with tokens.
|
|
47
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
48
|
+
*
|
|
49
|
+
* @deprecated Use `injectable()` function.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
54
|
+
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
55
|
+
* const result = decoratedFactory(container);
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
export declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { injectable, resolveValue, tryResolveValue, tryResolveValues, } from './utils';
|
|
2
|
+
/**
|
|
3
|
+
* Returns an array of resolved values by the specified token.
|
|
4
|
+
* If a token is not found, then `undefined` value is used.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Use `tryResolveValues()` function.
|
|
7
|
+
*/
|
|
8
|
+
export function getValues(container, ...tokens) {
|
|
9
|
+
return tryResolveValues(container, ...tokens);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
13
|
+
* If a token is not found, then `undefined` value is used.
|
|
14
|
+
*
|
|
15
|
+
* @deprecated Use `tryResolveValue()` function.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
20
|
+
* console.log(props); // {a: 1, b: 2}
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export function getProps(container, tokens) {
|
|
24
|
+
return tryResolveValue(container, tokens);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
28
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
29
|
+
*
|
|
30
|
+
* @deprecated Use `resolveValue()` function.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
35
|
+
* console.log(props); // {a: 1, b: 2}
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export function resolveProps(container, tokens) {
|
|
39
|
+
return resolveValue(container, tokens);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
43
|
+
* @param factory - A factory.
|
|
44
|
+
* @param tokens - Object with tokens.
|
|
45
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
46
|
+
*
|
|
47
|
+
* @deprecated Use `injectable()` function.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
52
|
+
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
53
|
+
* const result = decoratedFactory(container);
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
export function injectableProps(factory, tokens) {
|
|
57
|
+
return injectable(factory, tokens);
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=utils-deprecated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-deprecated.js","sourceRoot":"","sources":["../../src/utils-deprecated.ts"],"names":[],"mappings":"AACA,OAAO,EACL,UAAU,EACV,YAAY,EACZ,eAAe,EACf,gBAAgB,GACjB,MAAM,SAAS,CAAC;AAOjB;;;;;GAKG;AACH,MAAM,UAAU,SAAS,CAKvB,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,gBAAgB,CAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC;AAChD,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,QAAQ,CACtB,SAAoB,EACpB,MAAyB;IAEzB,OAAO,eAAe,CAAC,SAAS,EAAE,MAAM,CAAmB,CAAC;AAC9D,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,YAAY,CAC1B,SAAoB,EACpB,MAAyB;IAEzB,OAAO,YAAY,CAAC,SAAS,EAAE,MAAM,CAAU,CAAC;AAClD,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,eAAe,CAC7B,OAAiC,EACjC,MAAyB;IAEzB,OAAO,UAAU,CAAC,OAAc,EAAE,MAAM,CAAC,CAAC;AAC5C,CAAC"}
|