1.0.0 • Published 7 years ago

elao-container.js v1.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

container.js

Microscopic dependency injection container

Installation

npm install @elao/container.js

Usage

Given the given class, you want to declare as a service:

// MyApiClient.js
export default class MyApiClient {
    constructor(host, key) {
        this.host = host;
        this.key = key;
    }

    login() {
        // ...
    }
}

Set up your container like that:

// my-container.js
import Container from '@elao/container.js';
import MyApiClient from './MyApiClient';

const container = new Container();

// Register a parameter:
container.registerParameter('api:host', 'my.api.com');
container.registerParameter('api:key', 'xxxxxxxxxxx');

// Register a service:
container.registerDefinition('api', MyApiClient, ['api:host', 'api:key']);

export default container;

Require the api service wherever you need it:

import container from 'my-container.js';

container.get('api').login();