@half0wl/container 1.0.1 → 1.0.2
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 +85 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
# 📦 container
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@half0wl/container)
|
|
4
|
+
[](https://github.com/half0wl/container-dev/actions/workflows/ci.yml)
|
|
5
|
+
[](https://container.lib.ray.cat)
|
|
6
|
+
|
|
7
|
+
Lightweight decorator-based dependency injection container for TypeScript.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm i @half0wl/container
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { Container, BaseService, Service, Inject } from "@half0wl/container";
|
|
15
|
+
|
|
16
|
+
interface ContainerDeps {
|
|
17
|
+
db: DatabaseClient;
|
|
18
|
+
logger: Logger;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@Service()
|
|
22
|
+
class UserService extends BaseService<ContainerDeps> {
|
|
23
|
+
findById(id: string) {
|
|
24
|
+
return this.deps.db.users.findUnique({ where: { id } });
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
@Service()
|
|
29
|
+
class AuthService extends BaseService<ContainerDeps> {
|
|
30
|
+
@Inject(() => UserService)
|
|
31
|
+
declare readonly userService: UserService;
|
|
32
|
+
|
|
33
|
+
authenticate(token: string) {
|
|
34
|
+
return this.userService.findById(tokenToId(token));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const container = Container.create<ContainerDeps>({ db, logger });
|
|
39
|
+
const auth = container.get(AuthService);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Requires `"experimentalDecorators": true` in tsconfig.json.
|
|
43
|
+
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
Full guide and API reference at **[container.lib.ray.cat](https://container.lib.ray.cat)**.
|
|
47
|
+
|
|
48
|
+
- [Getting Started](https://container.lib.ray.cat/guide/getting-started)
|
|
49
|
+
- [Services](https://container.lib.ray.cat/guide/services)
|
|
50
|
+
- [Dependency Injection](https://container.lib.ray.cat/guide/injection)
|
|
51
|
+
- [Tracing](https://container.lib.ray.cat/guide/tracing)
|
|
52
|
+
- [Testing](https://container.lib.ray.cat/guide/testing)
|
|
53
|
+
- [API Reference](https://container.lib.ray.cat/api/)
|
|
54
|
+
|
|
55
|
+
## Development
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
# install dependencies
|
|
59
|
+
pnpm install
|
|
60
|
+
|
|
61
|
+
# build all packages
|
|
62
|
+
pnpm -r build
|
|
63
|
+
|
|
64
|
+
# run tests
|
|
65
|
+
pnpm -r test
|
|
66
|
+
|
|
67
|
+
# lint with biome
|
|
68
|
+
pnpm lint
|
|
69
|
+
|
|
70
|
+
# lint and auto-fix
|
|
71
|
+
pnpm lint:fix
|
|
72
|
+
|
|
73
|
+
# run documentation site locally
|
|
74
|
+
pnpm --filter docs dev
|
|
75
|
+
|
|
76
|
+
# build documentation for production
|
|
77
|
+
pnpm --filter docs build
|
|
78
|
+
|
|
79
|
+
# run the demo
|
|
80
|
+
pnpm --filter demo build && pnpm --filter demo start
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
[MIT](./LICENSE)
|