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

@codecapers/fusion

v1.0.15

Published

A simple automated dependency injection library for TypeScript, supporting React class and functional compoents.

Downloads

134

Readme

Fusion

A simple automated dependency injection library for TypeScript, supporting React class and functional components.

Learn more about Fusion in this blog post:

If you like this project, please star this repo and support my work

Aims

  • To have a simple dependency injection library with minimal configuration that can be used in TypeScript code and with React.

Features

  • Less than 400 lines of code (used to be 300, but you know how it goes, I keep adding extra stuff)
  • Configuration via TypeScript decorators.
  • Injects properties into generic TypeScript class.
  • Injects properties into React class components.
  • Injects parameters into React functional components.
    • Unfortuntely decorators can't be applied to global functions (seems like a big thing missing from TypeScript??) - so the injection approach for functional components doesn't use decorators.
  • Automated dependency injection.
    • Just add mark up and let Fusion do the wiring for you.
  • Detects and breaks circular references (with an error) at any level of nesting.
    • But only when NODE_ENV is not set to "production" (to make it fast in production).
  • Unit tested.

Examples

See the examples sub-directory in this repo for runnable Node.js and React examples.

Read the individual readme files for instructions.

There's also a separate repo with separate examples for React class components and functional components.

Usage

First enable decorators in your tsconfig.json file:

"experimentalDecorators": true

Install the Fusion library:

npm install --save @codecapers/fusion

Import the bits you need:

import { InjectProperty, InjectableClass, InjectableSingleton, injectable } from "@codecapers/fusion";

Create dependencies

Create dependencies that can be injected:

log.ts

//
// Interface to the logging service.
//
interface ILog {
    info(msg: string): void;
}

//
// This is a lazily injected singleton that's constructed when it's injected.
//
@InjectableSingleton("ILog")
class Log implements ILog {
    info(msg: string): void {
        console.log(msg);
    }
}

Note: if you can't get over the magic string, please skip to the last section!

Inject properties into classes

Mark up your class to have dependencies injected:

my-class.ts

import { InjectProperty, InjectableClass } from "@codecapers/fusion";
import { ILog } from "./log";

@InjectableClass()
class MyClass {

    //
    // Injects the logging service into this property.
    //
    @InjectProperty("ILog")
    log!: ILog;

    myFunction() {
        //
        // Use the injected logging service.
        // By the time we get to this code path the logging service 
        // has been automatically constructed and injected.
        //
        this.log.info("Hello world!");
    }

    // ... Other functions and other stuff ...
}

Now instance your injectable class:

import { MyClass } from "./my-class";

// The logging singleton is lazily created at this point.
const myObject = new MyClass(); 

Injected properties are solved during construction and available for use after the consturctor has returned.

So after your class is constructed you can call functions that rely on injected properties:

myObject.myFunction();

Inject parameters into functions

This can be used for injection into React functional components.

Create a functional component that needs dependencies:

my-component.jsx

import React from "react";
import { injectable } from "@codecapers/fusion";

function myComponent(props, context, dependency1, dependency2) {

    // Setup the component, use your dependencies...

    return (
        <div>
            // ... Your JSX goes here ...
        </div>;
    );
}

Wrap your functional component in the injectable higher order component (HOC):

my-component.jsx (extended)

export default injectable(myComponent, ["IDependency1", "IDependency2"]);

The exported component will have the dependencies injected as parameters in the order specified (after props and context of course).

Getting rid of the magic strings

I like to get rid of the magic string by using constants co-located with the dependencies:

log.ts

const ILog_id = "ILog";

//
// Interface to the logging service.
//
interface ILog {
    info(msg: string): void;
}

//
// This is a lazily injected singleton that's constructed when it's injected.
//
@InjectableSingleton(ILog_id)
class Log implements ILog {
    info(msg: string): void {
        console.log(msg);
    }
}

Then use the constant to identify your dependencies:

my-class.ts

@InjectableClass()
class MyClass {

    //
    // Injects the logging service into this property.
    //
    @InjectProperty(ILog_id)
    log!: ILog;

    // ... Other properties and methods ...
}

Have fun! There's more to it than this of course, but getting started is that simple.

See the blog post to learn more.