Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including inversify-token with all npm packages installed. Try it out:

require("inversify/package.json"); // inversify is a peer dependency. var inversifyToken = require("inversify-token")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

inversify-token v5.1.0

Token-based injection for InversifyJS

inversify-token

npm CircleCI Greenkeeper badge

Source: https://github.com/mscharley/inversify-token
Author: Matthew Scharley
Contributors: See contributors on GitHub
Bugs/Support: Github Issues
Copyright: 2018
License: MIT license
Status: Active

Synopsis

This library provides simpler, more type-safe options for injecting values using Inversify.

Installation

$ npm i inversify-token

Usage

Modified from the InversifyJS readme.

// Declare your interfaces as normal.

// file types.ts
import { Token, TokenType } from "inversify-token";
import { Warrior, Weapon, ThrowableWeapon } from "./interfaces"

const WarriorToken = new Token<Warrior>(Symbol.for("Warrior"));
type WarriorToken = TokenType<typeof WarriorToken>;
const WeaponToken = new Token<Weapon>(Symbol.for("Weapon"));
type WeaponToken = TokenType<typeof WeaponToken>;
const ThrowableWeaponToken = new Token<ThrowableWeapon>(Symbol.for("ThrowableWeapon"));
type ThrowableWeaponToken = TokenType<typeof ThrowableWeaponToken>;

export {
    WarriorToken as Warrior,
    WeaponToken as Weapon,
    ThrowableWeaponToken as ThrowableWeapon,
}

// file entities.ts
import { injectable } from "inversify";
import { injectToken } from "inversify-token";
import * as TYPES from "./types";

@injectable()
class Ninja implements Warrior {

    public constructor(
        @injectToken(TYPES.Weapon) private _katana: TYPES.Weapon,
        @injectToken(TYPES.ThrowableWeapon) private _shuriken: TYPES.ThrowableWeapon,
    ) { }

    public fight() { return this._katana.hit(); }
    public sneak() { return this._shuriken.throw(); }

}

// file inversify.config.ts
import { getToken, tokenBinder } from "inversify-token";

const myContainer = new Container();
const bindToken = tokenBinder(myContainer.bind.bind(myContainer));
bindToken(TYPES.Warrior).to(Ninja);
bindToken(TYPES.Weapon).to(Katana);
bindToken(TYPES.ThrowableWeapon).to(Shuriken);
const warrior = getToken(container, TYPES.Warrior);

// file inversify.module.ts
import { getToken, TokenContainerModule } from "inversify-token";

const myContainer = new Container();
const module = new TokenContainerModule((bindToken) => {
    bindToken(TYPES.Warrior).to(Ninja);
    bindToken(TYPES.Weapon).to(Katana);
    bindToken(TYPES.ThrowableWeapon).to(Shuriken);
});
myContainer.load(module);
const warrior = getToken(container, TYPES.Warrior);
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free