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

@asuka/di

Package Overview
Dependencies
Maintainers
2
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@asuka/di

Dependencies inject library

  • 0.3.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
49
increased by32.43%
Maintainers
2
Weekly downloads
 
Created
Source

@asuka/di

Dependencies injection library

Usage

Basic

import { Injectable, InjectableFactory } from '@asuka/di'

@Injectable()
class Engine { }

@Injectable()
class Car {
  constructor(public engine: Engine) { }
}

const car = InjectableFactory.getInstance(Car)
expect(car).to.be.instanceof(Car)
expect(car.engine).to.be.instanceof(Engine)

Value Provider

import { Inject, InjectionToken, Injectable, InjectableFactory, ValueProvider } from '@asuka/di'
import Axios from 'axios'

const token = new InjectionToken<Axios>('Axios client')

const provider: ValueProvider = {
  provide: token,
  useValue: Axios,
}

@Injectable({
  providers: [provider],
})
class HttpClient {
  constructor(@Inject(token) public axios: Axios) { }
}

const client = InjectableFactory.getInstance(HttpClient)
expect(client).to.be.instanceof(HttpClient)
expect(client.axios).to.equal(Axios)

Factory Provider

import { Inject, InjectionToken, Injectable, InjectableFactory, FactoryProvider } from '@asuka/di'
import Axios from 'axios'

const token = new InjectionToken<Axios>('Axios client')
const baseURL = 'https://leetcode-cn.com/api'
const provider: FactoryProvider = {
  provide: token,
  useFactory: () => {
    return Axios.create({
      baseURL,
    })
  },
}

@Injectable({
  providers: [provider],
})
class HttpClient {
  constructor(@Inject(token) public axios: Axios) { }
}

const client = InjectableFactory.getInstance(HttpClient)
expect(client).to.be.instanceof(HttpClient)
expect(client.axios).to.equal(Axios) // baseURL of client.axios is 'https://leetcode-cn.com/api'

Testing

Override Provider by configureModule

function whatever() {
  return true
}

function replacement() {
  return false
}

const token = new InjectionToken<typeof whatever>('replacable')

const provider: ValueProvider = {
  useValue: replacement,
  provide: token,
}

@Injectable({
  providers: [provider],
})
class Service {
  constructor(@Inject(token) public dep: typeof whatever) {}
}

const testModule = Test.createTestingModule({ providers: [{ provide: token, useValue: replacement }] }).compile()
const service = testModule.getInstance(Service)
t.true(service instanceof Service)
t.is(service.dep, replacement)
t.false(service.dep())

Override Provider by overrideProvider method

function whatever() {
  return true
}

function replacement() {
  return false
}

const token = new InjectionToken<typeof whatever>('replacabel')

const provider: ValueProvider = {
  useValue: replacement,
  provide: token,
}

@Injectable({
  providers: [provider],
})
class Service {
  constructor(@Inject(token) public dep: typeof whatever) {}
}

const testModule = Test.createTestingModule()
  .overrideProvider(token)
  .useValue(replacement)
  .compile()
const service = testModule.getInstance(Service)
t.true(service instanceof Service)
t.is(service.dep, replacement)
t.false(service.dep())

Keywords

FAQs

Package last updated on 08 Jan 2020

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