@sukujs/di 0.0.0-stage → 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/LICENSE +21 -0
- package/README.md +85 -2
- package/dist/index.cjs +847 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +325 -0
- package/dist/index.d.mts +325 -0
- package/dist/index.mjs +811 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Suku Authors
|
|
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
CHANGED
|
@@ -1,3 +1,86 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sukujs/di
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-dependency dependency injection for TypeScript with .NET-style lifetimes.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { Service, Inject, InjectionToken, ServiceCollection } from '@sukujs/di';
|
|
7
|
+
|
|
8
|
+
abstract class Cache {
|
|
9
|
+
abstract get(k: string): unknown;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
@Service()
|
|
13
|
+
class RedisCache extends Cache {
|
|
14
|
+
get(k: string) {
|
|
15
|
+
/* ... */
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const CONFIG = new InjectionToken<{ port: number }>('CONFIG');
|
|
20
|
+
|
|
21
|
+
@Service()
|
|
22
|
+
class Server {
|
|
23
|
+
constructor(
|
|
24
|
+
private cache: Cache,
|
|
25
|
+
@Inject(CONFIG) private cfg: { port: number },
|
|
26
|
+
) {}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const root = new ServiceCollection()
|
|
30
|
+
.addSingleton(Cache, RedisCache)
|
|
31
|
+
.addSingleton(CONFIG, { useValue: { port: 3000 } })
|
|
32
|
+
.addScoped(Server)
|
|
33
|
+
.build({ validateOnBuild: true });
|
|
34
|
+
|
|
35
|
+
const scope = root.createScope();
|
|
36
|
+
scope.get(Server);
|
|
37
|
+
await scope.dispose();
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
## Without a class
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
const Clock = service('clock', () => ({ now: () => new Date() }));
|
|
44
|
+
const Invoices = service('invoices', {
|
|
45
|
+
lifetime: 'scoped',
|
|
46
|
+
services: { clock: Clock },
|
|
47
|
+
create: ({ clock }) => ({ dueAt: () => clock.now() }),
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
new ServiceCollection().addRange([Clock, Invoices]).build().createScope().get(Invoices);
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
`@Service()` is only needed on classes whose constructor takes parameters; a `static inject = [...]`
|
|
54
|
+
array is the decorator-free alternative.
|
|
55
|
+
|
|
56
|
+
## Lifetimes
|
|
57
|
+
|
|
58
|
+
| Lifetime | Instances |
|
|
59
|
+
| ----------- | ------------------------------------------------------- |
|
|
60
|
+
| `singleton` | one per root provider |
|
|
61
|
+
| `scoped` | one per `createScope()`; resolving from the root throws |
|
|
62
|
+
| `transient` | one per resolve |
|
|
63
|
+
|
|
64
|
+
## Compiler settings
|
|
65
|
+
|
|
66
|
+
Constructor injection by type needs TypeScript's legacy decorators:
|
|
67
|
+
|
|
68
|
+
```json
|
|
69
|
+
{ "experimentalDecorators": true, "emitDecoratorMetadata": true }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Interfaces do not exist at runtime. Use an abstract class as the token, or an
|
|
73
|
+
`InjectionToken` with `@Inject(TOKEN)`.
|
|
74
|
+
|
|
75
|
+
## Validation
|
|
76
|
+
|
|
77
|
+
- `validateScopes` (default on): a singleton that pulls in a scoped service throws.
|
|
78
|
+
- `validateOnBuild` (default off): missing registrations, cycles, captive dependencies and
|
|
79
|
+
unresolvable constructor parameters fail at `build()` instead of at first use.
|
|
80
|
+
|
|
81
|
+
## Disposal
|
|
82
|
+
|
|
83
|
+
Instances created by the container that expose `dispose()`, `onDispose()`,
|
|
84
|
+
`[Symbol.dispose]` or `[Symbol.asyncDispose]` are disposed, newest first, when their
|
|
85
|
+
owning provider is disposed. `useValue` instances are never disposed. Providers support
|
|
86
|
+
`await using`.
|