saft

Saft is a lightweight Dependency Injection Framework DI for JS, almost as good as juice but a lot cheaper... It is based on ES2015/ES2017 features such as decorators.

Usage no npm install needed!

<script type="module">
  import saft from 'https://cdn.skypack.dev/saft';
</script>

README

About Saft

Saft is a lightweight Dependency Injection Framework DI for JS, almost as good as juice but a lot cheaper... It is based on ES2015/ES2017 features such as decorators.

Purpose

Allow dynamic extensions to application both in server (node) code as well as client (browserify, webpack) code.

Installation

npm install --save saft

Annotations / Decorators

@Provides

Used with Module classes to create a binding for a function that returns a value

class MyModule {
    @Provides('my-db')
    getDb() {
        return new Db();
    }
}

//Injector know how to create instances of DB with the key 'my-db'
new Injector(new MyModule());

@Singleton

Used to ensure single instance

class MyModule {
    @Provides('my-db')
    @Singleton()
    getDb() {
        return new Db();
    }
}

//Injector now we always get the same instance when @injecting key 'my-db'
new Injector(new MyModule());

@Promises

Used with Module classes to create a binding for a function that instead of the instance returns a promise of the instance. This is used when the value should be injected instead of the promise to the "consuming" function.

class MyModule {
    @Promises('my-db')
    getDb() {
        return Promise.resolve(new Db());
    }
}

//Injector know how to create instances of DB with the key 'my-db'
new Injector(new MyModule());

@Inject

Put on a class to show that in expects things to be injected

class MyModule {
    @Promises('my-db')
    getDb() {
        return Promise.resolve(new Db());
    }
}

@Inject('my-db')
class Consumer {
    constructor(db) {
    }
}
//Injector know how to create instances of DB with the key 'my-db'
new Injector(new MyModule());

Examples

TODO

  • Promise based DI
  • Deploy to NPM
  • Finialize documentation ;)