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

cubekit-ioc

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cubekit-ioc

Cubekit IoC Container

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
decreased by-75%
Maintainers
1
Weekly downloads
 
Created
Source

Cubekit IoC

import { IoCContainer } from 'cubekit-ioc'
import { MetaDecorator } from 'cubekit-meta'


const meta = new MetaDecorator

class Storage {}
class Config {}
class Fetcher {}

// This decorator does not replaces the class, just adds
// a flag to the __cubekitMeta__ static property, so you
// are able to test it without problems.
@meta.singleton()
class Es6Fetcher {
  fetch(...args) {
    return fetch(...args)
  }
}

@meta.singleton()
@meta.types(Fetcher, Storage, Config)
class Api {
  
  constructor(fetcher, storage, config) {
    this.fetcher = fetcher
    this.storage = storage
    this.config = config
  }
  
  request(path, body) {
    const url = `${this.config.baseUrl}/${path}`
    const sessionKey = this.storage.getItem('sessionKey')
    return this.fetcher.fetch(url, {
      body,
      headers: { 'X-Session': sessionKey },
    })
  }
}

@meta.singleton()
@meta.types(Api)
class CartApiUtils {
  constructor(api) {
    this.api = api
  }
  
  addItem(item) {
    return this.api.request('cart/add-item', { item })
  }
}

const ioc = new IoCContainer

// Bind some specific class to an "interface"
ioc.bind(Fetcher, Es6Fetcher)

// Define singleton and set its instance at the same time
ioc.instance(Storage, localStorage)
ioc.instance(Config, {
  baseUrl: 'http://example.com/api/v1',
})

// Instantiate CartApiUtils. All deps will be recursively resolved by the ioc
const cartUtils = ioc.make(CartApiUtils)
cartUtils.addItem({ name: 'apple', qty: 1 })

FAQs

Package last updated on 28 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