2.0.0 • Published 7 months ago

@beetlabs/stimpack v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

Stimpack 💉

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

Simple dependency injection for TypeScript and React

Dependency injection without the framework, and without reflect-metadata. Just pure TypeScript.

Install

npm install @beetlabs/stimpack

Usage

import { Injectable } from '@beetlabs/stimpack'

@Injectable()
class Dependency {
  test() {
    console.log('yay')
  }
}

const token = new Symbol();
@Injectable({
  token,
  inject: [Dependency]
})
class Service {
  constructor(
    dependency: Dependency
  ) {}

  method() {
    this.dependency.test() // typesafe
  }
}

API

Injectable

function Injectable(options?: {
  token?: symbol,
  inject?: any[]
})

This is where the magic happens. Injectable is a ClassDecorator which registers a service with the injection container. *Note that for now the inject array does need to match the order of the dependencies in the constructor:

@Injectable({
  inject: [Dep2, Dep1]
})
class Service {
  constructor(
    dep1: Dep1,
    dep2: Dep2
  ) {}

  test() {
    dep1.someMethodOnDep1() // this will fail because we injected Dep2 first
  }
}

Container

interface Container {
  instance: Container;
  register(service: any, token?: symbol): void;
  resolve<T>(token?: any | symbol): T
}

Most of the time you won't need to interact directly with Container, but in the case that you are extending this to use with a framework, it's here for you. Container is a singleton with a private constructor, so access it with instance. The other two methods do what they sound like.

2.0.0

7 months ago

1.2.0

7 months ago

1.1.0

7 months ago

1.0.0

7 months ago