Skip to content

Simple dependency injector which work in IE and doesn't use Proxy

Notifications You must be signed in to change notification settings

nissejokke/dependency-injector

Repository files navigation

Dependency Injector

Simple dependency injector for javascript/typescript which works in IE and doesn't use Proxy.

Inspired by Awilix.

Examples

import { DependencyInjector, asValue, asClass, asFunction } from './dependency-injector';

const di = new DependencyInjector();
di.register({
    db: asValue('localhost'),
});
const db = di.resolve('db'); 
// db == localhost
const di = new DependencyInjector();
di.register({
    db: asValue('localhost'),
    service: asClass(
        class Service {
            db: string;

            constructor(db: string) {
                this.db = db;
            }
        }
    ),
});
const service = di.resolve('service') as { db: string };
// service.db == localhost
const di = new DependencyInjector();
di.register({
    id: asValue(1),
    service: asFunction((id: number) => id * 2),
});
const service = di.resolve('service') as number;
// service == 2

Dependencies will be instantiated every time they are resolved. See test for more examples.

Restrictions

  • Class and function names can't be mangled, they must be preserved

API

DependencyInjector

register()

Registers dependencies as values (asValue), function (asFunction) or classes (asClass)

resolve()

Resolves dependency

resolveDependency()

Resolves dependency but returns DepencencyType result

createScope()

Creates new DepencencyInjector instance from current instance

deps

Dependecies dictionary

About

Simple dependency injector which work in IE and doesn't use Proxy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published