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

inversify-generator

v0.1.5

Published

Inversifyjs type identifiers and bindings generator

Downloads

17

Readme

Inversify Generator

Inversify it's a great inversion of control container for TypeScript. But due to the nature of TypeScript and the inability to infer types at runtime Inversify and every other IoC for TypeScript relies on identifiers to assign an implementation to an abstraction.

This packages tries to make the process of assigning an identifier to an implementation a little less cumbersome by generating type identifiers and bindings for inversify automatically.

Install

yarn install inversify-generator

Usage

First you must configure the generator settings.

There are two ways, through command flags or through a inversify-generator.json config file.

Options

| Option | Description | Default | | ---------- | ------------------------------------------------------------------------ | ----------------- | | tsconfig | Path to the tsconfig file | ./tsconfig.json | | output | Path to the output directory | ./src/ioc | | binding | Type of binding applied to theimplementations in the generated file | default | | watch | Watch files for binding and types generation | false |

Type inversify-generator -h to see a list of available flags and how to use them.

inversify-generator.json config file example:

{
  "tsconfig": "./tsconfig.json",
  "output": "./src/ioc",
  "binding": "default"
}

Binding type

One of the configuration options it's binding. This configuration has two possibilities: default or dynamic.

The default will generate the bindings in a simple manner directly to the container as stated in inversify documentation:

Example of generated file with this binding:

import { Container } from "inversify";
import { TYPES } from "./types";
import { Ninja } from "./entities/ninja";
import { Katana } from "./entities/katana";
import { Shuriken } from "./entities/shuriken";

const myContainer = new Container();
myContainer.bind(TYPES.Warrior).to(Ninja);
myContainer.bind(TYPES.Weapon).to(Katana);
myContainer.bind(TYPES.ThrowableWeapon).to(Shuriken);

export { myContainer };

On the other hand the dynamic type will use a util exposed by this package that binds implementations with dynamic imports adding the possibility of creating providers (asynchronous factories).

If you are using inversify in a front end app, this method will probably make your main bundle smaller (as long as the sections that load the async factories are not executed) because it will only download the corresponding dependency chunks when needed.

This would be an example of the generated file by the dynamic binding type:

import { Container } from "inversify";
import { TYPES } from "./types";
import { bindDynamicModule } from "inversify-generator/utils";

const myContainer = new Container();
bindDynamicModule(TYPES.Warrior, () => import("./entities/ninja"), locator.bind);
bindDynamicModule(TYPES.Weapon, () => import("./entities/katana"), locator.bind);
bindDynamicModule(TYPES.ThrowableWeapon, () => import("./entities/shuriken"), locator.bind);

export { myContainer };

IMPORTANT Using dynamic binding has the current limitation that the function only binds the first exported module of the file. Meaning, if you have more than one class exported in the file and decorated with @injectable only the first one will be bound. So if you are using the dynamic type try having one class implementation per file.

Decorator util

This package also has a decorator util that will let you handle per dependency both scope, binding type and type identifier custom name.

By default, Inversify when binding a dependency it does it with the transient scope. With this util you will be able to configure the scope per dependency to be either transient or singleton.

Also, this decorator adds the ability to configure the binding type per dependency so that if you have one default binding configured (through flag or config file) you can select another one to a specific class.

Regarding the type identifier name, this util lets you change it from the one from default (that it's corresponding implementation, or it's class name if it does not implement anything).

import { generatorConf } from "inversify-generator/decorators";

@injectable()
@generatorConf({ scope: "singleton", biding: "default", typeName: "CoolRepo" })
export class FooRepository implements IFooRepository {
  @inject(TYPES.Service) private serviceProvider!: IocProvider<IService>;

  async baz(): Promise<Page<Post>> {
    const service = await this.serviceProvider();
    const baz = await service.get<Array<Record<string, unknown>>>("/baz");
    return baz.data;
  }
}

Roadmap

  • [ ] Add typedoc
  • [ ] Add request scope for configuration decorator.
  • [ ] Add support for modules
  • [ ] Add tests
  • [ ] Add possibility to have several exported modules in a file to use with the dynamic binding method.