A lightweight dependency injection library for Node.js and TypeScript.
npm install @injets/core
import { Container } from '@injets/core';
class MyService {
constructor() {
console.log('MyService constructed');
}
method() {
console.log('MyService method');
}
}
const container = new Container();
container.provide({
token: 'myService',
useClass: MyService,
})
const myService = container.resolve('myService');
Creates a new container instance.
Registers a provider in the container.
Resolves a provider from the container.
Resolves multiple providers from the container.
Imports all exported providers from another container.
Exports a provider from the container.
Exists many types of providers:
Provide a instance of a class.
interface ClassProvider<T> {
token: Token<T>;
useClass: Class<T>;
mode?: ProviderMode;
}
Provide a value of a factory.
interface FactoryProvider<T> {
token: Token<T>;
useFactory: Factory<T>;
mode?: ProviderMode;
}
Provide a value.
interface ValueProvider<T> {
token: Token<T>;
useValue: T;
mode?: ProviderMode;
}
Options for the container.
interface ContainerOptions {
isGlobal?: boolean;
imports?: Container[];
providers?: Provider[];
exports?: Token[];
}
Mode of the provider.
enum ProviderMode {
SINGLETON = 'SINGLETON',
TRANSIENT = 'TRANSIENT',
}
A provider can be a class, a factory or a value.
type Provider<T> = ClassProvider<T> | FactoryProvider<T> | ValueProvider<T>;
A token is a unique identifier for a provider.
type Token<T = any> = string | symbol | Class<T>;
- @injets/functional - Support for functional programming in JavaScript and TypeScript.
- @injets/decorators - Support for decorators in TypeScript.