@bulatlib/di
v0.0.5
Published
A simple dependency injection container
Downloads
53
Maintainers
Readme
@bulatlib/di — A simple dependency injection container
Install
npm i @bulatlib/di
# or
pnpm add @bulatlib/di
# or
bun add @bulatlib/diQuick start
import { p, init } from '@bulatlib/di';
const createApi = () => {
return {
ping: () => 'pong',
};
};
const createService = (api: { ping: () => string } = di.api.get()) => {
return {
run: () => api.ping(),
};
};
const di = init(
{
api: p(() => createApi()),
service: p(() => createService()),
},
{
onBind: (name) => console.log('binding:', name),
},
);
// binding: di.service
// binding: di.api
console.log(di.service.get().run());
// pongOverride in tests
You can rebind any provider before the first .get():
let di = init({ api: p(() => new Api()) });
di.api.bind(() => new MockApi());
const api = di.api.get();API
p(() => T): create a provider ofT.init(deps, { initialKey = 'di', onBind? })→ returns initializeddeps.Provider.bind(builder?): bind with optional custom factory.Provider.get(): get a singleton instance.
Notes:
- All providers are singletons by default.
- No decorators or
reflect-metadataare required.
