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-config-binding

v1.2.1

Published

Package to create bindings automatically for an entire object tree (such as a config object)

Downloads

31

Readme

inversify-config-binding

Lets you bind constants to an InversifyJS container. You can bind a plain object, prototyped class, or es6 class and its sub-properties as constants into an Inversify container.

Supports

  • values
  • property getters
  • inherited values + property getters

Installation

npm install inversify-config-binding

Examples

Given any of these:

Plain object

    const testConfig = {
      otherSettings: {
        c: 1.2,
        d: {
          manyThings: [1, 2, 3]
        }
      },
      settings: {
        a: 1,
        b: 'name'
      }
    };

ES6 class

    class Config {
      get settings() {
        return {
          a: 1,
          b: 'name'
        };
      }
      public otherSettings() {
        /* istanbul ignore next */
        return {
          c: 1.2,
          d: {
            manyThings: [1, 2, 3]
          }
        };
      }
    }
    const configInstance = new Config();

ES6 Class with inheritance

    class GrandparentConfig {
      public get grandparentSettings() {
        return 42;
      }
    }

    class ParentConfig extends GrandparentConfig {
      public get parentSettings() {
        return 13;
      }
    }

    class Config extends ParentConfig {
      get settings() {
        return {
          a: 1,
          b: 'name'
        };
      }

    public otherSettings() {
        /* istanbul ignore next */
        return {
          c: 1.2,
          d: {
            manyThings: [1, 2, 3]
          }
        };
      }
    }
    const configInstance = new Config();

You may inject values by doing the following:

@injectable()
class NeedsConfig {
    public constructor(@inject('CFG.settings') settingsObj: any, @inject('CFG.settings.a') aString: string) {
        // snip
    }
}

// ioc.ts
import { Container } from 'inversify';
import { buildInjectionModule } from 'inversify-config-binding';

const container = new Container();

// -- perform bindings --
// -- end --

container.load(buildInjectionModule(configInstance, { debug: true, prefix: 'CFG' }));

const needsConfig = container.get<NeedsConfig>(NeedsConfig)

IoC creator support

If you have a complicated settings system, you may not want to instantiate your config object by hand. For this case, a second module is provided.

The following may be done:

@config({excludePatterns: [/^x/], prefix: 'CFG2', serviceIdentifier: 'Config2' })
class Config2 {
  get foo() { return 'bar'; }
  get xFoo() { return 'baz'; }
}

Note, the options to the decorator are optional, and the default behavior will bind the class to an identifier of itself. eg: bind(Config2).to(Config2)

You may then load all types decorated in this manner by loading the second type of module:

container.load(buildAutoInjectionModule(container.get.bind(container)));

This module will collect and bind all classes decorated with @config, and it will use the resolver parameter passed to buildAutoInjectionModule() to get an instance. This can be any function which consumes an InversifyJS service identifier and produces the target type, but in practice you would pass the container's get() as above.

Notes and caveats

  • It is somewhat of an anti-pattern to pass the resolver into a module. To ensure this works well for you, make sure to load this module last.
  • Notice the .bind(container). InversifyJS uses this inside the get call, so it is important to bind it. Alternately the following is valid: (serviceIdentifier: interfaces.ServiceIdentifier) => container.get(serviceIdentifier). The arrow function will take care of that parameter.

Configuration

In the above example we pass { debug: true, prefix: 'CFG' }. These are the only two options supported at this time.

The currently supported configuration parameters are defined here:

Debug

If true, logs to stdout some useful info.

example

Binding ".settings" to "CFG.settings"

Prefix

Sets this as the prefix for each binding. Useful to avoid property collision

excludePatterns

Set this to a list of regular expressions. Any property matching one of these will be excluded. Default: /^_/, for matching fields which start with _.