ditox 1.4.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -5
- package/dist/{index.d.ts → browser/index.d.ts} +98 -42
- package/dist/browser/index.js +2 -0
- package/dist/browser/index.js.map +1 -0
- package/dist/cjs/ditox.d.ts +125 -0
- package/dist/cjs/ditox.js +210 -0
- package/dist/cjs/ditox.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.js +23 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/modules.d.ts +118 -0
- package/dist/cjs/modules.js +109 -0
- package/dist/cjs/modules.js.map +1 -0
- package/dist/cjs/utils-deprecated.d.ts +59 -0
- package/dist/cjs/utils-deprecated.js +66 -0
- package/dist/cjs/utils-deprecated.js.map +1 -0
- package/dist/cjs/utils.d.ts +136 -0
- package/dist/cjs/utils.js +156 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/ditox.d.ts +125 -0
- package/{lib/index.js → dist/esm/ditox.js} +10 -201
- package/dist/esm/ditox.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/modules.d.ts +118 -0
- package/dist/esm/modules.js +102 -0
- package/dist/esm/modules.js.map +1 -0
- package/dist/esm/utils-deprecated.d.ts +59 -0
- package/dist/esm/utils-deprecated.js +59 -0
- package/dist/esm/utils-deprecated.js.map +1 -0
- package/dist/esm/utils.d.ts +136 -0
- package/dist/esm/utils.js +145 -0
- package/dist/esm/utils.js.map +1 -0
- package/{lib → dist/umd}/index.d.ts +98 -42
- package/dist/umd/index.js +2 -0
- package/dist/umd/index.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/README.md +250 -164
- package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
- package/package.json +15 -16
- package/src/ditox.test.ts +10 -1
- package/src/index.ts +13 -13
- package/src/modules.test.ts +2 -0
- package/src/modules.ts +1 -8
- package/src/utils.test.ts +173 -58
- package/src/utils.ts +164 -66
- package/CHANGELOG.md +0 -121
- package/dist/index.browser.js +0 -2
- package/dist/index.browser.js.flow +0 -4
- package/dist/index.browser.js.map +0 -1
- package/dist/index.js +0 -411
- package/dist/index.js.flow +0 -4
- package/dist/index.js.map +0 -1
- package/dist/index.module.d.ts +0 -301
- package/dist/index.module.js +0 -393
- package/dist/index.module.js.flow +0 -4
- package/dist/index.module.js.map +0 -1
- package/dist/index.umd.d.ts +0 -301
- package/dist/index.umd.js +0 -2
- package/dist/index.umd.js.flow +0 -4
- package/dist/index.umd.js.map +0 -1
- package/lib/index.js.flow +0 -4
- package/lib/index.js.map +0 -1
- package/mod.ts +0 -2
- package/src/ditox.js.flow +0 -131
- package/src/index.js.flow +0 -34
- package/src/modules.js.flow +0 -156
- package/src/utils.js.flow +0 -113
package/src/modules.js.flow
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type {Container, Token} from './ditox';
|
|
4
|
-
import {token} from './ditox';
|
|
5
|
-
import type {TokenProps, ValueProps} from './utils';
|
|
6
|
-
|
|
7
|
-
type ModuleController = {
|
|
8
|
-
/** Dispose the module and clean its resources */
|
|
9
|
-
destroy?: () => void,
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Dependency module
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```ts
|
|
17
|
-
* type LoggerModule = Module<{
|
|
18
|
-
* logger: Logger;
|
|
19
|
-
* }>;
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
export type Module<ModuleProps: ValueProps = {}> = {
|
|
23
|
-
...ModuleController,
|
|
24
|
-
...ModuleProps,
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Description of a dependency module in declarative way.
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```ts
|
|
32
|
-
* const LOGGER_MODULE: ModuleDeclaration<LoggerModule> = {
|
|
33
|
-
* token: LOGGER_MODULE_TOKEN,
|
|
34
|
-
* factory: (container) => {
|
|
35
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
36
|
-
* return {
|
|
37
|
-
* logger: { log: (message) => transport.write(message) },
|
|
38
|
-
* destroy: () => transport.close(),
|
|
39
|
-
* }
|
|
40
|
-
* },
|
|
41
|
-
* exports: {
|
|
42
|
-
* logger: LOGGER_TOKEN,
|
|
43
|
-
* },
|
|
44
|
-
* };
|
|
45
|
-
* ```
|
|
46
|
-
*/
|
|
47
|
-
export type ModuleDeclaration<T: Module<*>> = {
|
|
48
|
-
/** Token for the module */
|
|
49
|
-
token: Token<T>,
|
|
50
|
-
|
|
51
|
-
/** Modules for binding */
|
|
52
|
-
imports?: $ReadOnlyArray<ModuleBindingEntry>;
|
|
53
|
-
|
|
54
|
-
/** Factory of the module */
|
|
55
|
-
factory: (container: Container) => T,
|
|
56
|
-
|
|
57
|
-
/** Dictionary of module properties which are bound to tokens. */
|
|
58
|
-
exports?: $Shape<TokenProps<T>>,
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Dictionary of module properties which are bound to tokens.
|
|
62
|
-
*
|
|
63
|
-
* @deprecated Use `exports` property
|
|
64
|
-
*/
|
|
65
|
-
exportedProps?: $Shape<TokenProps<T>>,
|
|
66
|
-
|
|
67
|
-
/** Callback could be used to prepare an environment. It is called before binding the module. */
|
|
68
|
-
beforeBinding?: (container: Container) => void,
|
|
69
|
-
|
|
70
|
-
/** Callback could be used to export complex dependencies from the module. It is called after binding the module. */
|
|
71
|
-
afterBinding?: (container: Container) => void,
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Options for module binding.
|
|
76
|
-
*
|
|
77
|
-
* `scope` types:
|
|
78
|
-
* - `singleton` - **This is the default**. The module is created and cached by the container which registered the factory.
|
|
79
|
-
* - `scoped` - The module is created and cached by the container which starts resolving.
|
|
80
|
-
*/
|
|
81
|
-
export type BindModuleOptions = {
|
|
82
|
-
scope?: 'scoped' | 'singleton',
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Binds the dependency module to the container
|
|
87
|
-
* @param container - Dependency container.
|
|
88
|
-
* @param moduleDeclaration - Declaration of the dependency module.
|
|
89
|
-
* @param options - Options for module binding.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```ts
|
|
93
|
-
* bindModule(container, LOGGER_MODULE);
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
declare export function bindModule<T: Module<*>>(
|
|
97
|
-
container: Container,
|
|
98
|
-
moduleDeclaration: ModuleDeclaration<T>,
|
|
99
|
-
options?: BindModuleOptions,
|
|
100
|
-
): void;
|
|
101
|
-
|
|
102
|
-
export type ModuleBindingEntry =
|
|
103
|
-
| ModuleDeclaration<*>
|
|
104
|
-
| {
|
|
105
|
-
module: ModuleDeclaration<*>,
|
|
106
|
-
options: BindModuleOptions,
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Binds dependency modules to the container
|
|
111
|
-
*
|
|
112
|
-
* @param container - Dependency container for binding
|
|
113
|
-
* @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects.
|
|
114
|
-
*/
|
|
115
|
-
declare export function bindModules(
|
|
116
|
-
container: Container,
|
|
117
|
-
modules: $ReadOnlyArray<ModuleBindingEntry>,
|
|
118
|
-
): void;
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Declares a module binding
|
|
122
|
-
*
|
|
123
|
-
* @param declaration - a module declaration
|
|
124
|
-
* @param declaration.token - optional field
|
|
125
|
-
*
|
|
126
|
-
* @example
|
|
127
|
-
* ```ts
|
|
128
|
-
* const LOGGER_MODULE = declareModule<LoggerModule>({
|
|
129
|
-
* factory: (container) => {
|
|
130
|
-
* const transport = container.resolve(TRANSPORT_TOKEN).open();
|
|
131
|
-
* return {
|
|
132
|
-
* logger: { log: (message) => transport.write(message) },
|
|
133
|
-
* destroy: () => transport.close(),
|
|
134
|
-
* }
|
|
135
|
-
* },
|
|
136
|
-
* exportedProps: {
|
|
137
|
-
* logger: LOGGER_TOKEN,
|
|
138
|
-
* },
|
|
139
|
-
* });
|
|
140
|
-
* ```
|
|
141
|
-
*/
|
|
142
|
-
declare export function declareModule<T>(
|
|
143
|
-
declaration: ModuleDeclaration<T> | {
|
|
144
|
-
...$Rest<ModuleDeclaration<T>, {token: *}>,
|
|
145
|
-
token?: $PropertyType<ModuleDeclaration<T>, 'token'>,
|
|
146
|
-
},
|
|
147
|
-
): ModuleDeclaration<T>;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Declares bindings of several modules
|
|
151
|
-
*
|
|
152
|
-
* @param modules - module declaration entries
|
|
153
|
-
*/
|
|
154
|
-
declare export function declareModuleBindings(
|
|
155
|
-
modules: $ReadOnlyArray<ModuleBindingEntry>,
|
|
156
|
-
): ModuleDeclaration<Module<*>>;
|
package/src/utils.js.flow
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import type {Container, Token} from './ditox';
|
|
4
|
-
|
|
5
|
-
type ValuesTuple =
|
|
6
|
-
| [*]
|
|
7
|
-
| [*, *]
|
|
8
|
-
| [*, *, *]
|
|
9
|
-
| [*, *, *, *]
|
|
10
|
-
| [*, *, *, *, *]
|
|
11
|
-
| [*, *, *, *, *, *]
|
|
12
|
-
| [*, *, *, *, *, *, *]
|
|
13
|
-
| [*, *, *, *, *, *, *, *]
|
|
14
|
-
| [*, *, *, *, *, *, *, *, *]
|
|
15
|
-
| [*, *, *, *, *, *, *, *, *, *]
|
|
16
|
-
| [*, *, *, *, *, *, *, *, *, *, *]
|
|
17
|
-
| [*, *, *, *, *, *, *, *, *, *, *, *];
|
|
18
|
-
|
|
19
|
-
type Tokens<Values: ValuesTuple> = $TupleMap<Values, <V>() => Token<V>>;
|
|
20
|
-
type OptionalValues<Values: ValuesTuple> = $TupleMap<Values, <V>() => V | void>;
|
|
21
|
-
|
|
22
|
-
export type mapPropToToken = <K, V>(key: K, value: V) => Token<V>;
|
|
23
|
-
export type ValueProps = {[key: string]: *};
|
|
24
|
-
export type TokenProps<Values: ValueProps> = $ObjMapi<Values, mapPropToToken>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Rebinds the array by the token with added new value.
|
|
28
|
-
* @param container - Dependency container.
|
|
29
|
-
* @param token - Token for an array of values.
|
|
30
|
-
* @param value - New value which is added to the end of the array.
|
|
31
|
-
*/
|
|
32
|
-
declare export function bindMultiValue<T>(
|
|
33
|
-
container: Container,
|
|
34
|
-
token: Token<Array<T>>,
|
|
35
|
-
value: T,
|
|
36
|
-
): void;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Returns an array of resolved values by the specified token.
|
|
40
|
-
* If a token is not found, then `undefined` value is used.
|
|
41
|
-
*/
|
|
42
|
-
declare export function getValues<Values: ValuesTuple>(
|
|
43
|
-
container: Container,
|
|
44
|
-
...tokens: Tokens<Values>
|
|
45
|
-
): OptionalValues<Values>;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Returns an array of resolved values by the specified token.
|
|
49
|
-
* If a token is not found, then `ResolverError` is thrown.
|
|
50
|
-
*/
|
|
51
|
-
declare export function resolveValues<Values: ValuesTuple>(
|
|
52
|
-
container: Container,
|
|
53
|
-
...tokens: Tokens<Values>
|
|
54
|
-
): Values;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Decorates a factory by passing resolved tokens as factory arguments.
|
|
58
|
-
* @param factory - A factory.
|
|
59
|
-
* @param tokens - Tokens which correspond to factory arguments.
|
|
60
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
61
|
-
*/
|
|
62
|
-
declare export function injectable<Values: ValuesTuple, Result>(
|
|
63
|
-
factory: (...params: Values) => Result,
|
|
64
|
-
...tokens: Tokens<Values>
|
|
65
|
-
): (container: Container) => Result;
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Returns an object with resolved properties which are specified by token properties.
|
|
69
|
-
* If a token is not found, then `undefined` value is used.
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* const props = getProps(container, {a: tokenA, b: tokenB});
|
|
74
|
-
* console.log(props); // {a: 1, b: 2}
|
|
75
|
-
* ```
|
|
76
|
-
*/
|
|
77
|
-
declare export function getProps<Props: ValueProps>(
|
|
78
|
-
container: Container,
|
|
79
|
-
tokens: TokenProps<Props>,
|
|
80
|
-
): $Shape<Props>;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Returns an object with resolved properties which are specified by token properties.
|
|
84
|
-
* If a token is not found, then `ResolverError` is thrown.
|
|
85
|
-
*
|
|
86
|
-
* @example
|
|
87
|
-
* ```ts
|
|
88
|
-
* const props = resolveProps(container, {a: tokenA, b: tokenB});
|
|
89
|
-
* console.log(props); // {a: 1, b: 2}
|
|
90
|
-
* ```
|
|
91
|
-
*/
|
|
92
|
-
declare export function resolveProps<Props: ValueProps>(
|
|
93
|
-
container: Container,
|
|
94
|
-
tokens: TokenProps<Props>,
|
|
95
|
-
): Props;
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Decorates a factory by passing a resolved object with tokens as the first argument.
|
|
99
|
-
* @param factory - A factory.
|
|
100
|
-
* @param tokens - Object with tokens.
|
|
101
|
-
* @return Decorated factory which takes a dependency container as a single argument.
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* ```ts
|
|
105
|
-
* const factory = ({a, b}: {a: number, b: number}) => a + b;
|
|
106
|
-
* const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
|
|
107
|
-
* const result = decoratedFactory(container);
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
declare export function injectableProps<Props: ValueProps, Result>(
|
|
111
|
-
factory: (props: Props) => Result,
|
|
112
|
-
tokens: TokenProps<Props>,
|
|
113
|
-
): (container: Container) => Result;
|