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.
Files changed (69) hide show
  1. package/README.md +6 -5
  2. package/dist/{index.d.ts → browser/index.d.ts} +98 -42
  3. package/dist/browser/index.js +2 -0
  4. package/dist/browser/index.js.map +1 -0
  5. package/dist/cjs/ditox.d.ts +125 -0
  6. package/dist/cjs/ditox.js +210 -0
  7. package/dist/cjs/ditox.js.map +1 -0
  8. package/dist/cjs/index.d.ts +5 -0
  9. package/dist/cjs/index.js +23 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/modules.d.ts +118 -0
  12. package/dist/cjs/modules.js +109 -0
  13. package/dist/cjs/modules.js.map +1 -0
  14. package/dist/cjs/utils-deprecated.d.ts +59 -0
  15. package/dist/cjs/utils-deprecated.js +66 -0
  16. package/dist/cjs/utils-deprecated.js.map +1 -0
  17. package/dist/cjs/utils.d.ts +136 -0
  18. package/dist/cjs/utils.js +156 -0
  19. package/dist/cjs/utils.js.map +1 -0
  20. package/dist/esm/ditox.d.ts +125 -0
  21. package/{lib/index.js → dist/esm/ditox.js} +10 -201
  22. package/dist/esm/ditox.js.map +1 -0
  23. package/dist/esm/index.d.ts +5 -0
  24. package/dist/esm/index.js +4 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/modules.d.ts +118 -0
  27. package/dist/esm/modules.js +102 -0
  28. package/dist/esm/modules.js.map +1 -0
  29. package/dist/esm/utils-deprecated.d.ts +59 -0
  30. package/dist/esm/utils-deprecated.js +59 -0
  31. package/dist/esm/utils-deprecated.js.map +1 -0
  32. package/dist/esm/utils.d.ts +136 -0
  33. package/dist/esm/utils.js +145 -0
  34. package/dist/esm/utils.js.map +1 -0
  35. package/{lib → dist/umd}/index.d.ts +98 -42
  36. package/dist/umd/index.js +2 -0
  37. package/dist/umd/index.js.map +1 -0
  38. package/docs/.nojekyll +1 -0
  39. package/docs/README.md +250 -164
  40. package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
  41. package/package.json +15 -16
  42. package/src/ditox.test.ts +10 -1
  43. package/src/index.ts +13 -13
  44. package/src/modules.test.ts +2 -0
  45. package/src/modules.ts +1 -8
  46. package/src/utils.test.ts +173 -58
  47. package/src/utils.ts +164 -66
  48. package/CHANGELOG.md +0 -121
  49. package/dist/index.browser.js +0 -2
  50. package/dist/index.browser.js.flow +0 -4
  51. package/dist/index.browser.js.map +0 -1
  52. package/dist/index.js +0 -411
  53. package/dist/index.js.flow +0 -4
  54. package/dist/index.js.map +0 -1
  55. package/dist/index.module.d.ts +0 -301
  56. package/dist/index.module.js +0 -393
  57. package/dist/index.module.js.flow +0 -4
  58. package/dist/index.module.js.map +0 -1
  59. package/dist/index.umd.d.ts +0 -301
  60. package/dist/index.umd.js +0 -2
  61. package/dist/index.umd.js.flow +0 -4
  62. package/dist/index.umd.js.map +0 -1
  63. package/lib/index.js.flow +0 -4
  64. package/lib/index.js.map +0 -1
  65. package/mod.ts +0 -2
  66. package/src/ditox.js.flow +0 -131
  67. package/src/index.js.flow +0 -34
  68. package/src/modules.js.flow +0 -156
  69. package/src/utils.js.flow +0 -113
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.declareModuleBindings = exports.declareModule = exports.bindModules = exports.bindModule = 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 modules_1 = require("./modules");
19
+ Object.defineProperty(exports, "bindModule", { enumerable: true, get: function () { return modules_1.bindModule; } });
20
+ Object.defineProperty(exports, "bindModules", { enumerable: true, get: function () { return modules_1.bindModules; } });
21
+ Object.defineProperty(exports, "declareModule", { enumerable: true, get: function () { return modules_1.declareModule; } });
22
+ Object.defineProperty(exports, "declareModuleBindings", { enumerable: true, get: function () { return modules_1.declareModuleBindings; } });
23
+ //# 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,qCAKmB;AAJjB,qGAAA,UAAU,OAAA;AACV,sGAAA,WAAW,OAAA;AACX,wGAAA,aAAa,OAAA;AACb,gHAAA,qBAAqB,OAAA"}
@@ -0,0 +1,118 @@
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
+ /** Callback could be used to prepare an environment. It is called before binding the module. */
52
+ beforeBinding?: (container: Container) => void;
53
+ /** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
54
+ afterBinding?: (container: Container) => void;
55
+ };
56
+ /**
57
+ * Options for module binding.
58
+ *
59
+ * `scope` types:
60
+ * - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
61
+ * - `scoped` - The module is created and cached by the container which starts resolving.
62
+ */
63
+ export declare type BindModuleOptions = {
64
+ scope?: 'scoped' | 'singleton';
65
+ };
66
+ export declare type ModuleBindingEntry = ModuleDeclaration<AnyObject> | {
67
+ module: ModuleDeclaration<AnyObject>;
68
+ options: BindModuleOptions;
69
+ };
70
+ /**
71
+ * Binds the dependency module to the container
72
+ * @param container - Dependency container.
73
+ * @param moduleDeclaration - Declaration of the dependency module.
74
+ * @param options - Options for module binding.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * bindModule(container, LOGGER_MODULE);
79
+ * ```
80
+ */
81
+ export declare function bindModule<T extends Module<AnyObject>>(container: Container, moduleDeclaration: ModuleDeclaration<T>, options?: BindModuleOptions): void;
82
+ /**
83
+ * Binds dependency modules to the container
84
+ *
85
+ * @param container - Dependency container for binding
86
+ * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
87
+ */
88
+ export declare function bindModules(container: Container, modules: ReadonlyArray<ModuleBindingEntry>): void;
89
+ /**
90
+ * Declares a module binding
91
+ *
92
+ * @param declaration - a module declaration
93
+ * @param declaration.token - optional field
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * const LOGGER_MODULE = declareModule<LoggerModule>({
98
+ * factory: (container) => {
99
+ * const transport = container.resolve(TRANSPORT_TOKEN).open();
100
+ * return {
101
+ * logger: { log: (message) => transport.write(message) },
102
+ * destroy: () => transport.close(),
103
+ * }
104
+ * },
105
+ * exports: {
106
+ * logger: LOGGER_TOKEN,
107
+ * },
108
+ * });
109
+ * ```
110
+ */
111
+ export declare function declareModule<T>(declaration: Omit<ModuleDeclaration<T>, 'token'> & Partial<Pick<ModuleDeclaration<T>, 'token'>>): ModuleDeclaration<T>;
112
+ /**
113
+ * Declares bindings of several modules
114
+ *
115
+ * @param modules - module declaration entries
116
+ */
117
+ export declare function declareModuleBindings(modules: ReadonlyArray<ModuleBindingEntry>): ModuleDeclaration<Module>;
118
+ export {};
@@ -0,0 +1,109 @@
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
+ const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
19
+ const exports = moduleDeclaration.exports;
20
+ const scope = options === null || options === void 0 ? void 0 : options.scope;
21
+ if (beforeBinding) {
22
+ beforeBinding(container);
23
+ }
24
+ if (imports) {
25
+ bindModules(container, imports);
26
+ }
27
+ const exportedValueTokens = new Set();
28
+ if (exports) {
29
+ const keys = Object.keys(exports);
30
+ keys.forEach((valueKey) => {
31
+ const valueToken = exports[valueKey];
32
+ if (valueToken) {
33
+ exportedValueTokens.add(valueToken);
34
+ container.bindFactory(valueToken, (0, utils_1.injectable)((module) => module[valueKey], token), { scope });
35
+ }
36
+ });
37
+ }
38
+ container.bindFactory(token, factory, {
39
+ scope,
40
+ onRemoved: (module) => {
41
+ if (module.destroy) {
42
+ module.destroy();
43
+ }
44
+ exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
45
+ exportedValueTokens.clear();
46
+ },
47
+ });
48
+ if (afterBinding) {
49
+ afterBinding(container);
50
+ }
51
+ }
52
+ exports.bindModule = bindModule;
53
+ /**
54
+ * Binds dependency modules to the container
55
+ *
56
+ * @param container - Dependency container for binding
57
+ * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
58
+ */
59
+ function bindModules(container, modules) {
60
+ modules.forEach((entry) => {
61
+ if ('module' in entry) {
62
+ bindModule(container, entry.module, entry.options);
63
+ }
64
+ else {
65
+ bindModule(container, entry);
66
+ }
67
+ });
68
+ }
69
+ exports.bindModules = bindModules;
70
+ /**
71
+ * Declares a module binding
72
+ *
73
+ * @param declaration - a module declaration
74
+ * @param declaration.token - optional field
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const LOGGER_MODULE = declareModule<LoggerModule>({
79
+ * factory: (container) => {
80
+ * const transport = container.resolve(TRANSPORT_TOKEN).open();
81
+ * return {
82
+ * logger: { log: (message) => transport.write(message) },
83
+ * destroy: () => transport.close(),
84
+ * }
85
+ * },
86
+ * exports: {
87
+ * logger: LOGGER_TOKEN,
88
+ * },
89
+ * });
90
+ * ```
91
+ */
92
+ function declareModule(declaration) {
93
+ var _a;
94
+ return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : (0, ditox_1.token)() };
95
+ }
96
+ exports.declareModule = declareModule;
97
+ /**
98
+ * Declares bindings of several modules
99
+ *
100
+ * @param modules - module declaration entries
101
+ */
102
+ function declareModuleBindings(modules) {
103
+ return declareModule({
104
+ factory: () => ({}),
105
+ imports: modules,
106
+ });
107
+ }
108
+ exports.declareModuleBindings = declareModuleBindings;
109
+ //# sourceMappingURL=modules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":";;;AAAA,mCAAgD;AAChD,mCAAmC;AAqFnC;;;;;;;;;;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,iBAAiB,CAAC,OAAO,CAAC;IAE1C,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 {};
@@ -0,0 +1,156 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
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;
15
+ /**
16
+ * Rebinds the array by the token with added new value.
17
+ * @param container - Dependency container.
18
+ * @param token - Token for an array of values.
19
+ * @param value - New value which is added to the end of the array.
20
+ */
21
+ function bindMultiValue(container, token, value) {
22
+ var _a;
23
+ const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
24
+ const nextValues = [...prevValues, value];
25
+ container.bindValue(token, nextValues);
26
+ }
27
+ exports.bindMultiValue = bindMultiValue;
28
+ /**
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
+
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
+ * ```
44
+ */
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;
52
+ }
53
+ exports.tryResolveValue = tryResolveValue;
54
+ /**
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
+ * ```
70
+ */
71
+ function tryResolveValues(container, ...tokens) {
72
+ return tokens.map((item) => tryResolveValue(container, item));
73
+ }
74
+ exports.tryResolveValues = tryResolveValues;
75
+ /**
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.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const value = resolveValue(container, tokenA);
86
+ * console.log(value); // 1
87
+ *
88
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
89
+ * console.log(props); // {a: 1, b: 2}
90
+ * ```
91
+ */
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])));
98
+ return obj;
99
+ }
100
+ exports.resolveValue = resolveValue;
101
+ /**
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
+
107
+ * If a token is not found, then `ResolverError` is thrown.
108
+ *
109
+ * @example
110
+ * ```ts
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}]
116
+ * ```
117
+ */
118
+ function resolveValues(container, ...tokens) {
119
+ return tokens.map((item) => resolveValue(container, item));
120
+ }
121
+ exports.resolveValues = resolveValues;
122
+ /**
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
+ *
128
+ * @param factory - A factory.
129
+ * @param tokens - Tokens which correspond to factory arguments.
130
+ *
131
+ * @return Decorated factory which takes a dependency container as a single argument.
132
+ */
133
+ function injectable(factory, ...tokens) {
134
+ return (container) => {
135
+ const values = resolveValues(container, ...tokens);
136
+ return factory.apply(this, values);
137
+ };
138
+ }
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;
156
+ //# sourceMappingURL=utils.js.map