InversifyJS Logger Middleware
inversify-logger-middleware is a specialized middleware designed to integrate with InversifyJS, providing a console-based logger for visualizing the dependency injection resolution plan. It intercepts the binding and resolution process within an InversifyJS container, displaying a detailed tree-like structure of service identifiers, bindings, implementation types, scopes, and target metadata. This tool is invaluable for debugging complex dependency graphs and understanding how InversifyJS resolves dependencies. The current stable version is 3.1.0, which is compatible with `inversify@3.x.x`. Its release cadence is tightly coupled with major InversifyJS releases to ensure compatibility. Key differentiators include its deep integration with InversifyJS internals and its clear, visual representation of the container's resolution logic, which helps developers quickly identify misconfigurations or unexpected dependency flows.
Common errors
-
Error: No matching bindings found for serviceIdentifier: Symbol(Weapon)
cause The logger middleware cannot resolve bindings that are not correctly configured in the InversifyJS container.fixReview your `ContainerModule` or direct `bind` calls to ensure that all requested service identifiers have at least one valid binding. Check for typos in service identifiers or incorrect `when` clauses. -
TypeError: container.applyMiddleware is not a function
cause You might be using an older version of InversifyJS that does not support the `applyMiddleware` method, or `container` is not an instance of `Inversify.Container`.fixUpdate your `inversify` package to a version that supports middleware (e.g., 2.0.0 or higher). Ensure that the `container` variable is indeed a `new Container()` instance.
Warnings
- breaking Version 3.0.0 introduced breaking changes to align with InversifyJS v3.0.0. Ensure your core 'inversify' package is also updated to v3.x.x for compatibility.
- breaking Version 2.0.0 of inversify-logger-middleware is compatible with inversify@2.0.0. Major version bumps typically indicate breaking changes aligning with the core InversifyJS library's API changes.
- gotcha This package requires TypeScript 2.0 or higher for its type definitions. Using an older TypeScript version may result in compilation errors related to missing types or incorrect type inference.
Install
-
npm install inversify-logger-middleware -
yarn add inversify-logger-middleware -
pnpm add inversify-logger-middleware
Imports
- makeLoggerMiddleware
const makeLoggerMiddleware = require('inversify-logger-middleware');import { makeLoggerMiddleware } from 'inversify-logger-middleware'; - Container
import { Container } from 'inversify-logger-middleware';import { Container, ContainerModule } from 'inversify'; - interfaces.Bind
import { Bind } from 'inversify-logger-middleware';import { interfaces } from 'inversify';
Quickstart
import "reflect-metadata";
import { Container, ContainerModule, interfaces } from 'inversify';
import { makeLoggerMiddleware } from 'inversify-logger-middleware';
interface Weapon { attack(): string; }
interface Warrior { fight(): string; sneak(): string; }
class Katana implements Weapon {
public attack() { return "cut!"; }
}
class Shuriken implements Weapon {
public attack() { return "throw!"; }
}
class Samurai implements Warrior {
private _weapon: Weapon;
constructor(@inversify.inject("Weapon") weapon: Weapon) { this._weapon = weapon; }
public fight() { return this._weapon.attack(); }
public sneak() { return ""; }
}
class Ninja implements Warrior {
private _weapon: Weapon;
constructor(@inversify.inject("Weapon") weapon: Weapon) { this._weapon = weapon; }
public fight() { return this._weapon.attack(); }
public sneak() { return "sneak!"; }
}
const container = new Container();
const logger = makeLoggerMiddleware();
container.applyMiddleware(logger);
const warriorModule = new ContainerModule((bind: interfaces.Bind) => {
bind<Weapon>("Weapon").to(Katana).whenInjectedInto(Samurai);
bind<Weapon>("Weapon").to(Shuriken).whenInjectedInto(Ninja);
bind<Warrior>("Warrior").to(Samurai).whenTargetTagged("canSneak", false);
bind<Warrior>("Warrior").to(Ninja).whenTargetTagged("canSneak", true);
});
container.load(warriorModule);
// This will now log the resolution plan to the console
const ninja = container.getTagged<Warrior>("Warrior", "canSneak", true);
console.log(ninja.fight());
console.log(ninja.sneak());
const samurai = container.getTagged<Warrior>("Warrior", "canSneak", false);
console.log(samurai.fight());