Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@microgamma/digator

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@microgamma/digator

Handcrafted DI container

  • 2.1.0
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-50%
Maintainers
1
Weekly downloads
 
Created
Source

@microgamma/digator npm version

install

npm i -S @microgamma/digator or yarn add @microgamma/digator

How to use

Decorate a class with @Injectable().

// my-class.ts


import { Injectable } from '@microgamma/digator';

@Injectable()
class MyClass {

  public sayHello(name: string) {
    return `Hello ${name}`;
  }
}

Inject it using @Inject(<Class>)

// consumer.ts

import { Inject } from '@microgamma/digator';

class Consumer {

  constructor(
    @Inject(MyClass) public myClassSingleton: MyClass
  ) {
    this.myClassSingleton.sayHello('consumer');
  }

}

Getting a singleton programmatically

// my-script.ts

const singleton = injector(MyClass);

Create the app


@DI({
  providers: [
    MyClass,
  ]
})
class App {
}

const app = bootstrap(App);

Providing custom implementation

providers array also accept an object such as:

@DI({
  providers: [
    {
      provide: MyClass,
      useClass: MyCustomImplementation,
    },
  ]
})
class App {
}

const app = bootstrap(App);

Testing

In order to test a singleton and mock dependencies use TestBed

import { Inject } from '@microgamma/digator';


describe('MyClass', () => {

  @Injectable()
  class HttpService {
  }

  @Injectable()
  class TestClass {
    constructor(
      @Inject(HttpService) http: HttpService,
    ) {
    }
  }

  let instance;

  beforeEach(() => {
    TestBed.configure({
      providers: [
        TestClass,
        {
          provide: HttpService,
          useClass: class {
            // test implementation
          }
        }
      ]
    });

    instance = TestBed.inject(TestClass);
  });

  it('should exist', () => {
    expect(instance).toBeTruthy();
  });
});

Mocked

Only if jest is used the Mocked utility can be used to automatically mock any given class.

import { Inject } from '@microgamma/digator';


describe('MyClass', () => {

  @Injectable()
  class HttpService {
  }

  @Injectable()
  class TestClass {
    constructor(
      @Inject(HttpService) http: HttpService,
    ) {
    }
  }

  let instance;

  beforeEach(() => {
    TestBed.configure({
      providers: [
        TestClass,
        {
          provide: HttpService,
          useClass: Mocked(HttpService)
        }
      ]
    });

    instance = TestBed.inject(TestClass);
  });

  it('should exist', () => {
    expect(instance).toBeTruthy();
  });
});

for more info look at its tests

DEBUG

Prepend DEBUG=microgamma:digator* to your script to see debugging information.

Keywords

FAQs

Package last updated on 17 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc