Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

autoinject

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

autoinject

Autoinject classes

  • 0.6.9
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Auto inject

Auto dependency injector for typescript.

This is a proof of concept project, it may not be stable.

Install
$ npm install autoinject
Usage
import {autoInject, dependencyInjection} from 'autoinject';

class User {
    test = 'It works fine';
}

@autoInject
class Db {

    user: User;

    constructor(user: User) {
        this.user = user;
    }
}

@autoInject
class MyClass {

    db: Db;

    constructor(db: Db) {
        this.db = db;
    }
}

const myClass = dependencyInjection(MyClass);

console.log(myClass.db.user.test);

All dependencies are stored on a static prototype on the target object.

import {autoInject} from 'autoinject';
import {Db} from 'db';

@autoInject
class Controller {
    constructor(db: Db) {}
}

console.log(Controller.inject); // [ Db ]

You can also auto instantiate a class

import {autoInject, autoInstantiate} from 'autoinject';

class User {
    test = 'It works fine';
}

@autoInjecte
class Db {
    constructor(public user: User) {}
}

@autoInstantiate
class MyClass {
    constructor(public db?: Db) {}
}

var k = new MyClass();

console.log(k.db.user.test); // output: "It works fine"

You can always override the inject (for testing purpose)

// http.ts
class Http {
    get(url: string) {
        // some logic
    }
}
export {Http};

// image-repository.ts
import {autoInject} from 'autoinject';
import {Http} from './http';

@autoInjecte
class ImageRepository {
    http: Http;

    constructor(http: Http) {
        this.http = http;
    }

    getImage() {
        return this.http.get('/image');
    }
}
export {ImageRepository};

// test.ts
import {dependencyInjection} from 'autoinject';
import {ImageRepository} from './image-repository';

const httpMock = {
    get() {
        return 'Nice';
    }
};
ImageRepository.inject = [httpMock];

const imageRepository = dependencyInjection(ImageRepository);

console.assert(imageRepository.getImage(), 'Nice');

Include the typing with:

"moduleResolution": "node"

in your tsconfig.json

Keywords

FAQs

Package last updated on 03 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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc