Sign In

injectly

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

injectly

Lightweight dependency injector

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
5
25%
Maintainers
1
Weekly downloads
 
Created
Source

Injectly

Injectly is a lightweight dependency injector based on Angular 2's dependency injection system.

Similar to angular, Injectly uses providers to store different types of values into the DI container. Injectly supports classes, factory functions and arbitrary values.

Injector API

bindProvider(provider: Provider);
bind(name: string, dependencies: string[], config?: IInjectableConfig);
resolve<T>(name: string): T;
inject(dependencyNames: string[], callback: any;

Values

import {Injector} from 'injectly';

let injector = new Injector();

injector.bindProvider(new Provider('Value', {
    useValue: "Hello World"
}));

injector.bindProvider(new Provider('function', {
    useValue: () => { return 5; }
}));

Factories


import {Provider} from 'injectly';

injector.bindProvider(new Provider('LogFactory', {
    dependencies: ['Value'],
    useFactory: (value) => {
        console.log(`LogFactory: ${value}`); // Hello World
        return value;
    }
}));

Classes


import {Injectable} from 'injectly';

// Using decorators

@injector.bind('A', [])
class A {
    constructor() {
        console.log('A was instantiated');
    }
}

// Create singletons by specifiying useExisting
@injector.bind('B', ['A'], { useExisting: true })
class B {
    constructor(a: A) {
        console.log('Test was instantiated with', A);
    }
}

// Without decorators

class A {
    constructor() {
        console.log('A was instantiated');
    }
}

injector.bindProvider(new Provider('A', {
    useClass: A
}));

class B {
    constructor(a: A) {
        console.log('Test was instantiated with', A);
    }
}

injector.bindProvider(new Provider('B', {
    dependencies: ['A'],
    useExisting: B
}));

Retrieving


// Get dependency from container
let B = injector.resolve<B>('B');

// Inject dependencies into a callback
injector.inject(['A', 'B'], (a, b) => {
    console.log(a); // A { }
    console.log(b); // B { }
});

Keywords

Dependency

FAQs

Package last updated on 22 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts