Giter VIP home page Giter VIP logo

injex's Introduction

Injex Injex License Node Version TypeScript Version

Simple, Decorated, Pluggable dependency-injection framework for TypeScript applications

Injex makes software architecture more easy & fun by creating a dependency tree between your application modules with a minimal API.

Installation

Start by installing the core package. This package includes most of the functionality you're going to use when working with the Injex framework.

npm install --save @injex/core

After the core is installed and based on your project, you need to install a runtime container. The runtime container enables modules definition and registration across your application.

You can currently choose between the Node, Webpack or Vite runtimes for the server or the client.

Node Runtime

Create a dependency-injection container inside a Node.JS application.

npm install --save @injex/node

Webpack Runtime

Create a dependency-injection container inside a Webpack client-side application.

npm install --save @injex/webpack

Vite Runtime

Create a dependency-injection container inside a Vite client-side application.

npm install --save @injex/vite

Getting Started

Basic Usage

Create an Injex Node runtime container.

import { Injex } from "@injex/node";

Injex.create({
    rootDirs: [
        "./src"
    ]
}).bootstrap()

Injex will scan all the files and folders recursively and look for Injex modules.

Module definition example:

// src/services/mailService.ts
import { define, singleton, inject } from "@injex/core";

@define()
@singleton()
export class MailService {
    @inject() private mailProvider: IMailProvider;

    public sendMail(mail: Mail) {
        this.mailProvider.send(mail);
    }
}

Since Injex automatically scans all the files and folders inside the rootDirs, this is all you need to do to create an injectable module.

Learn more >>

Plugins

Injex is pluggable, so you can use and create your plugins to enrich your applications.

📦 Env Plugin - Manage environment variables across your application. Docs →

📦 Express Plugin - Use Injex to power up your Express application by creating controllers, better route handlers, and middlewares. Docs →

📦 React Plugin - Use React hooks to inject dependencies into components. Docs →

Learn more about Injex plugins and the plugin anatomy.

Live Example

Checkout this live chat application built with Injex & Injex Plugins using React and Mobx

Follow Us

Follow us on Twitter or join our live Discord server for more help, ideas, and discussions.

Author

twitter/uditalias
Udi Talias

License

This repository is available under the MIT License.


injex's People

Contributors

dependabot[bot] avatar lgtm-com[bot] avatar roeibh avatar uditalias avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

injex's Issues

Question/Help needed: Basic setup

Hi @uditalias,
First of all, great work, I really like the idea of auto-scanning all the classes/modules annotated with @define.

I've created this small demo https://github.com/flowck/injex-demo where I attempt to implement injex/node, but I can't make it work, and the run() method from the bootstrap class doesn't quite run. I probably made a basic mistake somewhere but I can't quite figure it out.

When I try to run the application after being compiled, I get this error:

Screenshot 2021-03-15 at 21 55 22

Sorry for this issue.

Kind regards,
Firmino.

Feature: Add the container instance as a dependency of itself

There should be an option to inject the container into itself, so a module can require it using the @inject() decorator.

For example:

@define()
export class MyModule {
    // injecting the runtime container
    @inject() private $injex: Injex;

    public addItem(item, name) {
        // using the runtime container API
        this.$injex.addObject(item, name);
    }
}

Today, when creating a runtime container, its reference is not accessible from the ecosystem of Injex.

Parameter Decorator alternative for `@inject`

Is your feature request related to a problem? Please describe.

Wondering if it were possible in this library to do parameter dependency injection into the constructor instead of property dependency injection. Various articles seem to be against the latter though I am learning DI at the moment so I have no idea what the correct answer is.

Describe the solution you'd like

I have not tested this, but I ended up writing a small wrapper to implement this.

// src/lib/runtime/injex.ts
export const injectParam = (shadowedKey: string, dependencyNameOrType?: any) => {
  return function (target: any, _key: string | symbol, _index: number) {
    // _key is `undefined` in the constructor, I think, and _index doesn't
    // seem to be useful in guessing the value of `shadowedKey`
    inject(dependencyNameOrType)(target.prototype, shadowedKey);
  };
}

// Usage
import { Dependency } from "somewhere/in/my/project"

export class SomeService {
  constructor(
    // Here `dep` is required as I couldn't find a way to 
    // get the name of the paramter in the constructor using 
    // `reflect-metadata`
    @injectParam("dep", Dependency) private dep: Dependency) {}
  
  public someMethod() {
    const someResult = this.dep.someResource();
    // ...
  }
}

It's not great as two function parameters in the decorator go unused, but I'm sure someone more qualified can come up with a solution to that - maybe even detect the parameter names in the constructor.

Describe alternatives you've considered
InversifyJS includes the desired functionality with the caveat that it requires explicit registration of dependencies which I am too lazy to consider (I am only building a small app at the moment, no need for overkill, methinks).

Additional context
What prompted this feature request:

image

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.