@byteshift/injector

A tiny dependency injection library

Usage no npm install needed!

<script type="module">
  import byteshiftInjector from 'https://cdn.skypack.dev/@byteshift/injector';
</script>

README

Byteshift Injector is a tiny Dependency Injection library designed to be used with TypeScript. It utilizes the reflect-metadata package to automatically inject singleton instances of 'services' into a property of any class upon instantiation.

Install through NPM:

$ npm i @byteshift/injector

or through Yarn:

$ yarn add @byteshift/injector

Define a service using the @Service decorator:

import {Service} from '@byteshift/injector';

@Service
export class DatabaseService
{
    public query(sql: string): any;
}

Inject the service using the @Inject decorator on a property.

export class RegistrationForm
{
    @Inject private readonly db: DatabaseService;

    constructor()
    {
        // Access it as you would expect it to work.
        this.db.query('SELECT * FROM ...');
    }
}