@stone-js/service-container 0.0.41

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.
@@ -0,0 +1,178 @@
1
+ import { Proxiable } from './Proxiable';
2
+ import { Binding } from './models/Binding';
3
+ import { BindingKey, BindingValue, Resolver } from './declarations';
4
+ /**
5
+ * Class representing a Container.
6
+ *
7
+ * The Container class acts as a dependency injection container, managing bindings and resolving instances.
8
+ * It supports different types of bindings, such as singletons, factories, and instances, and allows the use of aliases for bindings.
9
+ * This makes it easier to manage and resolve complex dependency trees in an application.
10
+ *
11
+ * @author Mr. Stone <evensstone@gmail.com>
12
+ */
13
+ export declare class Container extends Proxiable {
14
+ private readonly aliases;
15
+ private readonly resolvingKeys;
16
+ private readonly bindings;
17
+ /**
18
+ * Create a container.
19
+ *
20
+ * Initializes the container with empty alias and binding maps.
21
+ */
22
+ constructor();
23
+ /**
24
+ * Retrieve the value of the bindings property.
25
+ *
26
+ * @returns A map of all bindings registered in the container.
27
+ */
28
+ getBindings(): Map<BindingKey, Binding<BindingValue>>;
29
+ /**
30
+ * Retrieve the value of the aliases property.
31
+ *
32
+ * @returns A map of all aliases registered in the container.
33
+ */
34
+ getAliases(): Map<string, BindingKey>;
35
+ /**
36
+ * Set a binding as alias.
37
+ *
38
+ * Adds one or more aliases for a given binding key.
39
+ *
40
+ * @param key - The binding value.
41
+ * @param aliases - One or more strings representing the aliases.
42
+ * @returns The container instance.
43
+ */
44
+ alias(key: BindingKey, aliases: string | string[]): this;
45
+ /**
46
+ * Check if an alias exists in the container.
47
+ *
48
+ * @param alias - The alias to check.
49
+ * @returns True if the alias exists, false otherwise.
50
+ */
51
+ isAlias(alias: BindingKey): boolean;
52
+ /**
53
+ * Get a binding key by its alias.
54
+ *
55
+ * @param alias - The alias name.
56
+ * @returns The binding key associated with the alias, or undefined if not found.
57
+ */
58
+ getAliasKey(alias: BindingKey): BindingKey | undefined;
59
+ /**
60
+ * Set class name as camelCase alias.
61
+ *
62
+ * Automatically assigns a camelCase alias to a given class based on its name or metadata.
63
+ *
64
+ * @param Class - The class to alias.
65
+ * @returns The container instance.
66
+ */
67
+ asAlias(Class: Function): this;
68
+ /**
69
+ * Bind a single instance or value into the container under the provided key.
70
+ *
71
+ * @param key - The key to associate with the value.
72
+ * @param value - The value to be bound.
73
+ * @returns The container instance.
74
+ */
75
+ instance(key: BindingKey, value: BindingValue): this;
76
+ /**
77
+ * Bind a single instance or value into the container under the provided key if not already bound.
78
+ *
79
+ * @param key - The key to associate with the value.
80
+ * @param value - The value to be bound.
81
+ * @returns The container instance.
82
+ */
83
+ instanceIf(key: BindingKey, value: BindingValue): this;
84
+ /**
85
+ * Bind a resolver function into the container under the provided key as a singleton.
86
+ *
87
+ * The resolver function will be called once, and the resulting value will be cached for future use.
88
+ *
89
+ * @param key - The key to associate with the singleton value.
90
+ * @param resolver - The resolver function to provide the value.
91
+ * @returns The container instance.
92
+ */
93
+ singleton<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
94
+ /**
95
+ * Bind a resolver function into the container under the provided key as a singleton if not already bound.
96
+ *
97
+ * @param key - The key to associate with the singleton value.
98
+ * @param resolver - The resolver function to provide the value.
99
+ * @returns The container instance.
100
+ */
101
+ singletonIf<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
102
+ /**
103
+ * Bind a resolver function into the container under the provided key, returning a new instance each time.
104
+ *
105
+ * @param key - The key to associate with the value.
106
+ * @param resolver - The resolver function to provide the value.
107
+ * @returns The container instance.
108
+ */
109
+ binding<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
110
+ /**
111
+ * Bind a resolver function into the container under the provided key, returning a new instance each time if not already bound.
112
+ *
113
+ * @param key - The key to associate with the value.
114
+ * @param resolver - The resolver function to provide the value.
115
+ * @returns The container instance.
116
+ */
117
+ bindingIf<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
118
+ /**
119
+ * Resolve a registered value from the container by its key.
120
+ *
121
+ * @param key - The key to resolve.
122
+ * @returns The resolved value.
123
+ * @throws ContainerError if the key cannot be resolved.
124
+ */
125
+ make<V extends BindingValue>(key: BindingKey): V | undefined;
126
+ /**
127
+ * Resolve a value from the container by its key, binding it if necessary.
128
+ *
129
+ * @param key - The key to resolve.
130
+ * @param singleton - Whether to bind as a singleton if not already bound.
131
+ * @returns The resolved value.
132
+ */
133
+ resolve<V extends BindingValue>(key: BindingKey, singleton?: boolean): V | undefined;
134
+ /**
135
+ * Resolve a value from the container by its key and return it in a factory function.
136
+ *
137
+ * @param key - The key to resolve.
138
+ * @returns A factory function that returns the resolved value.
139
+ */
140
+ factory<V extends BindingValue>(key: BindingKey): () => V | undefined;
141
+ /**
142
+ * Check if a value is already bound in the container by its key.
143
+ *
144
+ * @param key - The key to check.
145
+ * @returns True if the key is bound, false otherwise.
146
+ */
147
+ bound(key: BindingKey): boolean;
148
+ /**
149
+ * Check if a value is already bound in the container by its key.
150
+ *
151
+ * @param key - The key to check.
152
+ * @returns True if the key is bound, false otherwise.
153
+ */
154
+ has(key: BindingKey): boolean;
155
+ /**
156
+ * Reset the container so that all bindings are removed.
157
+ *
158
+ * @returns The container instance.
159
+ */
160
+ clear(): this;
161
+ /**
162
+ * Register services with zero configuration.
163
+ *
164
+ * @param classes - Classes representing the services to be registered in the container.
165
+ * @returns The container instance.
166
+ */
167
+ register(classes: Function | Function[]): this;
168
+ /**
169
+ * AutoBind value to the service container.
170
+ *
171
+ * @param name - A key to make the binding. Can be anything.
172
+ * @param item - The item to bind.
173
+ * @param singleton - Bind as singleton when true.
174
+ * @param alias - Key binding aliases.
175
+ * @returns The container instance.
176
+ */
177
+ autoBinding<V extends BindingValue>(name: BindingKey, item?: V, singleton?: boolean, alias?: string | string[]): this;
178
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Class representing a Proxiable.
3
+ *
4
+ * This class allows instances to be wrapped in a Proxy, enabling custom behaviors for property access, assignment, etc.
5
+ *
6
+ * @author Mr. Stone <evensstone@gmail.com>
7
+ */
8
+ export declare abstract class Proxiable {
9
+ /**
10
+ * Creates a Proxiable instance wrapped in a Proxy.
11
+ *
12
+ * @param handler - A trap object for the proxy, which defines custom behavior for fundamental operations (e.g., property lookup, assignment, etc.).
13
+ * @returns A new proxy object for this instance.
14
+ */
15
+ constructor(handler: ProxyHandler<Proxiable>);
16
+ }
@@ -0,0 +1,45 @@
1
+ import { Container } from './Container';
2
+ /**
3
+ * A resolver function that takes a container and returns a value of type V.
4
+ *
5
+ * @template V - The type of value that the resolver returns.
6
+ * @param container - The container used to resolve dependencies.
7
+ * @returns The resolved value of type V.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const myResolver: Resolver<number> = (container: Container) => {
12
+ * // Use the container to resolve dependencies and return a number.
13
+ * return 42;
14
+ * };
15
+ * ```
16
+ */
17
+ export type Resolver<V> = (container: Container) => V;
18
+ /**
19
+ * A union type representing the possible keys that can be used to bind values in the container.
20
+ *
21
+ * Binding keys can be of various types, such as numbers, booleans, strings, functions, objects, or symbols.
22
+ * These types are used because they provide a broad range of ways to uniquely identify a binding.
23
+ *
24
+ * - `number`, `boolean`, `string`: These are basic types that are easy to use and uniquely identify a binding.
25
+ * - `Function`: Useful for identifying bindings by constructor or other functions.
26
+ * - `object`: Allows more complex key types, like instances of classes.
27
+ * - `symbol`: Guarantees a unique identifier, which can prevent conflicts.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * const key1: BindingKey = 42; // Using a number as a key
32
+ * const key2: BindingKey = 'serviceName'; // Using a string as a key
33
+ * const key3: BindingKey = Symbol('uniqueKey'); // Using a symbol for uniqueness
34
+ * const key4: BindingKey = MyServiceClass; // Using a function (constructor) as a key
35
+ * const key5: BindingKey = { custom: 'objectKey' }; // Using an object as a key
36
+ * ```
37
+ */
38
+ export type BindingKey = number | boolean | string | Function | object | symbol;
39
+ /**
40
+ * A union type representing the possible values that can be bound in the container.
41
+ *
42
+ * Binding values can be of various types, including numbers, booleans, strings, functions, objects, or symbols.
43
+ * Unlike `BindingKey`, `BindingValue` represents the actual data or instance being bound, while `BindingKey` represents the identifier used to access that data.
44
+ */
45
+ export type BindingValue = number | boolean | string | Function | object | symbol;
@@ -0,0 +1,62 @@
1
+ import { BindingKey } from '../declarations';
2
+ /**
3
+ * Class representing a ContainerError.
4
+ *
5
+ * @author Mr. Stone <evensstone@gmail.com>
6
+ */
7
+ export declare class ContainerError extends Error {
8
+ /**
9
+ * Error type indicating an alias conflict.
10
+ */
11
+ static readonly ALIAS_TYPE = "alias";
12
+ /**
13
+ * Error type indicating that the resolver is not a function.
14
+ */
15
+ static readonly RESOLVER_TYPE = "resolver";
16
+ /**
17
+ * Error type indicating a resolution failure.
18
+ */
19
+ static readonly RESOLUTION_TYPE = "resolution";
20
+ /**
21
+ * Error type indicating an attempt to alias an unbound value.
22
+ */
23
+ static readonly ALIAS_UNBOUND_TYPE = "alias_unbound";
24
+ /**
25
+ * Error type indicating that a value is not a service.
26
+ */
27
+ static readonly NOT_A_SERVICE_TYPE = "not_a_service";
28
+ /**
29
+ * Error type indicating an error thrown by the resolver function.
30
+ */
31
+ static readonly CANNOT_RESOLVE_TYPE = "cannot_resolve";
32
+ /**
33
+ * Error type indicating a circular dependency.
34
+ */
35
+ static readonly CIRCULAR_DEPENDENCY_TYPE = "circular_dependency";
36
+ /**
37
+ * The type of the error.
38
+ */
39
+ private readonly type;
40
+ /**
41
+ * Create a ContainerError.
42
+ *
43
+ * @param type - The type of the error.
44
+ * @param message - The error message or key related to the error.
45
+ */
46
+ constructor(type: string, message: BindingKey);
47
+ /**
48
+ * Retrieve the error message based on the type and provided message.
49
+ *
50
+ * @param type - The type of the error.
51
+ * @param message - The error message or key related to the error.
52
+ * @returns The formatted error message.
53
+ */
54
+ private getMessage;
55
+ /**
56
+ * Retrieve the resolution message based on the key.
57
+ *
58
+ * @param key - The key for which the resolution failed.
59
+ * @returns The formatted resolution error message.
60
+ */
61
+ private getResolutionMessage;
62
+ }