This is a lightweight and flexible dependency injection (DI) container library for JavaScript and TypeScript applications. It provides a simple yet powerful way to manage and resolve dependencies within your projects.
You can install the package via npm or yarn:
pnpm add @guarapi/di-container
# or
npm install @guarapi/di-container
# or
yarn add @guarapi/di-container
First, you need to create symbols to act as keys for your dependencies using the createDependencyKey
function:
import { createDependencyKey } from '@guarapi/di-container';
const myDependencyKey = createDependencyKey<string>('myDependency');
Then, you can set dependencies in the container using the set
method:
import Container from '@guarapi/di-container';
const container = new Container()
.set(myDependencyKey, () => 'Dependency value');
Finally, you can retrieve dependencies from the container using the get
method:
const dependencyValue = container.get(myDependencyKey);
console.log(dependencyValue); // Output: Dependency value
Contributions are welcome! Please feel free to submit any issues or pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.