@tecp/di 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/README.md +92 -0
- package/package.json +40 -0
package/README.md
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# @tecp/di
|
|
2
|
+
|
|
3
|
+
A lightweight, zero-dependency Dependency Injection container for TypeScript.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @tecp/di
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Decorator registration
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { di, singleton, transient } from '@tecp/di';
|
|
17
|
+
|
|
18
|
+
@singleton()
|
|
19
|
+
class DbService {
|
|
20
|
+
query(sql: string) {
|
|
21
|
+
return `result of ${sql}`;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@transient()
|
|
26
|
+
class RequestContext {
|
|
27
|
+
id = Math.random();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const a = di(DbService);
|
|
31
|
+
const b = di(DbService);
|
|
32
|
+
console.log(a === b); // true
|
|
33
|
+
|
|
34
|
+
const c = di(RequestContext);
|
|
35
|
+
const d = di(RequestContext);
|
|
36
|
+
console.log(c === d); // false
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Manual registration
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { di, regSingleton, regTransient } from '@tecp/di';
|
|
43
|
+
|
|
44
|
+
class Logger {
|
|
45
|
+
log(msg: string) {
|
|
46
|
+
console.log(msg);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
regSingleton(Logger);
|
|
51
|
+
const l1 = di(Logger);
|
|
52
|
+
const l2 = di(Logger);
|
|
53
|
+
console.log(l1 === l2); // true
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### Value and factory providers
|
|
57
|
+
|
|
58
|
+
```ts
|
|
59
|
+
import { di, regSingleton, regTransient } from '@tecp/di';
|
|
60
|
+
|
|
61
|
+
const CONFIG = Symbol('config');
|
|
62
|
+
|
|
63
|
+
regSingleton({ token: CONFIG, useValue: { db: 'sqlite' } });
|
|
64
|
+
console.log(di(CONFIG)); // { db: 'sqlite' }
|
|
65
|
+
|
|
66
|
+
let n = 0;
|
|
67
|
+
const COUNTER = Symbol('counter');
|
|
68
|
+
regTransient({ token: COUNTER, useFactory: () => ++n });
|
|
69
|
+
console.log(di(COUNTER)); // 1
|
|
70
|
+
console.log(di(COUNTER)); // 2
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Testing
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { di, regSingleton, clearRegistry } from '@tecp/di';
|
|
77
|
+
|
|
78
|
+
clearRegistry(); // reset all registrations
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## API
|
|
82
|
+
|
|
83
|
+
| Function | Description |
|
|
84
|
+
|---|---|
|
|
85
|
+
| `regSingleton(provider)` | Register a singleton provider |
|
|
86
|
+
| `regTransient(provider)` | Register a transient provider |
|
|
87
|
+
| `di(token)` | Resolve a token to its instance |
|
|
88
|
+
| `singleton()` | Decorator — registers class as singleton |
|
|
89
|
+
| `transient()` | Decorator — registers class as transient |
|
|
90
|
+
| `clearRegistry()` | Reset all registrations |
|
|
91
|
+
|
|
92
|
+
A provider can be a class constructor or a `FullProvider` object with `token`, `useClass`, `useValue`, `useFactory`, and `isSingleton` fields.
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tecp/di",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"devEngines": {
|
|
9
|
+
"packageManager": {
|
|
10
|
+
"name": "pnpm",
|
|
11
|
+
"version": "^11.0.6",
|
|
12
|
+
"onFail": "download"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"type": "module",
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@biomejs/biome": "2.4.15",
|
|
18
|
+
"@types/node": "25.9.1",
|
|
19
|
+
"tsx": "4.22.3",
|
|
20
|
+
"typescript": "6.0.3"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./package.json": "./package.json"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist"
|
|
32
|
+
],
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"module": "./dist/index.js",
|
|
36
|
+
"scripts": {
|
|
37
|
+
"build": "rm -rf dist tsconfig.tsbuildinfo && tsc -p tsconfig.build.json",
|
|
38
|
+
"test": "node --import tsx --test \"./**/*.test.ts\""
|
|
39
|
+
}
|
|
40
|
+
}
|