ditox 1.4.2 → 2.0.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 (59) hide show
  1. package/README.md +1 -1
  2. package/dist/{index.d.ts → browser/index.d.ts} +0 -0
  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 +22 -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/{src/utils.js.flow → dist/cjs/utils.d.ts} +21 -60
  15. package/dist/cjs/utils.js +98 -0
  16. package/dist/cjs/utils.js.map +1 -0
  17. package/dist/esm/ditox.d.ts +125 -0
  18. package/{lib/index.js → dist/esm/ditox.js} +10 -201
  19. package/dist/esm/ditox.js.map +1 -0
  20. package/dist/esm/index.d.ts +5 -0
  21. package/dist/esm/index.js +4 -0
  22. package/dist/esm/index.js.map +1 -0
  23. package/dist/esm/modules.d.ts +124 -0
  24. package/dist/esm/modules.js +103 -0
  25. package/dist/esm/modules.js.map +1 -0
  26. package/dist/esm/utils.d.ts +74 -0
  27. package/dist/esm/utils.js +88 -0
  28. package/dist/esm/utils.js.map +1 -0
  29. package/{lib → dist/umd}/index.d.ts +0 -0
  30. package/dist/umd/index.js +2 -0
  31. package/dist/umd/index.js.map +1 -0
  32. package/docs/.nojekyll +1 -0
  33. package/docs/README.md +111 -96
  34. package/docs/classes/{resolvererror.md → ResolverError.md} +14 -12
  35. package/package.json +15 -16
  36. package/src/ditox.test.ts +10 -1
  37. package/src/index.ts +8 -9
  38. package/src/modules.test.ts +2 -0
  39. package/CHANGELOG.md +0 -132
  40. package/dist/index.browser.js +0 -2
  41. package/dist/index.browser.js.flow +0 -4
  42. package/dist/index.browser.js.map +0 -1
  43. package/dist/index.js +0 -411
  44. package/dist/index.js.flow +0 -4
  45. package/dist/index.js.map +0 -1
  46. package/dist/index.module.d.ts +0 -301
  47. package/dist/index.module.js +0 -393
  48. package/dist/index.module.js.flow +0 -4
  49. package/dist/index.module.js.map +0 -1
  50. package/dist/index.umd.d.ts +0 -301
  51. package/dist/index.umd.js +0 -2
  52. package/dist/index.umd.js.flow +0 -4
  53. package/dist/index.umd.js.map +0 -1
  54. package/lib/index.js.flow +0 -4
  55. package/lib/index.js.map +0 -1
  56. package/mod.ts +0 -2
  57. package/src/ditox.js.flow +0 -131
  58. package/src/index.js.flow +0 -34
  59. package/src/modules.js.flow +0 -156
@@ -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
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*]
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
-
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
+ };
26
8
  /**
27
9
  * Rebinds the array by the token with added new value.
28
10
  * @param container - Dependency container.
29
11
  * @param token - Token for an array of values.
30
12
  * @param value - New value which is added to the end of the array.
31
13
  */
32
- declare export function bindMultiValue<T>(
33
- container: Container,
34
- token: Token<$ReadOnlyArray<T>>,
35
- value: T,
36
- ): void;
37
-
14
+ export declare function bindMultiValue<T>(container: Container, token: Token<ReadonlyArray<T>>, value: T): void;
38
15
  /**
39
16
  * Returns an array of resolved values by the specified token.
40
17
  * If a token is not found, then `undefined` value is used.
41
18
  */
42
- declare export function getValues<Values: ValuesTuple>(
43
- container: Container,
44
- ...tokens: Tokens<Values>
45
- ): OptionalValues<Values>;
46
-
19
+ export declare function getValues<Tokens extends Token<unknown>[], Values extends {
20
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
21
+ }>(container: Container, ...tokens: Tokens): Values;
47
22
  /**
48
23
  * Returns an array of resolved values by the specified token.
49
24
  * If a token is not found, then `ResolverError` is thrown.
50
25
  */
51
- declare export function resolveValues<Values: ValuesTuple>(
52
- container: Container,
53
- ...tokens: Tokens<Values>
54
- ): Values;
55
-
26
+ export declare function resolveValues<Tokens extends Token<unknown>[], Values extends {
27
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
28
+ }>(container: Container, ...tokens: Tokens): Values;
56
29
  /**
57
30
  * Decorates a factory by passing resolved tokens as factory arguments.
58
31
  * @param factory - A factory.
59
32
  * @param tokens - Tokens which correspond to factory arguments.
60
33
  * @return Decorated factory which takes a dependency container as a single argument.
61
34
  */
62
- declare export function injectable<Values: ValuesTuple, Result>(
63
- factory: (...params: Values) => Result,
64
- ...tokens: Tokens<Values>
65
- ): (container: Container) => Result;
66
-
35
+ export declare function injectable<Tokens extends Token<unknown>[], Values extends {
36
+ [K in keyof Tokens]: Tokens[K] extends Token<infer V> ? V : never;
37
+ }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result;
67
38
  /**
68
39
  * Returns an object with resolved properties which are specified by token properties.
69
40
  * If a token is not found, then `undefined` value is used.
@@ -74,11 +45,7 @@ declare export function injectable<Values: ValuesTuple, Result>(
74
45
  * console.log(props); // {a: 1, b: 2}
75
46
  * ```
76
47
  */
77
- declare export function getProps<Props: ValueProps>(
78
- container: Container,
79
- tokens: TokenProps<Props>,
80
- ): $Shape<Props>;
81
-
48
+ export declare function getProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Partial<Props>;
82
49
  /**
83
50
  * Returns an object with resolved properties which are specified by token properties.
84
51
  * If a token is not found, then `ResolverError` is thrown.
@@ -89,11 +56,7 @@ declare export function getProps<Props: ValueProps>(
89
56
  * console.log(props); // {a: 1, b: 2}
90
57
  * ```
91
58
  */
92
- declare export function resolveProps<Props: ValueProps>(
93
- container: Container,
94
- tokens: TokenProps<Props>,
95
- ): Props;
96
-
59
+ export declare function resolveProps<Props extends ValuesProps>(container: Container, tokens: TokenProps<Props>): Props;
97
60
  /**
98
61
  * Decorates a factory by passing a resolved object with tokens as the first argument.
99
62
  * @param factory - A factory.
@@ -107,7 +70,5 @@ declare export function resolveProps<Props: ValueProps>(
107
70
  * const result = decoratedFactory(container);
108
71
  * ```
109
72
  */
110
- declare export function injectableProps<Props: ValueProps, Result>(
111
- factory: (props: Props) => Result,
112
- tokens: TokenProps<Props>,
113
- ): (container: Container) => Result;
73
+ export declare function injectableProps<Props extends ValuesProps, Result>(factory: (props: Props) => Result, tokens: TokenProps<Props>): (container: Container) => Result;
74
+ export {};
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.injectableProps = exports.resolveProps = exports.getProps = exports.injectable = exports.resolveValues = exports.getValues = exports.bindMultiValue = void 0;
4
+ /**
5
+ * Rebinds the array by the token with added new value.
6
+ * @param container - Dependency container.
7
+ * @param token - Token for an array of values.
8
+ * @param value - New value which is added to the end of the array.
9
+ */
10
+ function bindMultiValue(container, token, value) {
11
+ var _a;
12
+ const prevValues = (_a = container.get(token)) !== null && _a !== void 0 ? _a : [];
13
+ const nextValues = [...prevValues, value];
14
+ container.bindValue(token, nextValues);
15
+ }
16
+ exports.bindMultiValue = bindMultiValue;
17
+ /**
18
+ * Returns an array of resolved values by the specified token.
19
+ * If a token is not found, then `undefined` value is used.
20
+ */
21
+ function getValues(container, ...tokens) {
22
+ return tokens.map(container.get);
23
+ }
24
+ exports.getValues = getValues;
25
+ /**
26
+ * Returns an array of resolved values by the specified token.
27
+ * If a token is not found, then `ResolverError` is thrown.
28
+ */
29
+ function resolveValues(container, ...tokens) {
30
+ return tokens.map(container.resolve);
31
+ }
32
+ exports.resolveValues = resolveValues;
33
+ /**
34
+ * Decorates a factory by passing resolved tokens as factory arguments.
35
+ * @param factory - A factory.
36
+ * @param tokens - Tokens which correspond to factory arguments.
37
+ * @return Decorated factory which takes a dependency container as a single argument.
38
+ */
39
+ function injectable(factory, ...tokens) {
40
+ return (container) => {
41
+ const values = resolveValues(container, ...tokens);
42
+ return factory.apply(this, values);
43
+ };
44
+ }
45
+ exports.injectable = injectable;
46
+ /**
47
+ * Returns an object with resolved properties which are specified by token properties.
48
+ * If a token is not found, then `undefined` value is used.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const props = getProps(container, {a: tokenA, b: tokenB});
53
+ * console.log(props); // {a: 1, b: 2}
54
+ * ```
55
+ */
56
+ function getProps(container, tokens) {
57
+ const obj = { ...tokens };
58
+ Object.keys(obj).forEach((key) => (obj[key] = container.get(obj[key])));
59
+ return obj;
60
+ }
61
+ exports.getProps = getProps;
62
+ /**
63
+ * Returns an object with resolved properties which are specified by token properties.
64
+ * If a token is not found, then `ResolverError` is thrown.
65
+ *
66
+ * @example
67
+ * ```ts
68
+ * const props = resolveProps(container, {a: tokenA, b: tokenB});
69
+ * console.log(props); // {a: 1, b: 2}
70
+ * ```
71
+ */
72
+ function resolveProps(container, tokens) {
73
+ const obj = { ...tokens };
74
+ Object.keys(obj).forEach((key) => (obj[key] = container.resolve(obj[key])));
75
+ return obj;
76
+ }
77
+ exports.resolveProps = resolveProps;
78
+ /**
79
+ * Decorates a factory by passing a resolved object with tokens as the first argument.
80
+ * @param factory - A factory.
81
+ * @param tokens - Object with tokens.
82
+ * @return Decorated factory which takes a dependency container as a single argument.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const factory = ({a, b}: {a: number, b: number}) => a + b;
87
+ * const decoratedFactory = injectableProps(factory, {a: tokenA, b: tokenB});
88
+ * const result = decoratedFactory(container);
89
+ * ```
90
+ */
91
+ function injectableProps(factory, tokens) {
92
+ return (container) => {
93
+ const values = resolveProps(container, tokens);
94
+ return factory(values);
95
+ };
96
+ }
97
+ exports.injectableProps = injectableProps;
98
+ //# sourceMappingURL=utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":";;;AAOA;;;;;GAKG;AACH,SAAgB,cAAc,CAC5B,SAAoB,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;AAED;;;;;;;;;GASG;AACH,SAAgB,YAAY,CAC1B,SAAoB,EACpB,MAAyB;IAEzB,MAAM,GAAG,GAAQ,EAAC,GAAG,MAAM,EAAC,CAAC;IAC7B,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5E,OAAO,GAAG,CAAC;AACb,CAAC;AAPD,oCAOC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,eAAe,CAC7B,OAAiC,EACjC,MAAyB;IAEzB,OAAO,CAAC,SAAS,EAAE,EAAE;QACnB,MAAM,MAAM,GAAU,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;QACtD,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC,CAAC;AACJ,CAAC;AARD,0CAQC"}
@@ -0,0 +1,125 @@
1
+ /**
2
+ * @ignore
3
+ * Binding token for mandatory value
4
+ */
5
+ export declare type RequiredToken<T> = {
6
+ symbol: symbol;
7
+ type?: T;
8
+ isOptional?: false;
9
+ };
10
+ /**
11
+ * @ignore
12
+ * Binding token for optional value
13
+ */
14
+ export declare type OptionalToken<T> = {
15
+ symbol: symbol;
16
+ type?: T;
17
+ isOptional: true;
18
+ optionalValue: T;
19
+ };
20
+ /**
21
+ * Binding token.
22
+ */
23
+ export declare type Token<T> = RequiredToken<T> | OptionalToken<T>;
24
+ /**
25
+ * Creates a new binding token.
26
+ * @param description - Token description for better error messages.
27
+ */
28
+ export declare function token<T>(description?: string): Token<T>;
29
+ /**
30
+ * Decorate a token with an optional value.
31
+ * This value is be used as default value in case a container does not have registered token.
32
+ * @param token - Existed token.
33
+ * @param optionalValue - Default value for the resolver.
34
+ */
35
+ export declare function optional<T>(token: Token<T>, optionalValue: T): OptionalToken<T>;
36
+ export declare function optional<T>(token: Token<T>): OptionalToken<T | undefined>;
37
+ /**
38
+ * ResolverError is thrown by the resolver when a token is not found in a container.
39
+ */
40
+ export declare class ResolverError extends Error {
41
+ constructor(message: string);
42
+ }
43
+ /**
44
+ * @see https://github.com/mnasyrov/ditox#factory-lifetimes
45
+ */
46
+ export declare type FactoryScope = 'scoped' | 'singleton' | 'transient';
47
+ /**
48
+ * Options for factory binding.
49
+ *
50
+ * `scope` types:
51
+ * - `singleton` - **This is the default**. The value is created and cached by the container which registered the factory.
52
+ * - `scoped` - The value is created and cached by the container which starts resolving.
53
+ * - `transient` - The value is created every time it is resolved.
54
+ *
55
+ * `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container.
56
+ */
57
+ export declare type FactoryOptions<T> = {
58
+ scope?: 'scoped' | 'singleton';
59
+ onRemoved?: (value: T) => void;
60
+ } | {
61
+ scope: 'transient';
62
+ };
63
+ /**
64
+ * Dependency container.
65
+ */
66
+ export declare type Container = {
67
+ /**
68
+ * Binds a value for the token
69
+ */
70
+ bindValue<T>(token: Token<T>, value: T): void;
71
+ /**
72
+ * Binds a factory for the token.
73
+ */
74
+ bindFactory<T>(token: Token<T>, factory: (container: Container) => T, options?: FactoryOptions<T>): void;
75
+ /**
76
+ * Checks if the token is registered in the container hierarchy.
77
+ */
78
+ hasToken(token: Token<unknown>): boolean;
79
+ /**
80
+ * Returns a resolved value by the token, or returns `undefined` in case the token is not found.
81
+ */
82
+ get<T>(token: Token<T>): T | undefined;
83
+ /**
84
+ * Returns a resolved value by the token, or throws `ResolverError` in case the token is not found.
85
+ */
86
+ resolve<T>(token: Token<T>): T;
87
+ /**
88
+ * Removes a binding for the token.
89
+ */
90
+ remove<T>(token: Token<T>): void;
91
+ /**
92
+ * Removes all bindings in the container.
93
+ */
94
+ removeAll(): void;
95
+ };
96
+ /** @internal */
97
+ export declare const CONTAINER: Token<Container>;
98
+ /** @internal */
99
+ export declare const PARENT_CONTAINER: Token<Container>;
100
+ /** @internal */
101
+ export declare const RESOLVER: Token<Resolver>;
102
+ /** @internal */
103
+ declare const NOT_FOUND: unique symbol;
104
+ /** @internal */
105
+ export declare const FAKE_FACTORY: () => never;
106
+ /** @internal */
107
+ declare type FactoryContext<T> = {
108
+ factory: (container: Container) => T;
109
+ options?: FactoryOptions<T>;
110
+ };
111
+ /** @internal */
112
+ export declare type FactoriesMap = Map<symbol, FactoryContext<any>>;
113
+ /** @internal */
114
+ export declare const FACTORIES_MAP: Token<FactoriesMap>;
115
+ /** @internal */
116
+ declare type Resolver = <T>(token: Token<T>, origin: Container) => T | typeof NOT_FOUND;
117
+ /**
118
+ * Creates a new dependency container.
119
+ *
120
+ * Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token.
121
+ *
122
+ * @param parentContainer - Optional parent container.
123
+ */
124
+ export declare function createContainer(parentContainer?: Container): Container;
125
+ export {};