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

needle-inject

v1.2.3

Published

Seamless dependency injection for Typescript

Downloads

1

Readme

Needle

npm repo

Seamless dependency injection for Typescript

Installation

From npm:

npm i needle-inject

Usage

Important: When using the decorator, make sure you have the flags emitDecoratorMetadata and experimentalDecorators both set to true in tsconfig.json

{
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}

Any class can be used as a singleton using needle, no extra decorations allowing for backwards compatiblity.

Examples

Usage should be pretty straightforward, we have a service class we want to inject into another class/function. To achieve this we do not have to touch the original class simply make sure it has a constructor that takes no arguments.

class FooService {
    
    procedure(obj : number) : boolean {
        // do complex logic
        return obj % 2 != 0 
    }

    requestThing() : Promise<any> {
        // Send async call
    }
    
}

To use FooService:

  • Using function injection:

    import { provide } from "needle-inject"
    import FooService from "./FooService"
    
    const service = provide(FooService)
    console.log(service.procedure(2))
  • With class injection:

    import { inject } from "needle-inject"
    import FooService from "./FooService"
    
    class Goopus {
          
        @inject
        private fooService : FooService;
          
        constructor(obj : number) {
            console.log(fooService.procedure(obj))
        }
          
    }
    
    let goopus = new Goopus(3)
    console.log(goopus.fooService.procedure(6))
  • Using auto injection:

    import { autoInject, create } from "needle-inject"
    import FooService from "./FooService"
      
    @autoInject
    class Bloopus {
       
      constructor(private fooService : FooService) {
          
      }
       
      evaluate(obj : number) {
          console.log(this.fooService.procedure(obj))
      }
       
    }
      
    let bloopus = create(Bloopus);
    bloopus.evaluate(13)
      

These run in succesion will produce:

false
true
false
true

With React

Here is an example component that uses FooService:

import * as React from "react";
import { inject } from "needle-inject"
import FooService from "./FooService"

export default class MyComponent extends React.Component {

    @inject
    private fooService : FooService;

    render() {
        return <div>
            <Button onClick={this.fooService.requestThing()}>Send Request</Button>
            <p>
                Procedure of 3 is {this.fooService.procedure(3)}
            </p>
        </div>
    }

}

Use case with React & MobX

You can run an example app located in examples/react-mobx. Run npm start and navigate to localhost:1234 to view it.


When managing state in React.js a lot of the time you want a reference to a global store. When using the very useful library MobX they provide a very unopinionated way to handle the passing of stateful objects. Here is where needle becomes useful!

  1. Create a store class that will store state:

    import { observable, computed } from "mobx"
    
    // I store the state for auth sessions
    class AuthStore {
        @observable userToken : String | null;
        @observable userSession : any;
    
        @computed get isLoggedIn() {
            return this.userToken != null;
        }
    }
  2. Create a service that will hold this store:

    import { action } from "mobx";
    
    export default class AuthService {
    
        store: AuthStore;
    
        constructor() {
            store = new AuthStore();
        }
    
        @action
        logIn() {
            // Do complex logic here...
            this.store.userToken = "TOKEN_FROM_AUTH";
        }
    
    }
  3. Now we can inject AuthService, observe that store and any changes to that state will update our components:

    import * as React from "react";
    import { observer } from "mobx-react";
    import { inject } from "needle-inject";
    import AuthService from "./AuthService";
    
    @observe
    export default class MyComponent extends React.Component {
    
        @inject
        private authService : AuthService;
    
        render() {
            return this.authService.isLoggedIn ? <div>I am logged in!</div> : <a href="/login">Login?</a>;
        }
    
    }

Usage with Babel 7

Babel 7 includes it's own Typescript parser but unfortunately, from what I could find, it does not support emitting metadata that is required for the @inject annotation to function.

My suggested workaround is to pre-transpile using tsc (ts-loader with webpack etc.) and then feeding the result into Babel 7 until they support this feature.

Next.js and Babel 7

To mitigate the fact that next.js uses Babel 7 there is an addon for next.config.js bundled. To use needle & typescript with next.js:

const addons = require("needle-inject/dist/addons");
const {withNeedle} = addons.next;

module.exports = withNeedle({})

How it works

There isn't actually much going on under the hood. We keep a singleton InjectionManager which stores all the current mappings. When a class instance is requested we check if we already have one and serve it, otherwise creating a new instance and storing it for next time.

Limitations

  • I've yet to explore what happens when multiple libraries use needle at the same time. The problem I see arisng is two libraries using two impls of the same base class. Let's say they share a common lib with AuthService as an abstract base class yet they both use seperate services that inherit from this. One will clash with the other.

  • More complicated functionality has not yet being needed as part of my requirements but when they arise I fear they will complicate the current minimalist impl. Overthinking it? Probably.