GithubHelp home page GithubHelp logo

arkadiuszsz / injection-js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mgechev/injection-js

0.0 1.0 0.0 78 KB

Dependency injection library for JavaScript and TypeScript in 5.1K. It is an extraction of the Angular's ReflectiveInjector which means that it's well designed, feature complete, fast, reliable and well tested.

TypeScript 99.21% JavaScript 0.79%

injection-js's Introduction

Build Status Downloads

Dependency Injection

Dependency injection library for JavaScript and TypeScript in 5.2K. It is an extraction of the Angular's dependency injection which means that it's feature complete, fast, reliable and well tested.

Up-to-date with Angular 4.1.

Why not Angular version 5 and above?

Angular version 5 deprecated the ReflectiveInjector API and introduced StaticInjector. In short, the dependency injection in the newest versions of Angular will happen entirely compile-time so reflection will not be necessary.

However, if you want to use dependency injection in your Node.js, Vue, React, Vanilla JS, TypeScript, etc. application you won't be able to take advantage of StaticInjector the way that Angular will because your application won't be compatible with Angular compiler.

This means that if you need dependency injection outside of Angular @angular/core is not an option. In such case, use injection-js for fast, small, reliable, high-quality, well designed and well tested solution.

How to use?

$ npm i injection-js
# OR
$ yarn add injection-js

Note:

For ES5 Class syntax and TypeScript you need a polyfill for the Reflect API. You can use:

Also for TypeScript you will need to enable experimentalDecorators and emitDecoratorMetadata flags within your tsconfig.json

TypeScript

import 'reflect-metadata';
import { ReflectiveInjector, Injectable, Injector } from 'injection-js';

class Http {}

@Injectable()
class Service {
  constructor(private http: Http) {}
}

@Injectable()
class Service2 {
  constructor(private injector: Injector) {}

  getService(): void {
    console.log(this.injector.get(Service) instanceof Service);
  }

  createChildInjector(): void {
    const childInjector = ReflectiveInjector.resolveAndCreate([Service], this.injector);
  }
}

const injector = ReflectiveInjector.resolveAndCreate([Service, Http]);

console.log(injector.get(Service) instanceof Service);

ES6

const { Inject, ReflectiveInjector } = require('injection-js');

class Http {}

class Service {
  static get parameters() {
    return [new Inject(Http)];
  }

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

const injector = ReflectiveInjector.resolveAndCreate([Http, Service]);

console.log(injector.get(Service) instanceof Service);

ES5

require('reflect-metadata');
var di = require('injection-js');

var Http = di.Class({
  constructor: function() {},
});

var Service = di.Class({
  constructor: [
    Http,
    function(http) {
      this.http = http;
    },
  ],
});

var injector = di.ReflectiveInjector.resolveAndCreate([Http, Service]);

console.log(injector.get(Service) instanceof Service);

API

For full documentation check Angular DI docs:

Ecosystem

This is a list of libraries that are using injection-js. If you have a suggestion on what to add, please don't hesitate to submit a PR.

Libraries

  • @martin_hotell/axios-http Injectable axios HttpClient wrapper for browser and node
  • @martin_hotell/rea-di Dependency injection for React done right. Hierarchical injection on both component and service layer powered by injection-js (Angular DI framework) ๐Ÿ––

License

MIT

injection-js's People

Contributors

amcdnl avatar bisubus avatar galtalmor avatar hotell avatar mgechev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.