@stone-js/service-container 0.1.3 → 0.8.1

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 CHANGED
@@ -1,16 +1,16 @@
1
1
  # Stone.js - Service Container
2
2
 
3
- [![npm](https://img.shields.io/npm/l/@stone-js/service-container)](https://opensource.org/licenses/MIT)
4
- [![npm](https://img.shields.io/npm/v/@stone-js/service-container)](https://www.npmjs.com/package/@stone-js/service-container)
5
- [![npm](https://img.shields.io/npm/dm/@stone-js/service-container)](https://www.npmjs.com/package/@stone-js/service-container)
3
+ [![npm license](https://img.shields.io/npm/l/@stone-js/service-container)](https://opensource.org/licenses/MIT)
4
+ [![npm version](https://img.shields.io/npm/v/@stone-js/service-container)](https://www.npmjs.com/package/@stone-js/service-container)
5
+ [![npm downloads](https://img.shields.io/npm/dm/@stone-js/service-container)](https://www.npmjs.com/package/@stone-js/service-container)
6
6
  ![Maintenance](https://img.shields.io/maintenance/yes/2026)
7
- [![Build Status](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/main.yml/badge.svg)](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/main.yml)
8
- [![Publish Package to npmjs](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/release.yml/badge.svg)](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/release.yml)
9
- [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=stone-foundation_stone-js-service-container&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=stone-foundation_stone-js-service-container)
10
- [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=stone-foundation_stone-js-service-container&metric=coverage)](https://sonarcloud.io/summary/new_code?id=stone-foundation_stone-js-service-container)
11
- [![Security Policy](https://img.shields.io/badge/Security-Policy-blue.svg)](./SECURITY.md)
12
- [![CodeQL](https://github.com/stone-foundation/stone-js-service-container/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/stone-foundation/stone-js-service-container/security/code-scanning)
13
- [![Dependabot Status](https://img.shields.io/badge/Dependabot-enabled-brightgreen.svg)](https://github.com/stone-foundation/stone-js-service-container/network/updates)
7
+ [![CI](https://github.com/stone-foundation/stone-js-framework/actions/workflows/ci.yml/badge.svg)](https://github.com/stone-foundation/stone-js-framework/actions/workflows/ci.yml)
8
+ [![Release](https://github.com/stone-foundation/stone-js-framework/actions/workflows/release.yml/badge.svg)](https://github.com/stone-foundation/stone-js-framework/actions/workflows/release.yml)
9
+ [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=stone-foundation_stone-js-framework&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=stone-foundation_stone-js-framework)
10
+ [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=stone-foundation_stone-js-framework&metric=coverage)](https://sonarcloud.io/summary/new_code?id=stone-foundation_stone-js-framework)
11
+ [![Security Policy](https://img.shields.io/badge/Security-Policy-blue.svg)](https://github.com/stone-foundation/stone-js-framework/blob/main/SECURITY.md)
12
+ [![CodeQL](https://github.com/stone-foundation/stone-js-framework/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/stone-foundation/stone-js-framework/security/code-scanning)
13
+ [![Dependabot Status](https://img.shields.io/badge/Dependabot-enabled-brightgreen.svg)](https://github.com/stone-foundation/stone-js-framework/network/updates)
14
14
  [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg)](https://conventionalcommits.org)
15
15
 
16
16
  IoC Service Container with proxy resolver and destructuring injection provides a very simple, centralized container that stores and resolves libraries, objects, and values to better organize code, manage dependencies, and enhance testability.
@@ -0,0 +1,181 @@
1
+ import { Proxiable } from './Proxiable';
2
+ import { Binding } from './models/Binding';
3
+ import { BindingKey, BindingValue, Resolver, IContainer } 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 implements IContainer {
14
+ private readonly aliases;
15
+ private readonly resolvingKeys;
16
+ private readonly bindings;
17
+ /**
18
+ * Create a Container.
19
+ *
20
+ * @returns A new Container instance.
21
+ */
22
+ static create(): Container;
23
+ /**
24
+ * Create a ProxyHandler for the container.
25
+ *
26
+ * @returns A new ProxyHandler instance.
27
+ */
28
+ /**
29
+ * Well-known property names that must never trigger service resolution. Accessing them
30
+ * (via `await container`, `JSON.stringify`, `console.log`, React element checks, spread,
31
+ * etc.) returns `undefined` so the container is safe to inspect and pass around, while
32
+ * genuine unknown-service access still fails fast through `make()`.
33
+ */
34
+ private static readonly SYSTEM_PROPS;
35
+ private static Proxyhandler;
36
+ /**
37
+ * Create a container.
38
+ *
39
+ * Initializes the container with empty alias and binding maps.
40
+ */
41
+ protected constructor();
42
+ /**
43
+ * Retrieve the value of the bindings property.
44
+ *
45
+ * @returns A map of all bindings registered in the container.
46
+ */
47
+ getBindings(): Map<BindingKey, Binding<BindingValue>>;
48
+ /**
49
+ * Retrieve the value of the aliases property.
50
+ *
51
+ * @returns A map of all aliases registered in the container.
52
+ */
53
+ getAliases(): Map<string, BindingKey>;
54
+ /**
55
+ * Set a binding as alias.
56
+ *
57
+ * Adds one or more aliases for a given binding key.
58
+ *
59
+ * @param key - The binding value.
60
+ * @param aliases - One or more strings representing the aliases.
61
+ * @returns The container instance.
62
+ */
63
+ alias(key: BindingKey, aliases: string | string[]): this;
64
+ /**
65
+ * Check if an alias exists in the container.
66
+ *
67
+ * @param alias - The alias to check.
68
+ * @returns True if the alias exists, false otherwise.
69
+ */
70
+ isAlias(alias: BindingKey): boolean;
71
+ /**
72
+ * Get a binding key by its alias.
73
+ *
74
+ * @param alias - The alias name.
75
+ * @returns The binding key associated with the alias, or undefined if not found.
76
+ */
77
+ getAliasKey(alias: BindingKey): BindingKey | undefined;
78
+ /**
79
+ * Bind a single instance or value into the container under the provided key.
80
+ *
81
+ * @param key - The key to associate with the value.
82
+ * @param value - The value to be bound.
83
+ * @returns The container instance.
84
+ */
85
+ instance(key: BindingKey, value: BindingValue): this;
86
+ /**
87
+ * Bind a single instance or value into the container under the provided key if not already bound.
88
+ *
89
+ * @param key - The key to associate with the value.
90
+ * @param value - The value to be bound.
91
+ * @returns The container instance.
92
+ */
93
+ instanceIf(key: BindingKey, value: BindingValue): this;
94
+ /**
95
+ * Bind a resolver function into the container under the provided key as a singleton.
96
+ *
97
+ * The resolver function will be called once, and the resulting value will be cached for future use.
98
+ *
99
+ * @param key - The key to associate with the singleton value.
100
+ * @param resolver - The resolver function to provide the value.
101
+ * @returns The container instance.
102
+ */
103
+ singleton<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
104
+ /**
105
+ * Bind a resolver function into the container under the provided key as a singleton if not already bound.
106
+ *
107
+ * @param key - The key to associate with the singleton value.
108
+ * @param resolver - The resolver function to provide the value.
109
+ * @returns The container instance.
110
+ */
111
+ singletonIf<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
112
+ /**
113
+ * Bind a resolver function into the container under the provided key, returning a new instance each time.
114
+ *
115
+ * @param key - The key to associate with the value.
116
+ * @param resolver - The resolver function to provide the value.
117
+ * @returns The container instance.
118
+ */
119
+ binding<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
120
+ /**
121
+ * Bind a resolver function into the container under the provided key, returning a new instance each time if not already bound.
122
+ *
123
+ * @param key - The key to associate with the value.
124
+ * @param resolver - The resolver function to provide the value.
125
+ * @returns The container instance.
126
+ */
127
+ bindingIf<V extends BindingValue>(key: BindingKey, resolver: Resolver<V>): this;
128
+ /**
129
+ * Resolve a registered value from the container by its key.
130
+ *
131
+ * @param key - The key to resolve.
132
+ * @returns The resolved value.
133
+ * @throws ContainerError if the key cannot be resolved.
134
+ */
135
+ make<V extends BindingValue>(key: BindingKey): V;
136
+ /**
137
+ * Resolve a value from the container by its key, binding it if necessary.
138
+ *
139
+ * @param key - The key to resolve.
140
+ * @param singleton - Whether to bind as a singleton if not already bound.
141
+ * @returns The resolved value.
142
+ */
143
+ resolve<V extends BindingValue>(key: BindingKey, singleton?: boolean): V;
144
+ /**
145
+ * Resolve a value from the container by its key and return it in a factory function.
146
+ *
147
+ * @param key - The key to resolve.
148
+ * @returns A factory function that returns the resolved value.
149
+ */
150
+ factory<V extends BindingValue>(key: BindingKey): () => V;
151
+ /**
152
+ * Check if a value is already bound in the container by its key.
153
+ *
154
+ * @param key - The key to check.
155
+ * @returns True if the key is bound, false otherwise.
156
+ */
157
+ bound(key: BindingKey): boolean;
158
+ /**
159
+ * Check if a value is already bound in the container by its key.
160
+ *
161
+ * @param key - The key to check.
162
+ * @returns True if the key is bound, false otherwise.
163
+ */
164
+ has(key: BindingKey): boolean;
165
+ /**
166
+ * Reset the container so that all bindings are removed.
167
+ *
168
+ * @returns The container instance.
169
+ */
170
+ clear(): this;
171
+ /**
172
+ * AutoBind value to the service container.
173
+ *
174
+ * @param name - A key to make the binding. Can be anything.
175
+ * @param item - The item to bind.
176
+ * @param singleton - Bind as singleton when true.
177
+ * @param alias - Key binding aliases.
178
+ * @returns The container instance.
179
+ */
180
+ autoBinding<V extends BindingValue>(name: BindingKey, item?: V, singleton?: boolean, alias?: string | string[]): this;
181
+ }
@@ -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,144 @@
1
+ /**
2
+ * A resolver function that takes a container and returns a value of type V.
3
+ *
4
+ * @template V - The type of value that the resolver returns.
5
+ * @param container - The container used to resolve dependencies.
6
+ * @returns The resolved value of type V.
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * const myResolver: Resolver<number> = (container: IContainer) => {
11
+ * // Use the container to resolve dependencies and return a number.
12
+ * return 42;
13
+ * };
14
+ * ```
15
+ */
16
+ export type Resolver<V> = (container: IContainer) => V;
17
+ /**
18
+ * A union type representing the possible keys that can be used to bind values in the container.
19
+ *
20
+ * Binding keys can be of various types, such as numbers, booleans, strings, functions, objects, or symbols.
21
+ * These types are used because they provide a broad range of ways to uniquely identify a binding.
22
+ *
23
+ * - `number`, `boolean`, `string`: These are basic types that are easy to use and uniquely identify a binding.
24
+ * - `Function`: Useful for identifying bindings by constructor or other functions.
25
+ * - `object`: Allows more complex key types, like instances of classes.
26
+ * - `symbol`: Guarantees a unique identifier, which can prevent conflicts.
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * const key1: BindingKey = 42; // Using a number as a key
31
+ * const key2: BindingKey = 'serviceName'; // Using a string as a key
32
+ * const key3: BindingKey = Symbol('uniqueKey'); // Using a symbol for uniqueness
33
+ * const key4: BindingKey = MyServiceClass; // Using a function (constructor) as a key
34
+ * const key5: BindingKey = { custom: 'objectKey' }; // Using an object as a key
35
+ * ```
36
+ */
37
+ export type BindingKey = number | boolean | string | Function | object | symbol;
38
+ /**
39
+ * A union type representing the possible values that can be bound in the container.
40
+ *
41
+ * Binding values can be of various types, including numbers, booleans, strings, functions, objects, or symbols.
42
+ * Unlike `BindingKey`, `BindingValue` represents the actual data or instance being bound, while `BindingKey` represents the identifier used to access that data.
43
+ */
44
+ export type BindingValue = number | boolean | string | Function | object | symbol;
45
+ /**
46
+ * Interface representing a Binding.
47
+ *
48
+ * This interface defines the contract for all types of bindings in the service container.
49
+ * Bindings are used to manage dependencies and control how objects are instantiated within the container.
50
+ *
51
+ * @template V - The type of value that this binding holds.
52
+ * @author Mr. Stone <evensstone@gmail.com>
53
+ */
54
+ export interface IBinding<V extends BindingValue> {
55
+ /**
56
+ * Resolve and return the value of the binding.
57
+ *
58
+ * @param container - The container to resolve dependencies from.
59
+ * @returns The resolved value of the binding.
60
+ */
61
+ resolve: (container: IContainer) => V | undefined;
62
+ }
63
+ /**
64
+ * Interface representing a Container.
65
+ *
66
+ * This interface defines the public contract for dependency injection containers,
67
+ * allowing for better testability and preventing circular dependencies.
68
+ *
69
+ * @author Mr. Stone <evensstone@gmail.com>
70
+ */
71
+ export interface IContainer {
72
+ /**
73
+ * Retrieve the value of the bindings property.
74
+ */
75
+ getBindings: () => Map<BindingKey, IBinding<BindingValue>>;
76
+ /**
77
+ * Retrieve the value of the aliases property.
78
+ */
79
+ getAliases: () => Map<string, BindingKey>;
80
+ /**
81
+ * Set a binding as alias.
82
+ */
83
+ alias: (key: BindingKey, aliases: string | string[]) => this;
84
+ /**
85
+ * Check if an alias exists in the container.
86
+ */
87
+ isAlias: (alias: BindingKey) => boolean;
88
+ /**
89
+ * Get a binding key by its alias.
90
+ */
91
+ getAliasKey: (alias: BindingKey) => BindingKey | undefined;
92
+ /**
93
+ * Bind a single instance or value into the container under the provided key.
94
+ */
95
+ instance: (key: BindingKey, value: BindingValue) => this;
96
+ /**
97
+ * Bind a single instance or value into the container under the provided key if not already bound.
98
+ */
99
+ instanceIf: (key: BindingKey, value: BindingValue) => this;
100
+ /**
101
+ * Bind a resolver function into the container under the provided key as a singleton.
102
+ */
103
+ singleton: <V extends BindingValue>(key: BindingKey, resolver: Resolver<V>) => this;
104
+ /**
105
+ * Bind a resolver function into the container under the provided key as a singleton if not already bound.
106
+ */
107
+ singletonIf: <V extends BindingValue>(key: BindingKey, resolver: Resolver<V>) => this;
108
+ /**
109
+ * Bind a resolver function into the container under the provided key, returning a new instance each time.
110
+ */
111
+ binding: <V extends BindingValue>(key: BindingKey, resolver: Resolver<V>) => this;
112
+ /**
113
+ * Bind a resolver function into the container under the provided key, returning a new instance each time if not already bound.
114
+ */
115
+ bindingIf: <V extends BindingValue>(key: BindingKey, resolver: Resolver<V>) => this;
116
+ /**
117
+ * Resolve a registered value from the container by its key.
118
+ */
119
+ make: <V extends BindingValue>(key: BindingKey) => V;
120
+ /**
121
+ * Resolve a value from the container by its key, binding it if necessary.
122
+ */
123
+ resolve: <V extends BindingValue>(key: BindingKey, singleton?: boolean) => V;
124
+ /**
125
+ * Resolve a value from the container by its key and return it in a factory function.
126
+ */
127
+ factory: <V extends BindingValue>(key: BindingKey) => () => V;
128
+ /**
129
+ * Check if a value is already bound in the container by its key.
130
+ */
131
+ bound: (key: BindingKey) => boolean;
132
+ /**
133
+ * Check if a value is already bound in the container by its key.
134
+ */
135
+ has: (key: BindingKey) => boolean;
136
+ /**
137
+ * Reset the container so that all bindings are removed.
138
+ */
139
+ clear: () => this;
140
+ /**
141
+ * AutoBind value to the service container.
142
+ */
143
+ autoBinding: <V extends BindingValue>(name: BindingKey, item?: V, singleton?: boolean, alias?: string | string[]) => this;
144
+ }
@@ -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
+ }