集装箱
TypeScript 的轻量级装潢依赖性注射容器.
pnpm i @half0wl/container
import { Container, BaseService, Service, Inject } from "@half0wl/container";
interface ContainerDeps {
db: DatabaseClient;
logger: Logger;
}
@Service()
class UserService extends BaseService<ContainerDeps> {
findById(id: string) {
return this.deps.db.users.findUnique({ where: { id } });
}
}
@Service()
class AuthService extends BaseService<ContainerDeps> {
@Inject(() => UserService)
declare readonly userService: UserService;
authenticate(token: string) {
return this.userService.findById(tokenToId(token));
}
}
const container = Container.create<ContainerDeps>({ db, logger });
const auth = container.get(AuthService);
要求数 "experimentalDecorators": true 在tsconfig.json(英语:tsconfig.
文档
完整指南和 API 参考 集装箱.lib.ray.cat。 。 。 .
发展
# install dependencies
pnpm install
# build all packages
pnpm -r build
# run tests
pnpm -r test
# lint with biome
pnpm lint
# lint and auto-fix
pnpm lint:fix
# run documentation site locally
pnpm --filter docs dev
# build documentation for production
pnpm --filter docs build
# run the demo
pnpm --filter demo build && pnpm --filter demo start