Lightweight decorator-based dependency injection container for TypeScript.
pnpm i @half0wl/containerimport { 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);Requires "experimentalDecorators": true in tsconfig.json.
Full guide and API reference at container.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