dependencies.js 1.0.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/LICENSE +21 -0
- package/README.md +108 -0
- package/index.d.ts +14 -0
- package/index.d.ts.map +1 -0
- package/index.js +43 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aleksei Bespalov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# dependencies.js
|
|
2
|
+
|
|
3
|
+
[](https://github.com/nulldef/dependencies.js/actions/workflows/ci.yml)
|
|
4
|
+
|
|
5
|
+
A lightweight dependency injection container for TypeScript/JavaScript.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Simple decorator-based API (`@Injectable`, `@Inject`)
|
|
10
|
+
- Automatic dependency resolution with singleton caching
|
|
11
|
+
- Circular dependency detection with clear error messages
|
|
12
|
+
- Zero configuration — just decorate and resolve
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install dependencies.js
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { Inject, Injectable, container } from "dependencies.js"
|
|
24
|
+
|
|
25
|
+
@Injectable()
|
|
26
|
+
class Logger {
|
|
27
|
+
log(msg: string) {
|
|
28
|
+
console.log(msg)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@Injectable()
|
|
33
|
+
@Inject(Logger)
|
|
34
|
+
class UserService {
|
|
35
|
+
constructor(public logger: Logger) {}
|
|
36
|
+
|
|
37
|
+
greet(name: string) {
|
|
38
|
+
this.logger.log(`Hello, ${name}!`)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const userService = container.get(UserService)
|
|
43
|
+
userService.greet("World")
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
### `@Injectable()`
|
|
49
|
+
|
|
50
|
+
Registers a class in the default container.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
@Injectable()
|
|
54
|
+
class MyService {}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `@Inject(...deps)`
|
|
58
|
+
|
|
59
|
+
Declares constructor dependencies for a class.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
@Injectable()
|
|
63
|
+
@Inject(Database, Logger)
|
|
64
|
+
class UserRepo {
|
|
65
|
+
constructor(
|
|
66
|
+
public db: Database,
|
|
67
|
+
public logger: Logger,
|
|
68
|
+
) {}
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### `container`
|
|
73
|
+
|
|
74
|
+
The default `Container` instance. Use it to resolve registered classes.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
const service = container.get(UserService)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### `Container`
|
|
81
|
+
|
|
82
|
+
Create isolated containers when needed.
|
|
83
|
+
|
|
84
|
+
```typescript
|
|
85
|
+
import { Container } from "dependencies.js"
|
|
86
|
+
|
|
87
|
+
const c = new Container()
|
|
88
|
+
c.register(MyService)
|
|
89
|
+
const instance = c.get(MyService)
|
|
90
|
+
c.reset() // clears all registrations and cached instances
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
#### Methods
|
|
94
|
+
|
|
95
|
+
| Method | Description |
|
|
96
|
+
| ------------------ | ------------------------------------------------------------ |
|
|
97
|
+
| `get<T>(token)` | Resolves and returns a singleton instance of the given class |
|
|
98
|
+
| `register(target)` | Registers a class in the container |
|
|
99
|
+
| `reset()` | Clears all instances and registrations |
|
|
100
|
+
|
|
101
|
+
## Requirements
|
|
102
|
+
|
|
103
|
+
- TypeScript with decorators enabled
|
|
104
|
+
- `reflect-metadata` (included as a dependency)
|
|
105
|
+
|
|
106
|
+
## License
|
|
107
|
+
|
|
108
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
type Constructor<T = any> = new (...args: any[]) => T;
|
|
3
|
+
export declare class Container {
|
|
4
|
+
private registry;
|
|
5
|
+
private instances;
|
|
6
|
+
get<T>(token: Constructor<T>, resolving?: Set<Constructor<any>>): T;
|
|
7
|
+
register(target: Constructor): void;
|
|
8
|
+
reset(): void;
|
|
9
|
+
}
|
|
10
|
+
export declare const container: Container;
|
|
11
|
+
export declare const Injectable: () => <T extends Constructor>(target: T) => T;
|
|
12
|
+
export declare const Inject: (...deps: Constructor[]) => <T extends Constructor>(target: T) => T;
|
|
13
|
+
export {};
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
package/index.d.ts.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAA;AAEzB,KAAK,WAAW,CAAC,CAAC,GAAG,GAAG,IAAI,KAAK,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,CAAC,CAAA;AAIrD,qBAAa,SAAS;IACpB,OAAO,CAAC,QAAQ,CAAyB;IACzC,OAAO,CAAC,SAAS,CAA8B;IAE/C,GAAG,CAAC,CAAC,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,EAAE,SAAS,wBAAyB,GAAG,CAAC;IA2BpE,QAAQ,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAInC,KAAK,IAAI,IAAI;CAId;AAED,eAAO,MAAM,SAAS,WAAkB,CAAA;AAExC,eAAO,MAAM,UAAU,SAEpB,CAAC,SAAS,WAAW,EAAE,QAAQ,CAAC,KAAG,CAGnC,CAAA;AAEH,eAAO,MAAM,MAAM,GAAI,GAAG,MAAM,WAAW,EAAE,MACnC,CAAC,SAAS,WAAW,EAAE,QAAQ,CAAC,KAAG,CAI5C,CAAA"}
|
package/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
const DEPS_KEY = Symbol.for("di:dependencies");
|
|
3
|
+
export class Container {
|
|
4
|
+
registry = new Set();
|
|
5
|
+
instances = new Map();
|
|
6
|
+
get(token, resolving = new Set()) {
|
|
7
|
+
if (this.instances.has(token)) {
|
|
8
|
+
return this.instances.get(token);
|
|
9
|
+
}
|
|
10
|
+
if (!this.registry.has(token)) {
|
|
11
|
+
throw new Error(`"${token.name}" is not registered. Did you forget @Injectable?`);
|
|
12
|
+
}
|
|
13
|
+
if (resolving.has(token)) {
|
|
14
|
+
const chain = [...resolving, token].map(t => t.name).join(" -> ");
|
|
15
|
+
throw new Error(`Circular dependency detected: ${chain}`);
|
|
16
|
+
}
|
|
17
|
+
resolving.add(token);
|
|
18
|
+
const deps = Reflect.getMetadata(DEPS_KEY, token) ?? [];
|
|
19
|
+
const resolvedDeps = deps.map(dep => this.get(dep, resolving));
|
|
20
|
+
resolving.delete(token);
|
|
21
|
+
const instance = new token(...resolvedDeps);
|
|
22
|
+
this.instances.set(token, instance);
|
|
23
|
+
return instance;
|
|
24
|
+
}
|
|
25
|
+
register(target) {
|
|
26
|
+
this.registry.add(target);
|
|
27
|
+
}
|
|
28
|
+
reset() {
|
|
29
|
+
this.instances.clear();
|
|
30
|
+
this.registry.clear();
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export const container = new Container();
|
|
34
|
+
export const Injectable = () => (target) => {
|
|
35
|
+
container.register(target);
|
|
36
|
+
return target;
|
|
37
|
+
};
|
|
38
|
+
export const Inject = (...deps) => {
|
|
39
|
+
return (target) => {
|
|
40
|
+
Reflect.defineMetadata(DEPS_KEY, deps, target);
|
|
41
|
+
return target;
|
|
42
|
+
};
|
|
43
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dependencies.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Dependencies.js is a JavaScript library that provides a simple and efficient way to manage dependencies in your projects. It allows you to define and resolve dependencies between modules, making it easier to organize and maintain your codebase.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"url": "https://github.com/nulldef/dependencies.js",
|
|
7
|
+
"type": "git"
|
|
8
|
+
},
|
|
9
|
+
"main": "index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./index.d.ts",
|
|
14
|
+
"default": "./index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "Aleksei Bespalov <nulldefiner@gmail.com>",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"reflect-metadata": "^0.2.2"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@trivago/prettier-plugin-sort-imports": "^6.0.2",
|
|
25
|
+
"@types/jest": "^30.0.0",
|
|
26
|
+
"@types/mocha": "^10.0.10",
|
|
27
|
+
"jest": "^30.3.0",
|
|
28
|
+
"prettier": "^3.8.1",
|
|
29
|
+
"ts-jest": "^29.4.9",
|
|
30
|
+
"typescript": "^6.0.2"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "tsc",
|
|
34
|
+
"test": "jest",
|
|
35
|
+
"format": "prettier --write .",
|
|
36
|
+
"format:check": "prettier --check .",
|
|
37
|
+
"cleanup": "rm -rf *.js *.d.ts *.map"
|
|
38
|
+
}
|
|
39
|
+
}
|