Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

di-helper

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

di-helper

A concise JavaScript dependency injector

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

di-helper

npm

A concise JavaScript dependency injector

import {provides, using} from 'di-helper'

provides('logger')(() => {
  return {logger: console.log}
})

using('logger')((logger) => {
  logger('The logger has been injected')
})

Usage

Install

yarn add di-helper

Import

di-heper is built into UMD package, which supports both ESM, CommonJS, etc..

// Default instance and its methods
import context, {provides, using} from 'di-helper'
const {default: context, provides, using} = require('di-helper')

// Context class
import {Context} from 'di-helper'
const {Context} = require('di-helper')

// Utility functions
import {compose} from 'di-helper'
const {compose} = require('di-helper')

Methods

provide(modules: object)

Simply adds modules to the current context. Each key of the modules argument becomes the key for resolve().

context.provide({
  logger: console.log,
})

register(key: string, provider: () => (any | Promise<any>))

Registers provider to the current context.

  • providers argument is a function that returns (eventually) module that corresponds to the key.
  • key argument becomes the key for resolve().
context.register('logger', () => {
  return console.log
})

resolve(key: string): Promise<any>

Resolves the module that has been provided with the key. The modules are cached once resolved.

context.resolve('logger').then((logger) => {
  logger('The logger has been resolved')
})

resolveAll(pattern?): Promise

Executes all providers that matches the pattern and cache the results not resolved yet.

  • When no arguments given, all providers are matched.
  • If pattern is string: Test if the provider's key euqals.
  • If pattern is string[]: Test if the provider's key is included.
  • If pattern is (key: string) => boolean: Test if true is returned when the provider's key is given.
  • If pattern is RegExp: Test the provider's key by the given expression.
context.resolveAll()

Annotation Factories

di-helper has been designed to support only high-order function and method/property decorator.

provides(...keys: string[])

Registers a target as a provider for the keys.

If a provider returns the modules, all its keys should be listed in the decorator.

Otherwise, a provider can return undefined or {}. In this case, resolve() for the keys listed only executes the provider.

context.provides('logger')(() => {
  return {logger: console.log}
})
class Logger {

  // ...

  @provides('logger')
  static provide() {
    return {logger: new Logger()}
  }
}

using(...keys: string[])

Wraps a target and inject the modules for the keys. The resolved modules are prepended to its own arguments when the wrapped function gets called. The resulting function wrapped by this decorator returns Promise eventually resolved into the original return value.

context.using('logger')((logger) => {
  logger('The logger has been injected')
})
class UserModel {

  // ...

  async update(id, user) {
    return updateUser(id, user)
      .then(() => this.log(`Updated user ${id}`))
      .catch(() => this.log(`Error occurred updating user ${id}`))
  }

  @using('logger')
  log(logger, message) {
    const time = new Date().toDateString()
    logger(`[${time}] ${message}`)
  }
}

FAQs

Package last updated on 03 Apr 2018

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