syrinject 0.0.4 → 0.1.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/dist/container/Container.d.ts +80 -27
- package/dist/container/Container.js +132 -34
- package/dist/decorators/logger.d.ts +19 -9
- package/dist/decorators/logger.js +37 -16
- package/dist/errors/CircularDependencyError.d.ts +13 -0
- package/dist/errors/CircularDependencyError.js +30 -0
- package/dist/errors/DependencyNotFoundError.d.ts +12 -0
- package/dist/errors/DependencyNotFoundError.js +27 -0
- package/dist/errors/InvalidProviderError.d.ts +12 -0
- package/dist/errors/InvalidProviderError.js +27 -0
- package/dist/index.d.ts +9 -3
- package/dist/index.js +13 -1
- package/dist/types/Provider.d.ts +31 -0
- package/dist/types/Provider.js +2 -0
- package/dist/types/Token.d.ts +9 -0
- package/dist/types/Token.js +2 -0
- package/package.json +1 -1
|
@@ -1,19 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*
|
|
4
|
-
* A `Token` can be:
|
|
5
|
-
* - A class constructor (used for class-based dependency injection)
|
|
6
|
-
* - A string (used as a named token)
|
|
7
|
-
* - A symbol (used for unique, non-colliding tokens)
|
|
8
|
-
*
|
|
9
|
-
* @typeParam T - The type of the dependency associated with the token.
|
|
10
|
-
*/
|
|
11
|
-
type Token<T = any> = new (...args: any[]) => T | string | symbol;
|
|
1
|
+
import type { Provider } from '../types/Provider';
|
|
2
|
+
import type { Newable, Token } from '../types/Token';
|
|
12
3
|
/**
|
|
13
4
|
* A simple and lightweight dependency injection container.
|
|
14
5
|
*
|
|
15
6
|
* The `Container` class provides methods to register and resolve dependencies using unique tokens.
|
|
16
|
-
* Tokens can be class constructors, strings, or symbols, allowing
|
|
7
|
+
* Tokens can be class constructors, strings, or symbols, allowing flexible and type-safe dependency management.
|
|
17
8
|
*
|
|
18
9
|
* @example
|
|
19
10
|
* // Create a new container instance
|
|
@@ -21,10 +12,10 @@ type Token<T = any> = new (...args: any[]) => T | string | symbol;
|
|
|
21
12
|
*
|
|
22
13
|
* // Register a class dependency
|
|
23
14
|
* class MyService {}
|
|
24
|
-
* container.register(MyService,
|
|
15
|
+
* container.register(MyService, MyService, { singleton: true });
|
|
25
16
|
*
|
|
26
17
|
* // Register a value with a string token
|
|
27
|
-
* container.
|
|
18
|
+
* container.registerValue('config', { port: 3000 });
|
|
28
19
|
*
|
|
29
20
|
* // Resolve dependencies
|
|
30
21
|
* const service = container.resolve(MyService);
|
|
@@ -32,30 +23,92 @@ type Token<T = any> = new (...args: any[]) => T | string | symbol;
|
|
|
32
23
|
*/
|
|
33
24
|
export declare class Container {
|
|
34
25
|
private registry;
|
|
35
|
-
private singletons;
|
|
36
26
|
/**
|
|
37
27
|
* Registers a dependency with a class constructor and optional dependencies.
|
|
38
28
|
*
|
|
39
|
-
* @typeParam T - The type
|
|
40
|
-
* @param token -
|
|
41
|
-
* @param useClass -
|
|
42
|
-
* @param options - Optional configuration
|
|
43
|
-
* @param options.singleton -
|
|
44
|
-
* @param options.deps - Optional array of tokens
|
|
45
|
-
*/
|
|
46
|
-
register<T>(token: Token<T>, useClass:
|
|
29
|
+
* @typeParam T - The dependency type.
|
|
30
|
+
* @param token - Unique token associated with the dependency.
|
|
31
|
+
* @param useClass - Class constructor used to create instances.
|
|
32
|
+
* @param options - Optional configuration.
|
|
33
|
+
* @param options.singleton - Whether to cache a single instance.
|
|
34
|
+
* @param options.deps - Optional array of dependency tokens.
|
|
35
|
+
*/
|
|
36
|
+
register<T>(token: Token<T>, useClass: Newable<T>, options?: {
|
|
47
37
|
singleton?: boolean;
|
|
48
38
|
deps?: Token[];
|
|
49
39
|
}): void;
|
|
40
|
+
register<T>(token: Token<T>, provider: Provider<T>): void;
|
|
41
|
+
/**
|
|
42
|
+
* Registers a class provider (syntactic sugar for `register` with a class).
|
|
43
|
+
*
|
|
44
|
+
* @typeParam T - The dependency type.
|
|
45
|
+
* @param token - Unique token associated with the dependency.
|
|
46
|
+
* @param useClass - Class constructor used to create instances.
|
|
47
|
+
* @param options - Optional configuration.
|
|
48
|
+
* @param options.singleton - Whether to cache a single instance.
|
|
49
|
+
* @param options.deps - Optional array of dependency tokens.
|
|
50
|
+
*/
|
|
51
|
+
registerClass<T>(token: Token<T>, useClass: Newable<T>, options?: {
|
|
52
|
+
singleton?: boolean;
|
|
53
|
+
deps?: Token[];
|
|
54
|
+
}): void;
|
|
55
|
+
registerClass<T>(useClass: Newable<T>, options?: {
|
|
56
|
+
singleton?: boolean;
|
|
57
|
+
deps?: Token[];
|
|
58
|
+
}): void;
|
|
59
|
+
/**
|
|
60
|
+
* Registers a value provider.
|
|
61
|
+
*
|
|
62
|
+
* @typeParam T - The dependency type.
|
|
63
|
+
* @param token - Unique token associated with the dependency.
|
|
64
|
+
* @param useValue - Value to be returned on resolve.
|
|
65
|
+
*/
|
|
66
|
+
registerValue<T>(token: Token<T>, useValue: T): void;
|
|
67
|
+
/**
|
|
68
|
+
* Registers a factory provider.
|
|
69
|
+
*
|
|
70
|
+
* @typeParam T - The dependency type.
|
|
71
|
+
* @param token - Unique token associated with the dependency.
|
|
72
|
+
* @param useFactory - Factory function that builds the dependency.
|
|
73
|
+
* @param options - Optional configuration.
|
|
74
|
+
* @param options.singleton - Whether to cache a single instance.
|
|
75
|
+
*/
|
|
76
|
+
registerFactory<T>(token: Token<T>, useFactory: (container: Container) => T, options?: {
|
|
77
|
+
singleton?: boolean;
|
|
78
|
+
}): void;
|
|
79
|
+
/**
|
|
80
|
+
* Unregisters a token.
|
|
81
|
+
*
|
|
82
|
+
* @param token - Token to remove from the registry.
|
|
83
|
+
*/
|
|
84
|
+
unregister(token: Token): void;
|
|
85
|
+
/**
|
|
86
|
+
* Clears all registrations and cached singletons.
|
|
87
|
+
*/
|
|
88
|
+
clear(): void;
|
|
89
|
+
/**
|
|
90
|
+
* Checks if a token is registered.
|
|
91
|
+
*
|
|
92
|
+
* @param token - Token to check.
|
|
93
|
+
* @returns True if the token is registered, otherwise false.
|
|
94
|
+
*/
|
|
95
|
+
has(token: Token): boolean;
|
|
50
96
|
/**
|
|
51
97
|
* Resolves a dependency by its token.
|
|
52
98
|
*
|
|
53
|
-
* @typeParam T -
|
|
54
|
-
* @param token -
|
|
99
|
+
* @typeParam T - Expected dependency type.
|
|
100
|
+
* @param token - Token associated with the dependency.
|
|
55
101
|
* @returns The resolved dependency instance or value.
|
|
56
|
-
* @throws
|
|
102
|
+
* @throws DependencyNotFoundError when the token is not registered.
|
|
103
|
+
* @throws CircularDependencyError when a dependency cycle is detected.
|
|
57
104
|
*/
|
|
58
105
|
resolve<T>(token: Token<T>): T;
|
|
106
|
+
private resolveInternal;
|
|
59
107
|
private createInstance;
|
|
108
|
+
private normalizeProvider;
|
|
109
|
+
private isProvider;
|
|
110
|
+
private isClassProvider;
|
|
111
|
+
private isValueProvider;
|
|
112
|
+
private isFactoryProvider;
|
|
113
|
+
private getDepsFromClass;
|
|
60
114
|
}
|
|
61
|
-
export {};
|
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Container = void 0;
|
|
4
|
+
const CircularDependencyError_1 = require("../errors/CircularDependencyError");
|
|
5
|
+
const DependencyNotFoundError_1 = require("../errors/DependencyNotFoundError");
|
|
6
|
+
const InvalidProviderError_1 = require("../errors/InvalidProviderError");
|
|
1
7
|
/**
|
|
2
8
|
* A simple and lightweight dependency injection container.
|
|
3
9
|
*
|
|
4
10
|
* The `Container` class provides methods to register and resolve dependencies using unique tokens.
|
|
5
|
-
* Tokens can be class constructors, strings, or symbols, allowing
|
|
11
|
+
* Tokens can be class constructors, strings, or symbols, allowing flexible and type-safe dependency management.
|
|
6
12
|
*
|
|
7
13
|
* @example
|
|
8
14
|
* // Create a new container instance
|
|
@@ -10,65 +16,157 @@
|
|
|
10
16
|
*
|
|
11
17
|
* // Register a class dependency
|
|
12
18
|
* class MyService {}
|
|
13
|
-
* container.register(MyService,
|
|
19
|
+
* container.register(MyService, MyService, { singleton: true });
|
|
14
20
|
*
|
|
15
21
|
* // Register a value with a string token
|
|
16
|
-
* container.
|
|
22
|
+
* container.registerValue('config', { port: 3000 });
|
|
17
23
|
*
|
|
18
24
|
* // Resolve dependencies
|
|
19
25
|
* const service = container.resolve(MyService);
|
|
20
26
|
* const config = container.resolve<{ port: number }>('config');
|
|
21
27
|
*/
|
|
22
|
-
|
|
28
|
+
class Container {
|
|
23
29
|
constructor() {
|
|
24
30
|
this.registry = new Map();
|
|
25
|
-
|
|
31
|
+
}
|
|
32
|
+
register(token, providerOrClass, options = {}) {
|
|
33
|
+
const provider = this.normalizeProvider(providerOrClass, options);
|
|
34
|
+
this.registry.set(token, { provider });
|
|
35
|
+
}
|
|
36
|
+
registerClass(tokenOrClass, useClassOrOptions, options = {}) {
|
|
37
|
+
var _a;
|
|
38
|
+
if (typeof tokenOrClass === 'function') {
|
|
39
|
+
const inferredToken = tokenOrClass.name;
|
|
40
|
+
this.register(inferredToken, tokenOrClass, (_a = useClassOrOptions) !== null && _a !== void 0 ? _a : {});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
this.register(tokenOrClass, useClassOrOptions, options);
|
|
26
44
|
}
|
|
27
45
|
/**
|
|
28
|
-
* Registers a
|
|
46
|
+
* Registers a value provider.
|
|
29
47
|
*
|
|
30
|
-
* @typeParam T - The type
|
|
31
|
-
* @param token -
|
|
32
|
-
* @param
|
|
33
|
-
* @param options - Optional configuration object.
|
|
34
|
-
* @param options.singleton - If true, the instance will be singleton.
|
|
35
|
-
* @param options.deps - Optional array of tokens for dependencies.
|
|
48
|
+
* @typeParam T - The dependency type.
|
|
49
|
+
* @param token - Unique token associated with the dependency.
|
|
50
|
+
* @param useValue - Value to be returned on resolve.
|
|
36
51
|
*/
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
52
|
+
registerValue(token, useValue) {
|
|
53
|
+
this.register(token, { useValue });
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Registers a factory provider.
|
|
57
|
+
*
|
|
58
|
+
* @typeParam T - The dependency type.
|
|
59
|
+
* @param token - Unique token associated with the dependency.
|
|
60
|
+
* @param useFactory - Factory function that builds the dependency.
|
|
61
|
+
* @param options - Optional configuration.
|
|
62
|
+
* @param options.singleton - Whether to cache a single instance.
|
|
63
|
+
*/
|
|
64
|
+
registerFactory(token, useFactory, options = {}) {
|
|
65
|
+
var _a;
|
|
66
|
+
this.register(token, { useFactory, singleton: (_a = options.singleton) !== null && _a !== void 0 ? _a : false });
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Unregisters a token.
|
|
70
|
+
*
|
|
71
|
+
* @param token - Token to remove from the registry.
|
|
72
|
+
*/
|
|
73
|
+
unregister(token) {
|
|
74
|
+
this.registry.delete(token);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Clears all registrations and cached singletons.
|
|
78
|
+
*/
|
|
79
|
+
clear() {
|
|
80
|
+
this.registry.clear();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Checks if a token is registered.
|
|
84
|
+
*
|
|
85
|
+
* @param token - Token to check.
|
|
86
|
+
* @returns True if the token is registered, otherwise false.
|
|
87
|
+
*/
|
|
88
|
+
has(token) {
|
|
89
|
+
return this.registry.has(token);
|
|
45
90
|
}
|
|
46
91
|
/**
|
|
47
92
|
* Resolves a dependency by its token.
|
|
48
93
|
*
|
|
49
|
-
* @typeParam T -
|
|
50
|
-
* @param token -
|
|
94
|
+
* @typeParam T - Expected dependency type.
|
|
95
|
+
* @param token - Token associated with the dependency.
|
|
51
96
|
* @returns The resolved dependency instance or value.
|
|
52
|
-
* @throws
|
|
97
|
+
* @throws DependencyNotFoundError when the token is not registered.
|
|
98
|
+
* @throws CircularDependencyError when a dependency cycle is detected.
|
|
53
99
|
*/
|
|
54
100
|
resolve(token) {
|
|
101
|
+
return this.resolveInternal(token, []);
|
|
102
|
+
}
|
|
103
|
+
resolveInternal(token, resolving) {
|
|
104
|
+
var _a;
|
|
105
|
+
if (resolving.includes(token)) {
|
|
106
|
+
throw new CircularDependencyError_1.CircularDependencyError([...resolving, token]);
|
|
107
|
+
}
|
|
55
108
|
const entry = this.registry.get(token);
|
|
56
109
|
if (!entry) {
|
|
57
|
-
throw new
|
|
110
|
+
throw new DependencyNotFoundError_1.DependencyNotFoundError(token);
|
|
111
|
+
}
|
|
112
|
+
const provider = entry.provider;
|
|
113
|
+
if (this.isValueProvider(provider)) {
|
|
114
|
+
return provider.useValue;
|
|
115
|
+
}
|
|
116
|
+
if (this.isClassProvider(provider)) {
|
|
117
|
+
if (provider.singleton && entry.value !== undefined) {
|
|
118
|
+
return entry.value;
|
|
119
|
+
}
|
|
120
|
+
const deps = (_a = provider.deps) !== null && _a !== void 0 ? _a : this.getDepsFromClass(provider.useClass);
|
|
121
|
+
const instance = this.createInstance(provider.useClass, deps, [...resolving, token]);
|
|
122
|
+
if (provider.singleton) {
|
|
123
|
+
entry.value = instance;
|
|
124
|
+
}
|
|
125
|
+
return instance;
|
|
58
126
|
}
|
|
59
|
-
if (
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
127
|
+
if (this.isFactoryProvider(provider)) {
|
|
128
|
+
if (provider.singleton && entry.value !== undefined) {
|
|
129
|
+
return entry.value;
|
|
130
|
+
}
|
|
131
|
+
const instance = provider.useFactory(this);
|
|
132
|
+
if (provider.singleton) {
|
|
133
|
+
entry.value = instance;
|
|
65
134
|
}
|
|
66
|
-
return
|
|
135
|
+
return instance;
|
|
67
136
|
}
|
|
68
|
-
|
|
137
|
+
throw new InvalidProviderError_1.InvalidProviderError(token);
|
|
69
138
|
}
|
|
70
|
-
createInstance(ClassRef, deps = []) {
|
|
71
|
-
const resolvedDeps = deps.map(dep => this.
|
|
139
|
+
createInstance(ClassRef, deps = [], resolving = []) {
|
|
140
|
+
const resolvedDeps = deps.map(dep => this.resolveInternal(dep, resolving));
|
|
72
141
|
return new ClassRef(...resolvedDeps);
|
|
73
142
|
}
|
|
143
|
+
normalizeProvider(providerOrClass, options) {
|
|
144
|
+
var _a, _b;
|
|
145
|
+
if (this.isProvider(providerOrClass)) {
|
|
146
|
+
return providerOrClass;
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
useClass: providerOrClass,
|
|
150
|
+
singleton: (_a = options.singleton) !== null && _a !== void 0 ? _a : false,
|
|
151
|
+
deps: (_b = options.deps) !== null && _b !== void 0 ? _b : []
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
isProvider(value) {
|
|
155
|
+
return typeof value === 'object' && value !== null;
|
|
156
|
+
}
|
|
157
|
+
isClassProvider(provider) {
|
|
158
|
+
return 'useClass' in provider;
|
|
159
|
+
}
|
|
160
|
+
isValueProvider(provider) {
|
|
161
|
+
return 'useValue' in provider;
|
|
162
|
+
}
|
|
163
|
+
isFactoryProvider(provider) {
|
|
164
|
+
return 'useFactory' in provider;
|
|
165
|
+
}
|
|
166
|
+
getDepsFromClass(useClass) {
|
|
167
|
+
var _a, _b;
|
|
168
|
+
const classRef = useClass;
|
|
169
|
+
return (_b = (_a = classRef.deps) !== null && _a !== void 0 ? _a : classRef.dependencies) !== null && _b !== void 0 ? _b : [];
|
|
170
|
+
}
|
|
74
171
|
}
|
|
172
|
+
exports.Container = Container;
|
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Logger decorator options.
|
|
3
|
+
*/
|
|
4
|
+
export type LoggerOptions = {
|
|
5
|
+
/**
|
|
6
|
+
* Custom logger function. Defaults to `console.log`.
|
|
7
|
+
*/
|
|
8
|
+
logger?: (message: string, ...args: any[]) => void;
|
|
9
|
+
};
|
|
1
10
|
/**
|
|
2
11
|
* Class decorator that logs instantiation and method calls of the decorated class.
|
|
3
12
|
*
|
|
@@ -5,14 +14,15 @@
|
|
|
5
14
|
* - Log the class name, dependencies (if any), singleton status, and constructor arguments when the class is instantiated.
|
|
6
15
|
* - Intercept all public method calls and log the method name and arguments whenever a method is called.
|
|
7
16
|
*
|
|
8
|
-
* To display dependencies and singleton status, ensure these are set as static properties on the class
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* //
|
|
14
|
-
*
|
|
15
|
-
*
|
|
17
|
+
* To display dependencies and singleton status, ensure these are set as static properties on the class
|
|
18
|
+
* (e.g., `MyClass.deps`, `MyClass.singleton`).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Decorate your class:
|
|
23
|
+
* // Use: "@Logger()"
|
|
24
|
+
* class MyService { ... }
|
|
25
|
+
* ```
|
|
16
26
|
* When instantiated or when myMethod is called, logs will be printed to the console.
|
|
17
27
|
*/
|
|
18
|
-
export declare function Logger(): ClassDecorator;
|
|
28
|
+
export declare function Logger(options?: LoggerOptions): ClassDecorator;
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Logger = Logger;
|
|
1
4
|
/**
|
|
2
5
|
* Class decorator that logs instantiation and method calls of the decorated class.
|
|
3
6
|
*
|
|
@@ -5,35 +8,38 @@
|
|
|
5
8
|
* - Log the class name, dependencies (if any), singleton status, and constructor arguments when the class is instantiated.
|
|
6
9
|
* - Intercept all public method calls and log the method name and arguments whenever a method is called.
|
|
7
10
|
*
|
|
8
|
-
* To display dependencies and singleton status, ensure these are set as static properties on the class
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* //
|
|
14
|
-
*
|
|
15
|
-
*
|
|
11
|
+
* To display dependencies and singleton status, ensure these are set as static properties on the class
|
|
12
|
+
* (e.g., `MyClass.deps`, `MyClass.singleton`).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Decorate your class:
|
|
17
|
+
* // Use: "@Logger()"
|
|
18
|
+
* class MyService { ... }
|
|
19
|
+
* ```
|
|
16
20
|
* When instantiated or when myMethod is called, logs will be printed to the console.
|
|
17
21
|
*/
|
|
18
|
-
|
|
22
|
+
function Logger(options = {}) {
|
|
19
23
|
return (target) => {
|
|
24
|
+
var _a;
|
|
20
25
|
const original = target;
|
|
26
|
+
const log = (_a = options.logger) !== null && _a !== void 0 ? _a : console.log;
|
|
21
27
|
function construct(constructor, args) {
|
|
22
|
-
//
|
|
28
|
+
// Read metadata when available
|
|
23
29
|
const deps = constructor.deps || constructor.dependencies || [];
|
|
24
30
|
const singleton = constructor.singleton || false;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
31
|
+
log(`[Logger] Instanciando: ${constructor.name}`);
|
|
32
|
+
log(`[Logger] Dependências:`, deps.map((d) => (d === null || d === void 0 ? void 0 : d.name) || d));
|
|
33
|
+
log(`[Logger] Singleton:`, singleton);
|
|
34
|
+
log(`[Logger] Parâmetros:`, args);
|
|
29
35
|
const instance = new constructor(...args);
|
|
30
|
-
//
|
|
36
|
+
// Intercept public methods
|
|
31
37
|
const proto = constructor.prototype;
|
|
32
38
|
Object.getOwnPropertyNames(proto).forEach((prop) => {
|
|
33
39
|
if (prop !== 'constructor' && typeof proto[prop] === 'function') {
|
|
34
40
|
const originalMethod = instance[prop];
|
|
35
41
|
instance[prop] = function (...methodArgs) {
|
|
36
|
-
|
|
42
|
+
log(`[Logger] ${constructor.name}.${prop} chamado com:`, methodArgs);
|
|
37
43
|
return originalMethod.apply(this, methodArgs);
|
|
38
44
|
};
|
|
39
45
|
}
|
|
@@ -44,6 +50,21 @@ export function Logger() {
|
|
|
44
50
|
return construct(original, args);
|
|
45
51
|
};
|
|
46
52
|
newConstructor.prototype = original.prototype;
|
|
53
|
+
copyStaticProps(original, newConstructor);
|
|
47
54
|
return newConstructor;
|
|
48
55
|
};
|
|
49
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Copies static properties from the original constructor to the wrapped one.
|
|
59
|
+
*/
|
|
60
|
+
function copyStaticProps(source, target) {
|
|
61
|
+
Object.getOwnPropertyNames(source).forEach((key) => {
|
|
62
|
+
if (key === 'prototype' || key === 'length' || key === 'name') {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const descriptor = Object.getOwnPropertyDescriptor(source, key);
|
|
66
|
+
if (descriptor) {
|
|
67
|
+
Object.defineProperty(target, key, descriptor);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Token } from '../types/Token';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a circular dependency is detected.
|
|
4
|
+
*/
|
|
5
|
+
export declare class CircularDependencyError extends Error {
|
|
6
|
+
readonly path: Token[];
|
|
7
|
+
/**
|
|
8
|
+
* @param path - Dependency resolution path that caused the cycle.
|
|
9
|
+
*/
|
|
10
|
+
constructor(path: Token[]);
|
|
11
|
+
private static formatPath;
|
|
12
|
+
private static formatToken;
|
|
13
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CircularDependencyError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a circular dependency is detected.
|
|
6
|
+
*/
|
|
7
|
+
class CircularDependencyError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* @param path - Dependency resolution path that caused the cycle.
|
|
10
|
+
*/
|
|
11
|
+
constructor(path) {
|
|
12
|
+
super(`Dependência circular detectada: ${CircularDependencyError.formatPath(path)}`);
|
|
13
|
+
this.name = 'CircularDependencyError';
|
|
14
|
+
this.path = path;
|
|
15
|
+
}
|
|
16
|
+
static formatPath(path) {
|
|
17
|
+
return path.map(CircularDependencyError.formatToken).join(' -> ');
|
|
18
|
+
}
|
|
19
|
+
static formatToken(token) {
|
|
20
|
+
var _a;
|
|
21
|
+
if (typeof token === 'string') {
|
|
22
|
+
return token;
|
|
23
|
+
}
|
|
24
|
+
if (typeof token === 'symbol') {
|
|
25
|
+
return token.toString();
|
|
26
|
+
}
|
|
27
|
+
return (_a = token === null || token === void 0 ? void 0 : token.name) !== null && _a !== void 0 ? _a : 'UnknownToken';
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.CircularDependencyError = CircularDependencyError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Token } from '../types/Token';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a dependency token is not registered.
|
|
4
|
+
*/
|
|
5
|
+
export declare class DependencyNotFoundError extends Error {
|
|
6
|
+
readonly token: Token;
|
|
7
|
+
/**
|
|
8
|
+
* @param token - Token that could not be resolved.
|
|
9
|
+
*/
|
|
10
|
+
constructor(token: Token);
|
|
11
|
+
private static formatToken;
|
|
12
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DependencyNotFoundError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a dependency token is not registered.
|
|
6
|
+
*/
|
|
7
|
+
class DependencyNotFoundError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* @param token - Token that could not be resolved.
|
|
10
|
+
*/
|
|
11
|
+
constructor(token) {
|
|
12
|
+
super(`Dependência não registrada: ${DependencyNotFoundError.formatToken(token)}`);
|
|
13
|
+
this.name = 'DependencyNotFoundError';
|
|
14
|
+
this.token = token;
|
|
15
|
+
}
|
|
16
|
+
static formatToken(token) {
|
|
17
|
+
var _a;
|
|
18
|
+
if (typeof token === 'string') {
|
|
19
|
+
return token;
|
|
20
|
+
}
|
|
21
|
+
if (typeof token === 'symbol') {
|
|
22
|
+
return token.toString();
|
|
23
|
+
}
|
|
24
|
+
return (_a = token === null || token === void 0 ? void 0 : token.name) !== null && _a !== void 0 ? _a : 'UnknownToken';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.DependencyNotFoundError = DependencyNotFoundError;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Token } from '../types/Token';
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a token is registered with an invalid provider configuration.
|
|
4
|
+
*/
|
|
5
|
+
export declare class InvalidProviderError extends Error {
|
|
6
|
+
readonly token: Token;
|
|
7
|
+
/**
|
|
8
|
+
* @param token - Token with invalid provider configuration.
|
|
9
|
+
*/
|
|
10
|
+
constructor(token: Token);
|
|
11
|
+
private static formatToken;
|
|
12
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InvalidProviderError = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Error thrown when a token is registered with an invalid provider configuration.
|
|
6
|
+
*/
|
|
7
|
+
class InvalidProviderError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* @param token - Token with invalid provider configuration.
|
|
10
|
+
*/
|
|
11
|
+
constructor(token) {
|
|
12
|
+
super(`Provider inválido para o token: ${InvalidProviderError.formatToken(token)}`);
|
|
13
|
+
this.name = 'InvalidProviderError';
|
|
14
|
+
this.token = token;
|
|
15
|
+
}
|
|
16
|
+
static formatToken(token) {
|
|
17
|
+
var _a;
|
|
18
|
+
if (typeof token === 'string') {
|
|
19
|
+
return token;
|
|
20
|
+
}
|
|
21
|
+
if (typeof token === 'symbol') {
|
|
22
|
+
return token.toString();
|
|
23
|
+
}
|
|
24
|
+
return (_a = token === null || token === void 0 ? void 0 : token.name) !== null && _a !== void 0 ? _a : 'UnknownToken';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.InvalidProviderError = InvalidProviderError;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export {
|
|
1
|
+
export { Container } from './container/Container';
|
|
2
|
+
export { Logger } from './decorators/logger';
|
|
3
|
+
export { CircularDependencyError } from './errors/CircularDependencyError';
|
|
4
|
+
export { DependencyNotFoundError } from './errors/DependencyNotFoundError';
|
|
5
|
+
export { InvalidProviderError } from './errors/InvalidProviderError';
|
|
6
|
+
export type { Container as ContainerType } from './container/Container';
|
|
7
|
+
export type { Logger as LoggerType, LoggerOptions } from './decorators/logger';
|
|
8
|
+
export type { Token, Newable } from './types/Token';
|
|
9
|
+
export type { Provider, ClassProvider, ValueProvider, FactoryProvider } from './types/Provider';
|
package/dist/index.js
CHANGED
|
@@ -1 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InvalidProviderError = exports.DependencyNotFoundError = exports.CircularDependencyError = exports.Logger = exports.Container = void 0;
|
|
4
|
+
var Container_1 = require("./container/Container");
|
|
5
|
+
Object.defineProperty(exports, "Container", { enumerable: true, get: function () { return Container_1.Container; } });
|
|
6
|
+
var logger_1 = require("./decorators/logger");
|
|
7
|
+
Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_1.Logger; } });
|
|
8
|
+
var CircularDependencyError_1 = require("./errors/CircularDependencyError");
|
|
9
|
+
Object.defineProperty(exports, "CircularDependencyError", { enumerable: true, get: function () { return CircularDependencyError_1.CircularDependencyError; } });
|
|
10
|
+
var DependencyNotFoundError_1 = require("./errors/DependencyNotFoundError");
|
|
11
|
+
Object.defineProperty(exports, "DependencyNotFoundError", { enumerable: true, get: function () { return DependencyNotFoundError_1.DependencyNotFoundError; } });
|
|
12
|
+
var InvalidProviderError_1 = require("./errors/InvalidProviderError");
|
|
13
|
+
Object.defineProperty(exports, "InvalidProviderError", { enumerable: true, get: function () { return InvalidProviderError_1.InvalidProviderError; } });
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Container } from '../container/Container';
|
|
2
|
+
import type { Newable, Token } from './Token';
|
|
3
|
+
/**
|
|
4
|
+
* Provider that resolves dependencies by instantiating a class.
|
|
5
|
+
*/
|
|
6
|
+
export type ClassProvider<T = any> = {
|
|
7
|
+
/** Class constructor used to create instances. */
|
|
8
|
+
useClass: Newable<T>;
|
|
9
|
+
/** Optional dependency tokens for constructor injection. */
|
|
10
|
+
deps?: Token[];
|
|
11
|
+
/** Whether to cache a single instance. */
|
|
12
|
+
singleton?: boolean;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Provider that returns a pre-built value.
|
|
16
|
+
*/
|
|
17
|
+
export type ValueProvider<T = any> = {
|
|
18
|
+
useValue: T;
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* Provider that resolves dependencies via a factory function.
|
|
22
|
+
*/
|
|
23
|
+
export type FactoryProvider<T = any> = {
|
|
24
|
+
useFactory: (container: Container) => T;
|
|
25
|
+
/** Whether to cache a single instance. */
|
|
26
|
+
singleton?: boolean;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Union of all supported providers.
|
|
30
|
+
*/
|
|
31
|
+
export type Provider<T = any> = ClassProvider<T> | ValueProvider<T> | FactoryProvider<T>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Constructor signature for class-based providers.
|
|
3
|
+
*/
|
|
4
|
+
export type Newable<T = any> = new (...args: any[]) => T;
|
|
5
|
+
/**
|
|
6
|
+
* Token used to register and resolve dependencies.
|
|
7
|
+
* Can be a class constructor, string, or symbol.
|
|
8
|
+
*/
|
|
9
|
+
export type Token<T = any> = string | symbol | Newable<T>;
|