sloth-ts-injection
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

sloth-ts-injection

Build Status Coverage Status

Sloth Dependency Injection For Typescript

Installation

You can get the latest release and the type definitions using npm:

$ npm install sloth-ts-injection reflect-metadata --save

/!\ sloth-ts-injection requires TypeScript >= 2.0, your tsconfig should look similar to the one below (experimentalDecorators, emitDecoratorMetadata are important)

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6"],
        "types": ["reflect-metadata"],
        "module": "commonjs",
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true
    }
}

Getting Started

You can declare dependencies with @slothInject() decorator.

import { slothInject } from 'sloth-ts-injection';

@slothInject()
export class DependencyA {
    public data: number = 0;
}

You can then require this dependency.

import { slothInject } from 'sloth-ts-injection';
import { DependencyA } from "./dependencyA";

@slothInject()
export class DependencyD {

    constructor(public a: DependencyA   ) { }

    public update() {
        this.a.data++;
    }
}

sloth-ts-injection will resolve the dependencies needed for you

import { Injector } from 'sloth-ts-injection';

const injector = new Injector();
const dep: DependencyD = injector.inject(DependencyD);

dep.update();

Readme

Keywords

none

Package Sidebar

Install

npm i sloth-ts-injection

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

77 kB

Total Files

22

Last publish

Collaborators

  • imoverlord