dot package
A lightweight inversion of control framework for JavaScript and TypeScript
Remarks
For a discussion of the project goals, please see Github Discussions.
Example
import { createContainer, injectable, inject, type interfaces } from "@mscharley/dot";
export interface Warrior {
fight: () => string;
sneak: () => string;
}
export interface Weapon {
hit: () => string;
}
export interface ThrowableWeapon {
throw: () => string;
}
const TYPES = {
Warrior: new Token<Warrior>('Warrior'),
Weapon: new Token<Weapon>('Weapon'),
ThrowableWeapon: new Token<ThrowableWeapon>('ThrowableWeapon'),
};
@injectable()
class Katana implements Weapon {
public hit(): string {
return 'cut!';
}
}
@injectable()
class Shuriken implements ThrowableWeapon {
public throw(): string {
return 'hit!';
}
}
@injectable(TYPES.Weapon, TYPES.ThrowableWeapon)
class Ninja implements Warrior {
public constructor(
private readonly katana: Weapon,
private readonly shuriken: ThrowableWeapon,
) {
console.log('constructing:', this.katana, this.shuriken);
}
public fight(): string {
return this.katana.hit();
}
public sneak(): string {
return this.shuriken.throw();
}
}
const myContainer = createContainer();
const demoModule: interfaces.ContainerModule = (bind) => {
bind(TYPES.Warrior).inSingletonScope().to(Ninja);
bind(TYPES.Weapon).to(Katana);
bind(TYPES.ThrowableWeapon).to(Shuriken);
};
myContainer.load(demoModule);
const ninja = await myContainer.get(TYPES.Warrior);
console.log('fight', ninja.fight()); // cut!
console.log('sneak', ninja.sneak()); // hit!
const ninja2 = await myContainer.get(TYPES.Warrior);
console.log('ninja === ninja2:', ninja === ninja2); // true
Classes
Class | Description |
---|---|
Error for operations which aren’t allowed | |
An identifier that specifies bindings should also have metadata attached to them | |
Helper token type for the common case of simple named bindings | |
A recursion was detected while trying to resolve a dependency | |
A simple identifier that has a reference to a type which can be injected | |
Error for wrapping issues with resolving dependencies |
Abstract Classes
Abstract Class | Description |
---|---|
Superclass for all errors that are thrown by the IOC container | |
An error occurred while trying to resolve a request |
Functions
Function | Description |
---|---|
Decorator for classes to flag them as being usable with this library | |
Type guard to check if something is a token that supports metadata | |
Type guard to check if something is a token | |
Helper to provide a simple way to interact with named bindings | |
Turn a service identifier into a string | |
Used to label a constructor parameter as unmanaged by the IOC container. | |
Helper to provide a nicer interface for injecting with options |
Interfaces
Interface | Description |
---|---|
Typesafe definition of a class decorator | |
Typesafe definition of a class field decorator | |
Type for the |
Namespaces
Namespace | Description |
---|---|
Variables
Variable | Description |
---|---|
Create a new empty container | |
Decorator for property injections |
Type Aliases
Type Alias | Description |
---|---|
An easy way to reference any kind of token | |
The error codes used by the IOC container | |
A helper type to extract the type that a token will inject |