npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

injection-js

v2.4.0

Published

Dependency Injection library for JavaScript and TypeScript

Downloads

2,378,624

Readme

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.

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

  • ng-packagr Transpile your libraries to Angular Package Format. Part of the official Angular CLI.
  • @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) 🖖
  • rxstack RxStack is a realtime object-oriented framework which helps you build a micro service web applications on top of other frameworks like express and socketio by adding an abstraction layer.
  • ServeRX-ts Experimental Node.js HTTP framework using RxJS, built with TypeScript and optimized for serverless deployments. Features declarative routes and dependency injection powered by injection-js.

License

MIT