0.3.0 • Published 6 years ago

cubekit-ioc v0.3.0

Weekly downloads
46
License
MIT
Repository
github
Last release
6 years ago

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.resolve(CartApiUtils)
cartUtils.addItem({ name: 'apple', qty: 1 })
0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.0

7 years ago

0.0.5

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

9 years ago

0.0.1

9 years ago