Naughty Pinching Mannequins

    @stringke/sigi-di
    TypeScript icon, indicating that this package has built-in type declarations

    2.11.3-alpha.1 • Public • Published

    @stringke/sigi-di

    Sigi documents

    Dependencies injection library

    Usage

    Basic

    import { Injectable, InjectableFactory } from '@stringke/sigi-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 '@stringke/sigi-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 '@stringke/sigi-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>('replaceable')
    
    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>('replaceable')
    
    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

    Install

    npm i @stringke/sigi-di

    Homepage

    sigi.how

    DownloadsWeekly Downloads

    3

    Version

    2.11.3-alpha.1

    License

    MIT

    Unpacked Size

    99.7 kB

    Total Files

    63

    Last publish

    Collaborators

    • stringke