New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

boxed-injector

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

boxed-injector

Dependency Injection Tools

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

boxed-injector NPM version [Build Status][https://travis-ci.org/giddyinc/boxed-injector] Dependency Status Coverage Status

Dependency Injection Tools

Installation

$ npm install --save boxed-injector

Overview

This is a set of lightweight Dependency Injection components that can be used in both client and server javascript code.

There are 4 components.

  1. Injector (Dependency Injection Container)
  2. Locator (Service Locator, which leverages the DI Container)
  3. Inject (Higher Order Component for resolving dependencies declaratively into React containers) - See separate README
  4. Injectable (Wrapper around React Components for directly injecting resolved props) - See separate README

Installation

$ npm install --save boxed-injector

Usage


'use strict';

const parts = [];

class EngineFactory {
  static get inject() {
    return [
      'Parts'
    ];
  }
  constructor(parts) {
    this.parts = parts;
  }
}

class CarFactory {
  static get inject() {
    return [
      'EngineFactory'
    ];
  }
  constructor(engineFactory) {
    this.engineFactory = engineFactory;
  }
}

// Dependency Injection Container
const Injector = require('boxed-injector').Injector;
let injector = new Injector();

// Register instances
injector.register('Parts', parts);

// Register Factories
injector
  .factory('EngineFactory', EngineFactory)
  .factory('CarFactory', CarFactory);

const carFactory = injector.get('CarFactory');

console.log(carFactory);

// Service Locator - warning - this is a singleton!
const Locator = require('boxed-injector').Locator;
Locator.set(injector);
injector = Locator.get();

const sameCarFactory = injector.get('CarFactory');

console.log(sameCarFactory);
console.log(carFactory === sameCarFactory);

// injector.get('whatever');


Middleware

Middleware functions are executed every time a service is accessed from the container (or on a factory, the first time it's accessed). Global middleware as well as service/factory specific middleware is supported and is executed in the order of registry (FIFO). Note that registered instances are singletons and mutations will affect all consumers. Middleware is synchronous, and is passed an object as follows:

{
  name: 'ExampleService',
  depends: ['ThingItsDependentOn', 'OtherThing'],
  instance: { thing: {}, other: {} }, //fully instantiated instance,
  factory: ExampleService // factory
}

Usage:


  // will console log before getting any instance from the container
  injector.middleware(entity => console.log('before global');
  // will console log 'baz' before getting baz from the container - will always run after global above
  injector.middleware('baz', entity => console.log(entity.name);
  // will console log for any instance, but will run after baz and above global is logged 
  injector.middleware(entity => console.log(`before global again - resolving ${entity.name}`);

  injector.register('baz', result);

  // will console log AFTER getting any instance from the container
  injector.middleware(() => console.log('after global');
  // will console log 'baz' AFTER getting baz from the container - will always run after global above
  injector.middleware('baz', entity => console.log(entity.name);

  injector.get('baz');

  // -> before global
  // -> baz
  // -> before global again
  // instance returned
  // -> baz
  // -> after global  

Contributing

We look forward to seeing your contributions!

License

MIT © Ben Lugavere

Keywords

FAQs

Package last updated on 05 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc