syrinject 0.0.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 +5 -0
- package/dist/container/Container.d.ts +61 -0
- package/dist/container/Container.js +74 -0
- package/dist/decorators/logger.d.ts +18 -0
- package/dist/decorators/logger.js +49 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/dist/package.json +20 -0
- package/package.json +22 -0
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Represents a unique identifier for a dependency in the container.
|
|
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;
|
|
12
|
+
/**
|
|
13
|
+
* A simple and lightweight dependency injection container.
|
|
14
|
+
*
|
|
15
|
+
* The `Container` class provides methods to register and resolve dependencies using unique tokens.
|
|
16
|
+
* Tokens can be class constructors, strings, or symbols, allowing for flexible and type-safe dependency management.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* // Create a new container instance
|
|
20
|
+
* const container = new Container();
|
|
21
|
+
*
|
|
22
|
+
* // Register a class dependency
|
|
23
|
+
* class MyService {}
|
|
24
|
+
* container.register(MyService, new MyService());
|
|
25
|
+
*
|
|
26
|
+
* // Register a value with a string token
|
|
27
|
+
* container.register('config', { port: 3000 });
|
|
28
|
+
*
|
|
29
|
+
* // Resolve dependencies
|
|
30
|
+
* const service = container.resolve(MyService);
|
|
31
|
+
* const config = container.resolve<{ port: number }>('config');
|
|
32
|
+
*/
|
|
33
|
+
export declare class Container {
|
|
34
|
+
private registry;
|
|
35
|
+
private singletons;
|
|
36
|
+
/**
|
|
37
|
+
* Registers a dependency with a class constructor and optional dependencies.
|
|
38
|
+
*
|
|
39
|
+
* @typeParam T - The type of the dependency.
|
|
40
|
+
* @param token - The unique token to associate with the dependency.
|
|
41
|
+
* @param useClass - The class constructor to use for creating instances.
|
|
42
|
+
* @param options - Optional configuration object.
|
|
43
|
+
* @param options.singleton - If true, the instance will be singleton.
|
|
44
|
+
* @param options.deps - Optional array of tokens for dependencies.
|
|
45
|
+
*/
|
|
46
|
+
register<T>(token: Token<T>, useClass: new (...args: any[]) => T, options?: {
|
|
47
|
+
singleton?: boolean;
|
|
48
|
+
deps?: Token[];
|
|
49
|
+
}): void;
|
|
50
|
+
/**
|
|
51
|
+
* Resolves a dependency by its token.
|
|
52
|
+
*
|
|
53
|
+
* @typeParam T - The expected type of the dependency.
|
|
54
|
+
* @param token - The token associated with the dependency.
|
|
55
|
+
* @returns The resolved dependency instance or value.
|
|
56
|
+
* @throws If the dependency is not found for the given token.
|
|
57
|
+
*/
|
|
58
|
+
resolve<T>(token: Token<T>): T;
|
|
59
|
+
private createInstance;
|
|
60
|
+
}
|
|
61
|
+
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple and lightweight dependency injection container.
|
|
3
|
+
*
|
|
4
|
+
* The `Container` class provides methods to register and resolve dependencies using unique tokens.
|
|
5
|
+
* Tokens can be class constructors, strings, or symbols, allowing for flexible and type-safe dependency management.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Create a new container instance
|
|
9
|
+
* const container = new Container();
|
|
10
|
+
*
|
|
11
|
+
* // Register a class dependency
|
|
12
|
+
* class MyService {}
|
|
13
|
+
* container.register(MyService, new MyService());
|
|
14
|
+
*
|
|
15
|
+
* // Register a value with a string token
|
|
16
|
+
* container.register('config', { port: 3000 });
|
|
17
|
+
*
|
|
18
|
+
* // Resolve dependencies
|
|
19
|
+
* const service = container.resolve(MyService);
|
|
20
|
+
* const config = container.resolve<{ port: number }>('config');
|
|
21
|
+
*/
|
|
22
|
+
export class Container {
|
|
23
|
+
constructor() {
|
|
24
|
+
this.registry = new Map();
|
|
25
|
+
this.singletons = new Map();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Registers a dependency with a class constructor and optional dependencies.
|
|
29
|
+
*
|
|
30
|
+
* @typeParam T - The type of the dependency.
|
|
31
|
+
* @param token - The unique token to associate with the dependency.
|
|
32
|
+
* @param useClass - The class constructor to use for creating instances.
|
|
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.
|
|
36
|
+
*/
|
|
37
|
+
register(token, useClass, options = {}) {
|
|
38
|
+
var _a, _b;
|
|
39
|
+
this.registry.set(token, {
|
|
40
|
+
useClass,
|
|
41
|
+
value: null,
|
|
42
|
+
singleton: (_a = options.singleton) !== null && _a !== void 0 ? _a : false,
|
|
43
|
+
deps: (_b = options.deps) !== null && _b !== void 0 ? _b : []
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolves a dependency by its token.
|
|
48
|
+
*
|
|
49
|
+
* @typeParam T - The expected type of the dependency.
|
|
50
|
+
* @param token - The token associated with the dependency.
|
|
51
|
+
* @returns The resolved dependency instance or value.
|
|
52
|
+
* @throws If the dependency is not found for the given token.
|
|
53
|
+
*/
|
|
54
|
+
resolve(token) {
|
|
55
|
+
const entry = this.registry.get(token);
|
|
56
|
+
if (!entry) {
|
|
57
|
+
throw new Error('Dependência não registrada');
|
|
58
|
+
}
|
|
59
|
+
if (entry.singleton) {
|
|
60
|
+
if (!this.singletons.has(token)) {
|
|
61
|
+
this.singletons.set(token, {
|
|
62
|
+
...entry,
|
|
63
|
+
value: this.createInstance(entry.useClass, entry.deps)
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return this.singletons.get(token).value;
|
|
67
|
+
}
|
|
68
|
+
return this.createInstance(entry.useClass, entry.deps);
|
|
69
|
+
}
|
|
70
|
+
createInstance(ClassRef, deps = []) {
|
|
71
|
+
const resolvedDeps = deps.map(dep => this.resolve(dep));
|
|
72
|
+
return new ClassRef(...resolvedDeps);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class decorator that logs instantiation and method calls of the decorated class.
|
|
3
|
+
*
|
|
4
|
+
* When applied, this decorator will:
|
|
5
|
+
* - Log the class name, dependencies (if any), singleton status, and constructor arguments when the class is instantiated.
|
|
6
|
+
* - Intercept all public method calls and log the method name and arguments whenever a method is called.
|
|
7
|
+
*
|
|
8
|
+
* To display dependencies and singleton status, ensure these are set as static properties on the class (e.g., `MyClass.deps`, `MyClass.singleton`).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Decorate your class:
|
|
13
|
+
* // Use: "@Logger()"
|
|
14
|
+
* class MyService { ... }
|
|
15
|
+
* ```
|
|
16
|
+
* When instantiated or when myMethod is called, logs will be printed to the console.
|
|
17
|
+
*/
|
|
18
|
+
export declare function Logger(): ClassDecorator;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class decorator that logs instantiation and method calls of the decorated class.
|
|
3
|
+
*
|
|
4
|
+
* When applied, this decorator will:
|
|
5
|
+
* - Log the class name, dependencies (if any), singleton status, and constructor arguments when the class is instantiated.
|
|
6
|
+
* - Intercept all public method calls and log the method name and arguments whenever a method is called.
|
|
7
|
+
*
|
|
8
|
+
* To display dependencies and singleton status, ensure these are set as static properties on the class (e.g., `MyClass.deps`, `MyClass.singleton`).
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* // Decorate your class:
|
|
13
|
+
* // Use: "@Logger()"
|
|
14
|
+
* class MyService { ... }
|
|
15
|
+
* ```
|
|
16
|
+
* When instantiated or when myMethod is called, logs will be printed to the console.
|
|
17
|
+
*/
|
|
18
|
+
export function Logger() {
|
|
19
|
+
return (target) => {
|
|
20
|
+
const original = target;
|
|
21
|
+
function construct(constructor, args) {
|
|
22
|
+
// Recupera metadados se existirem
|
|
23
|
+
const deps = constructor.deps || constructor.dependencies || [];
|
|
24
|
+
const singleton = constructor.singleton || false;
|
|
25
|
+
console.log(`[Logger] Instanciando: ${constructor.name}`);
|
|
26
|
+
console.log(`[Logger] Dependências:`, deps.map((d) => (d === null || d === void 0 ? void 0 : d.name) || d));
|
|
27
|
+
console.log(`[Logger] Singleton:`, singleton);
|
|
28
|
+
console.log(`[Logger] Parâmetros:`, args);
|
|
29
|
+
const instance = new constructor(...args);
|
|
30
|
+
// Intercepta métodos públicos
|
|
31
|
+
const proto = constructor.prototype;
|
|
32
|
+
Object.getOwnPropertyNames(proto).forEach((prop) => {
|
|
33
|
+
if (prop !== 'constructor' && typeof proto[prop] === 'function') {
|
|
34
|
+
const originalMethod = instance[prop];
|
|
35
|
+
instance[prop] = function (...methodArgs) {
|
|
36
|
+
console.log(`[Logger] ${constructor.name}.${prop} chamado com:`, methodArgs);
|
|
37
|
+
return originalMethod.apply(this, methodArgs);
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
return instance;
|
|
42
|
+
}
|
|
43
|
+
const newConstructor = function (...args) {
|
|
44
|
+
return construct(original, args);
|
|
45
|
+
};
|
|
46
|
+
newConstructor.prototype = original.prototype;
|
|
47
|
+
return newConstructor;
|
|
48
|
+
};
|
|
49
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "simple-di-container",
|
|
3
|
+
"description": "Simple dependency injection container",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": ["dist"],
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc -p tsconfig.build.json",
|
|
10
|
+
"dev": "tsc -w",
|
|
11
|
+
"prepublishOnly": "npm run build"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "andersonreges",
|
|
15
|
+
"license": "ISC",
|
|
16
|
+
"type": "commonjs",
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"typescript": "^5.9.3"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "syrinject",
|
|
3
|
+
"description": "Simple dependency injection container",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsc -p tsconfig.build.json",
|
|
12
|
+
"dev": "tsc -w",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"author": "andersonreges",
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"type": "commonjs",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"typescript": "^5.9.3"
|
|
21
|
+
}
|
|
22
|
+
}
|