Skip to content

half0wl/container

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📦 container

github npm docs ci

Lightweight decorator-based dependency injection container for 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);

Requires "experimentalDecorators": true in tsconfig.json.

Documentation

Full guide and API reference at container.lib.ray.cat.

Development

# 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

License

MIT

About

📦 Lightweight decorator-based dependency injection container for TypeScript.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors