@stone-js/service-container 0.0.41 → 0.0.42

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.
@@ -1,6 +1,105 @@
1
- import { Proxiable } from './Proxiable';
2
- import { Binding } from './models/Binding';
3
- import { BindingKey, BindingValue, Resolver } from './declarations';
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
+ 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
+ }
17
+
18
+ /**
19
+ * A resolver function that takes a container and returns a value of type V.
20
+ *
21
+ * @template V - The type of value that the resolver returns.
22
+ * @param container - The container used to resolve dependencies.
23
+ * @returns The resolved value of type V.
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const myResolver: Resolver<number> = (container: Container) => {
28
+ * // Use the container to resolve dependencies and return a number.
29
+ * return 42;
30
+ * };
31
+ * ```
32
+ */
33
+ type Resolver<V> = (container: Container) => V;
34
+ /**
35
+ * A union type representing the possible keys that can be used to bind values in the container.
36
+ *
37
+ * Binding keys can be of various types, such as numbers, booleans, strings, functions, objects, or symbols.
38
+ * These types are used because they provide a broad range of ways to uniquely identify a binding.
39
+ *
40
+ * - `number`, `boolean`, `string`: These are basic types that are easy to use and uniquely identify a binding.
41
+ * - `Function`: Useful for identifying bindings by constructor or other functions.
42
+ * - `object`: Allows more complex key types, like instances of classes.
43
+ * - `symbol`: Guarantees a unique identifier, which can prevent conflicts.
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * const key1: BindingKey = 42; // Using a number as a key
48
+ * const key2: BindingKey = 'serviceName'; // Using a string as a key
49
+ * const key3: BindingKey = Symbol('uniqueKey'); // Using a symbol for uniqueness
50
+ * const key4: BindingKey = MyServiceClass; // Using a function (constructor) as a key
51
+ * const key5: BindingKey = { custom: 'objectKey' }; // Using an object as a key
52
+ * ```
53
+ */
54
+ type BindingKey = number | boolean | string | Function | object | symbol;
55
+ /**
56
+ * A union type representing the possible values that can be bound in the container.
57
+ *
58
+ * Binding values can be of various types, including numbers, booleans, strings, functions, objects, or symbols.
59
+ * Unlike `BindingKey`, `BindingValue` represents the actual data or instance being bound, while `BindingKey` represents the identifier used to access that data.
60
+ */
61
+ type BindingValue = number | boolean | string | Function | object | symbol;
62
+
63
+ /**
64
+ * Abstract class representing a Binding.
65
+ *
66
+ * This abstract class serves as the base class for all types of bindings in the service container. It holds a value and provides an abstract method
67
+ * to resolve and return that value, allowing different subclasses to implement their own resolution logic. Bindings are used to manage dependencies
68
+ * and control how objects are instantiated within the container.
69
+ *
70
+ * @template V - The type of value that this binding holds.
71
+ * @author Mr. Stone <evensstone@gmail.com>
72
+ */
73
+ declare abstract class Binding<V extends BindingValue> {
74
+ /**
75
+ * The value held by the binding.
76
+ *
77
+ * This value is resolved at runtime, either directly or through a resolver function.
78
+ */
79
+ protected value?: V;
80
+ /**
81
+ * Create a new instance of Binding.
82
+ *
83
+ * @param value - The value to be held by the binding.
84
+ */
85
+ constructor(value?: V);
86
+ /**
87
+ * Check if the value has been resolved.
88
+ *
89
+ * @returns A boolean indicating whether the value has been resolved.
90
+ */
91
+ protected isResolved(): boolean;
92
+ /**
93
+ * Resolve and return the value of the binding.
94
+ *
95
+ * This abstract method must be implemented by subclasses to provide specific resolution logic.
96
+ *
97
+ * @param container - The container to resolve dependencies from.
98
+ * @returns The resolved value of the binding.
99
+ */
100
+ abstract resolve(container: Container): V | undefined;
101
+ }
102
+
4
103
  /**
5
104
  * Class representing a Container.
6
105
  *
@@ -10,7 +109,7 @@ import { BindingKey, BindingValue, Resolver } from './declarations';
10
109
  *
11
110
  * @author Mr. Stone <evensstone@gmail.com>
12
111
  */
13
- export declare class Container extends Proxiable {
112
+ declare class Container extends Proxiable {
14
113
  private readonly aliases;
15
114
  private readonly resolvingKeys;
16
115
  private readonly bindings;
@@ -176,3 +275,5 @@ export declare class Container extends Proxiable {
176
275
  */
177
276
  autoBinding<V extends BindingValue>(name: BindingKey, item?: V, singleton?: boolean, alias?: string | string[]): this;
178
277
  }
278
+
279
+ export { type BindingKey, type BindingValue, Container, Proxiable, type Resolver };
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "@stone-js/service-container",
3
- "version": "0.0.41",
3
+ "version": "0.0.42",
4
4
  "description": "Vanilla Javascript IoC Service Container with proposal decorator, proxy resolver and destructuring injection",
5
5
  "author": "Mr. Stone <evensstone@gmail.com>",
6
6
  "license": "Apache-2.0",
7
- "repository": "git@github.com:stonemjs/service-container.git",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+ssh://git@github.com/stonemjs/service-container.git"
10
+ },
8
11
  "homepage": "https://github.com/stonemjs/service-container#readme",
9
12
  "bugs": {
10
13
  "url": "https://github.com/stonemjs/service-container/issues"
@@ -25,12 +28,10 @@
25
28
  "/dist"
26
29
  ],
27
30
  "type": "module",
31
+ "types": "./dist/index.d.ts",
28
32
  "exports": {
29
33
  ".": {
30
- "types": [
31
- "./dist/declarations.d.ts",
32
- "./dist/Container.d.ts"
33
- ],
34
+ "types": "./dist/index.d.ts",
34
35
  "default": "./dist/index.js"
35
36
  }
36
37
  },
@@ -63,25 +64,23 @@
63
64
  "@rollup/plugin-typescript": "^12.1.1",
64
65
  "@types/lodash-es": "^4.17.12",
65
66
  "@types/node": "^22.9.0",
66
- "@types/validator": "^13.12.2",
67
67
  "@vitest/coverage-v8": "^2.1.4",
68
68
  "husky": "^9.1.6",
69
69
  "rimraf": "^5.0.5",
70
70
  "rollup": "^4.1.5",
71
+ "rollup-plugin-delete": "^2.1.0",
72
+ "rollup-plugin-dts": "^6.1.1",
71
73
  "rollup-plugin-node-externals": "^6.1.2",
72
74
  "ts-standard": "^12.0.2",
73
75
  "tslib": "^2.8.1",
74
76
  "typedoc": "^0.26.11",
75
77
  "typedoc-plugin-markdown": "^4.2.10",
76
78
  "typescript": "^5.6.3",
77
- "validator": "^13.12.0",
78
79
  "vitest": "^2.1.4"
79
80
  },
80
81
  "ts-standard": {
81
82
  "globals": [
82
83
  "it",
83
- "jest",
84
- "test",
85
84
  "expect",
86
85
  "describe",
87
86
  "beforeEach"
@@ -1,16 +0,0 @@
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
- }
@@ -1,45 +0,0 @@
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;
@@ -1,62 +0,0 @@
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
- }
@@ -1,41 +0,0 @@
1
- import { Container } from '../Container';
2
- import { BindingValue } from '../declarations';
3
- /**
4
- * Abstract class representing a Binding.
5
- *
6
- * This abstract class serves as the base class for all types of bindings in the service container. It holds a value and provides an abstract method
7
- * to resolve and return that value, allowing different subclasses to implement their own resolution logic. Bindings are used to manage dependencies
8
- * and control how objects are instantiated within the container.
9
- *
10
- * @template V - The type of value that this binding holds.
11
- * @author Mr. Stone <evensstone@gmail.com>
12
- */
13
- export declare abstract class Binding<V extends BindingValue> {
14
- /**
15
- * The value held by the binding.
16
- *
17
- * This value is resolved at runtime, either directly or through a resolver function.
18
- */
19
- protected value?: V;
20
- /**
21
- * Create a new instance of Binding.
22
- *
23
- * @param value - The value to be held by the binding.
24
- */
25
- constructor(value?: V);
26
- /**
27
- * Check if the value has been resolved.
28
- *
29
- * @returns A boolean indicating whether the value has been resolved.
30
- */
31
- protected isResolved(): boolean;
32
- /**
33
- * Resolve and return the value of the binding.
34
- *
35
- * This abstract method must be implemented by subclasses to provide specific resolution logic.
36
- *
37
- * @param container - The container to resolve dependencies from.
38
- * @returns The resolved value of the binding.
39
- */
40
- abstract resolve(container: Container): V | undefined;
41
- }
@@ -1,25 +0,0 @@
1
- import { Container } from '../Container';
2
- import { BindingValue } from '../declarations';
3
- import { ResolverBinding } from './ResolverBinding';
4
- /**
5
- * Class representing a Factory.
6
- *
7
- * The Factory class extends the ResolverBinding class, providing a mechanism to resolve a new instance each time the binding is resolved.
8
- * This ensures that a fresh instance is created with each call to the `resolve` method.
9
- *
10
- * @template V - The type of value that this binding holds.
11
- * @author Mr. Stone <evensstone@gmail.com>
12
- */
13
- export declare class Factory<V extends BindingValue> extends ResolverBinding<V> {
14
- /**
15
- * Resolve and return the value of the binding.
16
- *
17
- * Each time this method is called, a new value is resolved using the resolver function.
18
- * This is intended for cases where a fresh instance is required for each resolution, such as factories or transient dependencies.
19
- *
20
- * @param container - The container to resolve dependencies from.
21
- * @returns The resolved value of the binding.
22
- * @throws ContainerError if the value cannot be resolved.
23
- */
24
- resolve(container: Container): V;
25
- }
@@ -1,21 +0,0 @@
1
- import { Binding } from './Binding';
2
- import { Container } from '../Container';
3
- import { BindingValue } from '../declarations';
4
- /**
5
- * Class representing an Instance.
6
- *
7
- * This class extends the Binding class and directly holds an instance value.
8
- * It provides a straightforward resolution mechanism that simply returns the stored value.
9
- *
10
- * @template V - The type of value that this binding holds.
11
- * @author Mr. Stone <evensstone@gmail.com>
12
- */
13
- export declare class Instance<V extends BindingValue> extends Binding<V> {
14
- /**
15
- * Resolve and return the value of the binding.
16
- *
17
- * @param _container - Container to resolve dependencies (not used in this implementation).
18
- * @returns The resolved value of the binding.
19
- */
20
- resolve(_container: Container): V | undefined;
21
- }
@@ -1,26 +0,0 @@
1
- import { Binding } from './Binding';
2
- import { BindingValue, Resolver } from '../declarations';
3
- /**
4
- * Class representing a ResolverBinding.
5
- *
6
- * This class extends the Binding class, using a resolver function to lazily resolve the value when needed.
7
- *
8
- * @template V - The type of value that this binding holds.
9
- * @author Mr. Stone <evensstone@gmail.com>
10
- */
11
- export declare abstract class ResolverBinding<V extends BindingValue> extends Binding<V> {
12
- /**
13
- * The resolver function used to provide the binding value.
14
- *
15
- * This function will be called when the value is needed, allowing for lazy instantiation
16
- * and dependency resolution. It should return an instance of type `V`.
17
- */
18
- protected readonly resolver: Resolver<V>;
19
- /**
20
- * Create a new instance of ResolverBinding.
21
- *
22
- * @param resolver - The resolver function to provide the binding value.
23
- * @throws ContainerError if the resolver is not a function.
24
- */
25
- constructor(resolver: Resolver<V>);
26
- }
@@ -1,25 +0,0 @@
1
- import { Container } from '../Container';
2
- import { BindingValue } from '../declarations';
3
- import { ResolverBinding } from './ResolverBinding';
4
- /**
5
- * Class representing a Singleton.
6
- *
7
- * The Singleton class extends the ResolverBinding class, ensuring that the value is only resolved once.
8
- * Subsequent calls to the `resolve` method will return the previously resolved value, making it behave as a singleton.
9
- *
10
- * @template V - The type of value that this binding holds.
11
- * @author Mr. Stone <evensstone@gmail.com>
12
- */
13
- export declare class Singleton<V extends BindingValue> extends ResolverBinding<V> {
14
- /**
15
- * Resolve and return the value of the binding.
16
- *
17
- * If the value has already been resolved, return the cached value. Otherwise, use the resolver function
18
- * to resolve the value, store it, and return it.
19
- *
20
- * @param container - The container to resolve dependencies from.
21
- * @returns The resolved value of the binding.
22
- * @throws ContainerError if the value cannot be resolved.
23
- */
24
- resolve(container: Container): V | undefined;
25
- }