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
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { token, optional, ResolverError, createContainer } from './ditox';
|
|
2
|
+
export type { RequiredToken, OptionalToken, Token, FactoryScope, FactoryOptions, Container, } from './ditox';
|
|
3
|
+
export { isToken, bindMultiValue, tryResolveValue, tryResolveValues, resolveValue, resolveValues, injectable, injectableClass, } from './utils';
|
|
4
|
+
export { getValues, getProps, resolveProps, injectableProps, } from './utils-deprecated';
|
|
5
|
+
export { bindModule, bindModules, declareModule, declareModuleBindings, } from './modules';
|
|
6
|
+
export type { Module, ModuleDeclaration, BindModuleOptions, ModuleBindingEntry, } from './modules';
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.declareModuleBindings = exports.declareModule = exports.bindModules = exports.bindModule = exports.injectableProps = exports.resolveProps = exports.getProps = exports.getValues = exports.injectableClass = exports.injectable = exports.resolveValues = exports.resolveValue = exports.tryResolveValues = exports.tryResolveValue = exports.bindMultiValue = exports.isToken = exports.createContainer = exports.ResolverError = exports.optional = exports.token = void 0;
|
|
4
|
+
var ditox_1 = require("./ditox");
|
|
5
|
+
Object.defineProperty(exports, "token", { enumerable: true, get: function () { return ditox_1.token; } });
|
|
6
|
+
Object.defineProperty(exports, "optional", { enumerable: true, get: function () { return ditox_1.optional; } });
|
|
7
|
+
Object.defineProperty(exports, "ResolverError", { enumerable: true, get: function () { return ditox_1.ResolverError; } });
|
|
8
|
+
Object.defineProperty(exports, "createContainer", { enumerable: true, get: function () { return ditox_1.createContainer; } });
|
|
9
|
+
var utils_1 = require("./utils");
|
|
10
|
+
Object.defineProperty(exports, "isToken", { enumerable: true, get: function () { return utils_1.isToken; } });
|
|
11
|
+
Object.defineProperty(exports, "bindMultiValue", { enumerable: true, get: function () { return utils_1.bindMultiValue; } });
|
|
12
|
+
Object.defineProperty(exports, "tryResolveValue", { enumerable: true, get: function () { return utils_1.tryResolveValue; } });
|
|
13
|
+
Object.defineProperty(exports, "tryResolveValues", { enumerable: true, get: function () { return utils_1.tryResolveValues; } });
|
|
14
|
+
Object.defineProperty(exports, "resolveValue", { enumerable: true, get: function () { return utils_1.resolveValue; } });
|
|
15
|
+
Object.defineProperty(exports, "resolveValues", { enumerable: true, get: function () { return utils_1.resolveValues; } });
|
|
16
|
+
Object.defineProperty(exports, "injectable", { enumerable: true, get: function () { return utils_1.injectable; } });
|
|
17
|
+
Object.defineProperty(exports, "injectableClass", { enumerable: true, get: function () { return utils_1.injectableClass; } });
|
|
18
|
+
var utils_deprecated_1 = require("./utils-deprecated");
|
|
19
|
+
Object.defineProperty(exports, "getValues", { enumerable: true, get: function () { return utils_deprecated_1.getValues; } });
|
|
20
|
+
Object.defineProperty(exports, "getProps", { enumerable: true, get: function () { return utils_deprecated_1.getProps; } });
|
|
21
|
+
Object.defineProperty(exports, "resolveProps", { enumerable: true, get: function () { return utils_deprecated_1.resolveProps; } });
|
|
22
|
+
Object.defineProperty(exports, "injectableProps", { enumerable: true, get: function () { return utils_deprecated_1.injectableProps; } });
|
|
23
|
+
var modules_1 = require("./modules");
|
|
24
|
+
Object.defineProperty(exports, "bindModule", { enumerable: true, get: function () { return modules_1.bindModule; } });
|
|
25
|
+
Object.defineProperty(exports, "bindModules", { enumerable: true, get: function () { return modules_1.bindModules; } });
|
|
26
|
+
Object.defineProperty(exports, "declareModule", { enumerable: true, get: function () { return modules_1.declareModule; } });
|
|
27
|
+
Object.defineProperty(exports, "declareModuleBindings", { enumerable: true, get: function () { return modules_1.declareModuleBindings; } });
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,iCAAwE;AAAhE,8FAAA,KAAK,OAAA;AAAE,iGAAA,QAAQ,OAAA;AAAE,sGAAA,aAAa,OAAA;AAAE,wGAAA,eAAe,OAAA;AAUvD,iCASiB;AARf,gGAAA,OAAO,OAAA;AACP,uGAAA,cAAc,OAAA;AACd,wGAAA,eAAe,OAAA;AACf,yGAAA,gBAAgB,OAAA;AAChB,qGAAA,YAAY,OAAA;AACZ,sGAAA,aAAa,OAAA;AACb,mGAAA,UAAU,OAAA;AACV,wGAAA,eAAe,OAAA;AAGjB,uDAK4B;AAJ1B,6GAAA,SAAS,OAAA;AACT,4GAAA,QAAQ,OAAA;AACR,gHAAA,YAAY,OAAA;AACZ,mHAAA,eAAe,OAAA;AAGjB,qCAKmB;AAJjB,qGAAA,UAAU,OAAA;AACV,sGAAA,WAAW,OAAA;AACX,wGAAA,aAAa,OAAA;AACb,gHAAA,qBAAqB,OAAA"}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { Container, Token } from './ditox';
|
|
2
|
+
declare type AnyObject = Record<string, any>;
|
|
3
|
+
declare type EmptyObject = Record<string, never>;
|
|
4
|
+
declare type ModuleController = {
|
|
5
|
+
/** Dispose the module and clean its resources */
|
|
6
|
+
destroy?: () => void;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Dependency module
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* type LoggerModule = Module<{
|
|
14
|
+
* logger: Logger;
|
|
15
|
+
* }>;
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare type Module<ModuleProps extends AnyObject = EmptyObject> = ModuleController & ModuleProps;
|
|
19
|
+
declare type GetModuleProps<T> = T extends Module<infer Props> ? Props : never;
|
|
20
|
+
/**
|
|
21
|
+
* Description how to bind the module in declarative way.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
26
|
+
* token: LOGGER_MODULE_TOKEN,
|
|
27
|
+
* factory: (container) => {
|
|
28
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
29
|
+
* return {
|
|
30
|
+
* logger: { log: (message) => transport.write(message) },
|
|
31
|
+
* destroy: () => transport.close(),
|
|
32
|
+
* }
|
|
33
|
+
* },
|
|
34
|
+
* exports: {
|
|
35
|
+
* logger: LOGGER_TOKEN,
|
|
36
|
+
* },
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export declare type ModuleDeclaration<T extends Module<AnyObject>> = {
|
|
41
|
+
/** Token for the module */
|
|
42
|
+
token: Token<T>;
|
|
43
|
+
/** Modules for binding */
|
|
44
|
+
imports?: ReadonlyArray<ModuleBindingEntry>;
|
|
45
|
+
/** Factory of the module */
|
|
46
|
+
factory: (container: Container) => T;
|
|
47
|
+
/** Dictionary of module properties which are bound to tokens. */
|
|
48
|
+
exports?: {
|
|
49
|
+
[K in keyof GetModuleProps<T>]?: Token<GetModuleProps<T>[K]>;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Dictionary of module properties which are bound to tokens.
|
|
53
|
+
*
|
|
54
|
+
* @deprecated Use `exports` property.
|
|
55
|
+
*/
|
|
56
|
+
exportedProps?: ModuleDeclaration<T>['exports'];
|
|
57
|
+
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
58
|
+
beforeBinding?: (container: Container) => void;
|
|
59
|
+
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
60
|
+
afterBinding?: (container: Container) => void;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* Options for module binding.
|
|
64
|
+
*
|
|
65
|
+
* `scope` types:
|
|
66
|
+
* - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
|
|
67
|
+
* - `scoped` - The module is created and cached by the container which starts resolving.
|
|
68
|
+
*/
|
|
69
|
+
export declare type BindModuleOptions = {
|
|
70
|
+
scope?: 'scoped' | 'singleton';
|
|
71
|
+
};
|
|
72
|
+
export declare type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
|
|
73
|
+
module: ModuleDeclaration<AnyObject>;
|
|
74
|
+
options: BindModuleOptions;
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Binds the dependency module to the container
|
|
78
|
+
* @param container - Dependency container.
|
|
79
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
80
|
+
* @param options - Options for module binding.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* bindModule(container, LOGGER_MODULE);
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
|
|
88
|
+
/**
|
|
89
|
+
* Binds dependency modules to the container
|
|
90
|
+
*
|
|
91
|
+
* @param container - Dependency container for binding
|
|
92
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
93
|
+
*/
|
|
94
|
+
export declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
|
|
95
|
+
/**
|
|
96
|
+
* Declares a module binding
|
|
97
|
+
*
|
|
98
|
+
* @param declaration - a module declaration
|
|
99
|
+
* @param declaration.token - optional field
|
|
100
|
+
*
|
|
101
|
+
* @example
|
|
102
|
+
* ```ts
|
|
103
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
104
|
+
* factory: (container) => {
|
|
105
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
106
|
+
* return {
|
|
107
|
+
* logger: { log: (message) => transport.write(message) },
|
|
108
|
+
* destroy: () => transport.close(),
|
|
109
|
+
* }
|
|
110
|
+
* },
|
|
111
|
+
* exports: {
|
|
112
|
+
* logger: LOGGER_TOKEN,
|
|
113
|
+
* },
|
|
114
|
+
* });
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
export declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
|
|
118
|
+
/**
|
|
119
|
+
* Declares bindings of several modules
|
|
120
|
+
*
|
|
121
|
+
* @param modules - module declaration entries
|
|
122
|
+
*/
|
|
123
|
+
export declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
|
|
124
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.declareModuleBindings = exports.declareModule = exports.bindModules = exports.bindModule = void 0;
|
|
4
|
+
const ditox_1 = require("./ditox");
|
|
5
|
+
const utils_1 = require("./utils");
|
|
6
|
+
/**
|
|
7
|
+
* Binds the dependency module to the container
|
|
8
|
+
* @param container - Dependency container.
|
|
9
|
+
* @param moduleDeclaration - Declaration of the dependency module.
|
|
10
|
+
* @param options - Options for module binding.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* bindModule(container, LOGGER_MODULE);
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function bindModule(container, moduleDeclaration, options) {
|
|
18
|
+
var _a;
|
|
19
|
+
const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
|
|
20
|
+
const exports = (_a = moduleDeclaration.exports) !== null && _a !== void 0 ? _a : moduleDeclaration.exportedProps;
|
|
21
|
+
const scope = options === null || options === void 0 ? void 0 : options.scope;
|
|
22
|
+
if (beforeBinding) {
|
|
23
|
+
beforeBinding(container);
|
|
24
|
+
}
|
|
25
|
+
if (imports) {
|
|
26
|
+
bindModules(container, imports);
|
|
27
|
+
}
|
|
28
|
+
const exportedValueTokens = new Set();
|
|
29
|
+
if (exports) {
|
|
30
|
+
const keys = Object.keys(exports);
|
|
31
|
+
keys.forEach((valueKey) => {
|
|
32
|
+
const valueToken = exports[valueKey];
|
|
33
|
+
if (valueToken) {
|
|
34
|
+
exportedValueTokens.add(valueToken);
|
|
35
|
+
container.bindFactory(valueToken, (0, utils_1.injectable)((module) => module[valueKey], token), { scope });
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
container.bindFactory(token, factory, {
|
|
40
|
+
scope,
|
|
41
|
+
onRemoved: (module) => {
|
|
42
|
+
if (module.destroy) {
|
|
43
|
+
module.destroy();
|
|
44
|
+
}
|
|
45
|
+
exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
|
|
46
|
+
exportedValueTokens.clear();
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
if (afterBinding) {
|
|
50
|
+
afterBinding(container);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
exports.bindModule = bindModule;
|
|
54
|
+
/**
|
|
55
|
+
* Binds dependency modules to the container
|
|
56
|
+
*
|
|
57
|
+
* @param container - Dependency container for binding
|
|
58
|
+
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
59
|
+
*/
|
|
60
|
+
function bindModules(container, modules) {
|
|
61
|
+
modules.forEach((entry) => {
|
|
62
|
+
if ('module' in entry) {
|
|
63
|
+
bindModule(container, entry.module, entry.options);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
bindModule(container, entry);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
exports.bindModules = bindModules;
|
|
71
|
+
/**
|
|
72
|
+
* Declares a module binding
|
|
73
|
+
*
|
|
74
|
+
* @param declaration - a module declaration
|
|
75
|
+
* @param declaration.token - optional field
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```ts
|
|
79
|
+
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
80
|
+
* factory: (container) => {
|
|
81
|
+
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
82
|
+
* return {
|
|
83
|
+
* logger: { log: (message) => transport.write(message) },
|
|
84
|
+
* destroy: () => transport.close(),
|
|
85
|
+
* }
|
|
86
|
+
* },
|
|
87
|
+
* exports: {
|
|
88
|
+
* logger: LOGGER_TOKEN,
|
|
89
|
+
* },
|
|
90
|
+
* });
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
function declareModule(declaration) {
|
|
94
|
+
var _a;
|
|
95
|
+
return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : (0, ditox_1.token)() };
|
|
96
|
+
}
|
|
97
|
+
exports.declareModule = declareModule;
|
|
98
|
+
/**
|
|
99
|
+
* Declares bindings of several modules
|
|
100
|
+
*
|
|
101
|
+
* @param modules - module declaration entries
|
|
102
|
+
*/
|
|
103
|
+
function declareModuleBindings(modules) {
|
|
104
|
+
return declareModule({
|
|
105
|
+
factory: () => ({}),
|
|
106
|
+
imports: modules,
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
exports.declareModuleBindings = declareModuleBindings;
|
|
110
|
+
//# sourceMappingURL=modules.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":";;;AAAA,mCAAgD;AAChD,mCAAmC;AA4FnC;;;;;;;;;;GAUG;AACH,SAAgB,UAAU,CACxB,SAAoB,EACpB,iBAAuC,EACvC,OAA2B;;IAE3B,MAAM,EAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,aAAa,EAAE,YAAY,EAAC,GAC1D,iBAAiB,CAAC;IACpB,MAAM,OAAO,GAAG,MAAA,iBAAiB,CAAC,OAAO,mCAAI,iBAAiB,CAAC,aAAa,CAAC;IAE7E,MAAM,KAAK,GAAG,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,KAAK,CAAC;IAE7B,IAAI,aAAa,EAAE;QACjB,aAAa,CAAC,SAAS,CAAC,CAAC;KAC1B;IAED,IAAI,OAAO,EAAE;QACX,WAAW,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;KACjC;IAED,MAAM,mBAAmB,GAAG,IAAI,GAAG,EAAkB,CAAC;IAEtD,IAAI,OAAO,EAAE;QACX,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAElC,IAAI,CAAC,OAAO,CAAC,CAAC,QAAQ,EAAE,EAAE;YACxB,MAAM,UAAU,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;YACrC,IAAI,UAAU,EAAE;gBACd,mBAAmB,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;gBAEpC,SAAS,CAAC,WAAW,CACnB,UAAU,EACV,IAAA,kBAAU,EAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,KAAK,CAAC,EAC/C,EAAC,KAAK,EAAC,CACR,CAAC;aACH;QACH,CAAC,CAAC,CAAC;KACJ;IAED,SAAS,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE;QACpC,KAAK;QACL,SAAS,EAAE,CAAC,MAAM,EAAE,EAAE;YACpB,IAAI,MAAM,CAAC,OAAO,EAAE;gBAClB,MAAM,CAAC,OAAO,EAAE,CAAC;aAClB;YAED,mBAAmB,CAAC,OAAO,CAAC,CAAC,UAAU,EAAE,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;YAC1E,mBAAmB,CAAC,KAAK,EAAE,CAAC;QAC9B,CAAC;KACF,CAAC,CAAC;IAEH,IAAI,YAAY,EAAE;QAChB,YAAY,CAAC,SAAS,CAAC,CAAC;KACzB;AACH,CAAC;AArDD,gCAqDC;AAED;;;;;GAKG;AACH,SAAgB,WAAW,CACzB,SAAoB,EACpB,OAA0C;IAE1C,OAAO,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,EAAE;QACxB,IAAI,QAAQ,IAAI,KAAK,EAAE;YACrB,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;SACpD;aAAM;YACL,UAAU,CAAC,SAAS,EAAE,KAAK,CAAC,CAAC;SAC9B;IACH,CAAC,CAAC,CAAC;AACL,CAAC;AAXD,kCAWC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,aAAa,CAC3B,WAC8C;;IAE9C,OAAO,EAAC,GAAG,WAAW,EAAE,KAAK,EAAE,MAAA,WAAW,CAAC,KAAK,mCAAI,IAAA,aAAK,GAAE,EAAC,CAAC;AAC/D,CAAC;AALD,sCAKC;AAED;;;;GAIG;AACH,SAAgB,qBAAqB,CACnC,OAA0C;IAE1C,OAAO,aAAa,CAAC;QACnB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;QACnB,OAAO,EAAE,OAAO;KACjB,CAAC,CAAC;AACL,CAAC;AAPD,sDAOC"}
|
|
@@ -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,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.injectableProps = exports.resolveProps = exports.getProps = exports.getValues = void 0;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
/**
|
|
6
|
+
* Returns an array of resolved values by the specified token.
|
|
7
|
+
* If a token is not found, then `undefined` value is used.
|
|
8
|
+
*
|
|
9
|
+
* @deprecated Use `tryResolveValues()` function.
|
|
10
|
+
*/
|
|
11
|
+
function getValues(container, ...tokens) {
|
|
12
|
+
return (0, utils_1.tryResolveValues)(container, ...tokens);
|
|
13
|
+
}
|
|
14
|
+
exports.getValues = getValues;
|
|
15
|
+
/**
|
|
16
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
17
|
+
* If a token is not found, then `undefined` value is used.
|
|
18
|
+
*
|
|
19
|
+
* @deprecated Use `tryResolveValue()` function.
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```ts
|
|
23
|
+
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
24
|
+
* console.log(props); // {a: 1, b: 2}
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function getProps(container, tokens) {
|
|
28
|
+
return (0, utils_1.tryResolveValue)(container, tokens);
|
|
29
|
+
}
|
|
30
|
+
exports.getProps = getProps;
|
|
31
|
+
/**
|
|
32
|
+
* Returns an object with resolved properties which are specified by token properties.
|
|
33
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
34
|
+
*
|
|
35
|
+
* @deprecated Use `resolveValue()` function.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
40
|
+
* console.log(props); // {a: 1, b: 2}
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
function resolveProps(container, tokens) {
|
|
44
|
+
return (0, utils_1.resolveValue)(container, tokens);
|
|
45
|
+
}
|
|
46
|
+
exports.resolveProps = resolveProps;
|
|
47
|
+
/**
|
|
48
|
+
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
49
|
+
* @param factory - A factory.
|
|
50
|
+
* @param tokens - Object with tokens.
|
|
51
|
+
* @return Decorated factory which takes a dependency container as a single argument.
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Use `injectable()` function.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
58
|
+
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
59
|
+
* const result = decoratedFactory(container);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function injectableProps(factory, tokens) {
|
|
63
|
+
return (0, utils_1.injectable)(factory, tokens);
|
|
64
|
+
}
|
|
65
|
+
exports.injectableProps = injectableProps;
|
|
66
|
+
//# sourceMappingURL=utils-deprecated.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils-deprecated.js","sourceRoot":"","sources":["../../src/utils-deprecated.ts"],"names":[],"mappings":";;;AACA,mCAKiB;AAOjB;;;;;GAKG;AACH,SAAgB,SAAS,CAKvB,SAAoB,EAAE,GAAG,MAAc;IACvC,OAAO,IAAA,wBAAgB,EAAC,SAAS,EAAE,GAAG,MAAM,CAAC,CAAC;AAChD,CAAC;AAPD,8BAOC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,QAAQ,CACtB,SAAoB,EACpB,MAAyB;IAEzB,OAAO,IAAA,uBAAe,EAAC,SAAS,EAAE,MAAM,CAAmB,CAAC;AAC9D,CAAC;AALD,4BAKC;AAED;;;;;;;;;;;GAWG;AACH,SAAgB,YAAY,CAC1B,SAAoB,EACpB,MAAyB;IAEzB,OAAO,IAAA,oBAAY,EAAC,SAAS,EAAE,MAAM,CAAU,CAAC;AAClD,CAAC;AALD,oCAKC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,eAAe,CAC7B,OAAiC,EACjC,MAAyB;IAEzB,OAAO,IAAA,kBAAU,EAAC,OAAc,EAAE,MAAM,CAAC,CAAC;AAC5C,CAAC;AALD,0CAKC"}
|
|
@@ -0,0 +1,136 @@
|
|
|
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
|
+
* Checks if a value is the token
|
|
10
|
+
*/
|
|
11
|
+
export declare function isToken<T>(value: unknown): value is Token<T>;
|
|
12
|
+
/**
|
|
13
|
+
* Rebinds the array by the token with added new value.
|
|
14
|
+
* @param container - Dependency container.
|
|
15
|
+
* @param token - Token for an array of values.
|
|
16
|
+
* @param value - New value which is added to the end of the array.
|
|
17
|
+
*/
|
|
18
|
+
export declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
|
|
19
|
+
/**
|
|
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
|
+
|
|
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
|
+
* ```
|
|
35
|
+
*/
|
|
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;
|
|
39
|
+
/**
|
|
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
|
+
* ```
|
|
55
|
+
*/
|
|
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;
|
|
60
|
+
}>(container: Container, ...tokens: Tokens): Values;
|
|
61
|
+
/**
|
|
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.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* const value = resolveValue(container, tokenA);
|
|
72
|
+
* console.log(value); // 1
|
|
73
|
+
*
|
|
74
|
+
* const props = resolveValue(container, {a: tokenA, b: tokenB});
|
|
75
|
+
* console.log(props); // {a: 1, b: 2}
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
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;
|
|
81
|
+
/**
|
|
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
|
+
|
|
87
|
+
* If a token is not found, then `ResolverError` is thrown.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
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}]
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
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;
|
|
103
|
+
/**
|
|
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
|
+
*
|
|
109
|
+
* @param factory - A factory.
|
|
110
|
+
* @param tokens - Tokens which correspond to factory arguments.
|
|
111
|
+
*
|
|
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.
|
|
121
|
+
*
|
|
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.
|
|
130
|
+
*/
|
|
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;
|
|
136
|
+
export {};
|