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.
Files changed (71) hide show
  1. package/README.md +7 -6
  2. package/dist/{index.d.ts → browser/index.d.ts} +133 -13
  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 +6 -0
  9. package/dist/cjs/index.js +28 -0
  10. package/dist/cjs/index.js.map +1 -0
  11. package/dist/cjs/modules.d.ts +124 -0
  12. package/dist/cjs/modules.js +110 -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 +6 -0
  24. package/dist/esm/index.js +5 -0
  25. package/dist/esm/index.js.map +1 -0
  26. package/dist/esm/modules.d.ts +124 -0
  27. package/dist/esm/modules.js +103 -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 +133 -13
  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 +364 -98
  40. package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
  41. package/package.json +16 -17
  42. package/src/ditox.test.ts +10 -1
  43. package/src/index.ts +18 -11
  44. package/src/modules.test.ts +32 -0
  45. package/src/modules.ts +4 -4
  46. package/src/utils-deprecated.test.ts +286 -0
  47. package/src/utils-deprecated.ts +87 -0
  48. package/src/utils.test.ts +173 -58
  49. package/src/utils.ts +164 -66
  50. package/CHANGELOG.md +0 -110
  51. package/dist/index.browser.js +0 -2
  52. package/dist/index.browser.js.flow +0 -4
  53. package/dist/index.browser.js.map +0 -1
  54. package/dist/index.js +0 -411
  55. package/dist/index.js.flow +0 -4
  56. package/dist/index.js.map +0 -1
  57. package/dist/index.module.d.ts +0 -301
  58. package/dist/index.module.js +0 -393
  59. package/dist/index.module.js.flow +0 -4
  60. package/dist/index.module.js.map +0 -1
  61. package/dist/index.umd.d.ts +0 -301
  62. package/dist/index.umd.js +0 -2
  63. package/dist/index.umd.js.flow +0 -4
  64. package/dist/index.umd.js.map +0 -1
  65. package/lib/index.js.flow +0 -4
  66. package/lib/index.js.map +0 -1
  67. package/mod.ts +0 -2
  68. package/src/ditox.js.flow +0 -131
  69. package/src/index.js.flow +0 -34
  70. package/src/modules.js.flow +0 -156
  71. package/src/utils.js.flow +0 -113
@@ -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,103 @@
1
+ import { token } from './ditox';
2
+ import { injectable } from './utils';
3
+ /**
4
+ * Binds the dependency module to the container
5
+ * @param container - Dependency container.
6
+ * @param moduleDeclaration - Declaration of the dependency module.
7
+ * @param options - Options for module binding.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * bindModule(container, LOGGER_MODULE);
12
+ * ```
13
+ */
14
+ export function bindModule(container, moduleDeclaration, options) {
15
+ var _a;
16
+ const { token, imports, factory, beforeBinding, afterBinding } = moduleDeclaration;
17
+ const exports = (_a = moduleDeclaration.exports) !== null && _a !== void 0 ? _a : moduleDeclaration.exportedProps;
18
+ const scope = options === null || options === void 0 ? void 0 : options.scope;
19
+ if (beforeBinding) {
20
+ beforeBinding(container);
21
+ }
22
+ if (imports) {
23
+ bindModules(container, imports);
24
+ }
25
+ const exportedValueTokens = new Set();
26
+ if (exports) {
27
+ const keys = Object.keys(exports);
28
+ keys.forEach((valueKey) => {
29
+ const valueToken = exports[valueKey];
30
+ if (valueToken) {
31
+ exportedValueTokens.add(valueToken);
32
+ container.bindFactory(valueToken, injectable((module) => module[valueKey], token), { scope });
33
+ }
34
+ });
35
+ }
36
+ container.bindFactory(token, factory, {
37
+ scope,
38
+ onRemoved: (module) => {
39
+ if (module.destroy) {
40
+ module.destroy();
41
+ }
42
+ exportedValueTokens.forEach((valueToken) => container.remove(valueToken));
43
+ exportedValueTokens.clear();
44
+ },
45
+ });
46
+ if (afterBinding) {
47
+ afterBinding(container);
48
+ }
49
+ }
50
+ /**
51
+ * Binds dependency modules to the container
52
+ *
53
+ * @param container - Dependency container for binding
54
+ * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
55
+ */
56
+ export function bindModules(container, modules) {
57
+ modules.forEach((entry) => {
58
+ if ('module' in entry) {
59
+ bindModule(container, entry.module, entry.options);
60
+ }
61
+ else {
62
+ bindModule(container, entry);
63
+ }
64
+ });
65
+ }
66
+ /**
67
+ * Declares a module binding
68
+ *
69
+ * @param declaration - a module declaration
70
+ * @param declaration.token - optional field
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * const LOGGER_MODULE = declareModule<LoggerModule>({
75
+ * factory: (container) => {
76
+ * const transport = container.resolve(TRANSPORT_TOKEN).open();
77
+ * return {
78
+ * logger: { log: (message) => transport.write(message) },
79
+ * destroy: () => transport.close(),
80
+ * }
81
+ * },
82
+ * exports: {
83
+ * logger: LOGGER_TOKEN,
84
+ * },
85
+ * });
86
+ * ```
87
+ */
88
+ export function declareModule(declaration) {
89
+ var _a;
90
+ return { ...declaration, token: (_a = declaration.token) !== null && _a !== void 0 ? _a : token() };
91
+ }
92
+ /**
93
+ * Declares bindings of several modules
94
+ *
95
+ * @param modules - module declaration entries
96
+ */
97
+ export function declareModuleBindings(modules) {
98
+ return declareModule({
99
+ factory: () => ({}),
100
+ imports: modules,
101
+ });
102
+ }
103
+ //# sourceMappingURL=modules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modules.js","sourceRoot":"","sources":["../../src/modules.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,KAAK,EAAQ,MAAM,SAAS,CAAC;AAChD,OAAO,EAAC,UAAU,EAAC,MAAM,SAAS,CAAC;AA4FnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,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,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aAAa,CAAC;QACnB,OAAO,EAAE,GAAG,EAAE,CAAC,CAAC,EAAE,CAAC;QACnB,OAAO,EAAE,OAAO;KACjB,CAAC,CAAC;AACL,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"}
@@ -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,145 @@
1
+ /**
2
+ * Checks if a value is the token
3
+ */
4
+ export function isToken(value) {
5
+ return (value !== undefined &&
6
+ value !== null &&
7
+ typeof value === 'object' &&
8
+ 'symbol' in value &&
9
+ typeof value.symbol === 'symbol');
10
+ }
11
+ /**
12
+ * Rebinds the array by the token with added new value.
13
+ * @param container - Dependency container.
14
+ * @param token - Token for an array of values.
15
+ * @param value - New value which is added to the end of the array.
16
+ */
17
+ export function bindMultiValue(container, token, value) {
18
+ var _a;
19
+ const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
20
+ const nextValues = [...prevValues, value];
21
+ container.bindValue(token, nextValues);
22
+ }
23
+ /**
24
+ * Tries to resolve a value by the provided token.
25
+ *
26
+ * If an argument is an object which has tokens as its properties,
27
+ * then returns an object containing resolved values as properties.
28
+
29
+ * If a token is not found, then `undefined` value is used.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const value = tryResolveValue(container, tokenA);
34
+ * console.log(value); // 1
35
+ *
36
+ * const props = tryResolveValue(container, {a: tokenA, b: tokenB});
37
+ * console.log(props); // {a: 1, b: 2}
38
+ * ```
39
+ */
40
+ export function tryResolveValue(container, token) {
41
+ if (isToken(token)) {
42
+ return container.get(token);
43
+ }
44
+ const obj = {};
45
+ Object.keys(token).forEach((key) => (obj[key] = container.get(token[key])));
46
+ return obj;
47
+ }
48
+ /**
49
+ * Returns an array of resolved values or objects with resolved values.
50
+ *
51
+ * If an item of the array is an object which has tokens as its properties,
52
+ * then returns an object containing resolved values as properties.
53
+
54
+ * If a token is not found, then `undefined` value is used.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * const items1 = tryResolveValues(container, tokenA);
59
+ * console.log(items1); // [1]
60
+ *
61
+ * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB});
62
+ * console.log(items2); // [1, {a: 1, b: 2}]
63
+ * ```
64
+ */
65
+ export function tryResolveValues(container, ...tokens) {
66
+ return tokens.map((item) => tryResolveValue(container, item));
67
+ }
68
+ /**
69
+ * Resolves a value by the provided token.
70
+ *
71
+ * If an argument is an object which has tokens as its properties,
72
+ * then returns an object containing resolved values as properties.
73
+
74
+ * If a value is not found by the token, then `ResolverError` is thrown.
75
+ *
76
+ * @example
77
+ * ```ts
78
+ * const value = resolveValue(container, tokenA);
79
+ * console.log(value); // 1
80
+ *
81
+ * const props = resolveValue(container, {a: tokenA, b: tokenB});
82
+ * console.log(props); // {a: 1, b: 2}
83
+ * ```
84
+ */
85
+ export function resolveValue(container, token) {
86
+ if (isToken(token)) {
87
+ return container.resolve(token);
88
+ }
89
+ const obj = {};
90
+ Object.keys(token).forEach((key) => (obj[key] = container.resolve(token[key])));
91
+ return obj;
92
+ }
93
+ /**
94
+ * Returns an array of resolved values or objects with resolved values.
95
+ *
96
+ * If an item of the array is an object which has tokens as its properties,
97
+ * then returns an object containing resolved values as properties.
98
+
99
+ * If a token is not found, then `ResolverError` is thrown.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * const items1 = resolveValues(container, tokenA);
104
+ * console.log(items1); // [1]
105
+ *
106
+ * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB});
107
+ * console.log(items2); // [1, {a: 1, b: 2}]
108
+ * ```
109
+ */
110
+ export function resolveValues(container, ...tokens) {
111
+ return tokens.map((item) => resolveValue(container, item));
112
+ }
113
+ /**
114
+ * Decorates a factory by passing resolved values as factory arguments.
115
+ *
116
+ * If an argument is an object which has tokens as its properties,
117
+ * then returns an object containing resolved values as properties.
118
+ *
119
+ * @param factory - A factory.
120
+ * @param tokens - Tokens which correspond to factory arguments.
121
+ *
122
+ * @return Decorated factory which takes a dependency container as a single argument.
123
+ */
124
+ export function injectable(factory, ...tokens) {
125
+ return (container) => {
126
+ const values = resolveValues(container, ...tokens);
127
+ return factory.apply(this, values);
128
+ };
129
+ }
130
+ /**
131
+ * Decorates a class by passing resolved values as arguments to its constructor.
132
+ *
133
+ * If an argument is an object which has tokens as its properties,
134
+ * then returns an object containing resolved values as properties.
135
+ *
136
+ * @param constructor - Constructor of a class
137
+ * @param tokens - Tokens which correspond to constructor arguments
138
+ *
139
+ * @return A factory function which takes a dependency container as a single argument
140
+ * and returns a new created class.
141
+ */
142
+ export function injectableClass(constructor, ...tokens) {
143
+ return injectable((...values) => new constructor(...values), ...tokens);
144
+ }
145
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAOA;;GAEG;AACH,MAAM,UAAU,OAAO,CAAI,KAAc;IACvC,OAAO,CACL,KAAK,KAAK,SAAS;QACnB,KAAK,KAAK,IAAI;QACd,OAAO,KAAK,KAAK,QAAQ;QACzB,QAAQ,IAAI,KAAK;QACjB,OAAQ,KAAa,CAAC,MAAM,KAAK,QAAQ,CAC1C,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,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,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,UAAU,CAAC,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