npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@archware/di

v1.1.2

Published

Lightweight dependency injection for TypeScript

Downloads

15

Readme

banner

GitHub Quality Gate Status Maintainability Rating Reliability Rating npm bundle size (scoped)

About

@archware/di is a lightweight inversion of control (IoC) solution for TypeScript projects. It designed to work seamlessly with object oriented architectures. It aims to be flexible, but easy to understand. It can be used in any modern environment, be it browser or Node.

Motivation

One of the strongest advantages of object oriented programming is interfaces. They allow defining strong architectural boundaries, which is the cornerstone of any maintainable piece of software.

Dependency Inversion, Interface Segregation and Open/Closed principle are the SOLID principles this library aims to encourage.

What about InversifyJS?

InversifyJS is a well known IoC solution for TypeScript. Why create another instead of contributing?

There are a few differences that this project deliberately creates:

It puts interfaces first

The goal is to encourage healthy project architecture and established principles. It is enabled by putting interfaces at the center.

It is designed with simplicity in mind.

By making less assumptions and deciding where the flexibility really needs to be, there's less maintenance and almost no learning curve.

It has minimal public API

This guides the user better towards the preferred solution and reduces confusion. Spend less time browsing the docs and more writing the code.

Get started

  1. Install the package:
npm install @archware/di --save
  1. Set tsconfig.json flags:
{
  "compilerOptions": {
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  }
}
  1. Decorate and resolve:
import { Resolvable, Injector } from '@archware/di';

const injector = new Injector();

// 1. Decorate dependency
@Resolvable()
class Engine { }

// 2. Decorate the resolved class
@Resolvable()
class Car {
  constructor(engine: Engine) { }
}

// 3. Resolve
const car = injector.resolve(Car);

Documentation

The full reference page will soon be shared. It will include detailed explanations, complex usage guides and library architecture.

For now please refer to common usecase cheatsheets below:

Resolving a class

import { Resolvable } from '@archware/di';

// 1. Decorate
@Resolvable()
class Car { }

// 2. Resolve
injector.resolve(Car);

Resolving an interface implementation

import { Resolvable } from '@archware/di';
import { asImplementation } from '@archware/di/register';

class Car {
  constructor(engine: Engine) { }
}

// 1. Preserve interface as runtime value
// by declaring an abstract class with same name
interface Engine {
  start(): void;
}
abstract class Engine { }

// 2. Decorate the implementation
@Resolvable()
class V8Engine implements Engine {
  start() { }
}

// 3. Register the implementation
injector.register(asImplementation(Engine, V8Engine));

// 4. Get the car with a V8 engine :)
injector.resolve(Car);

Declaring a class is a singleton

import { Resolvable } from '@archware/di';

@Resolvable({
  singleton: true
})
class Car { }

Resolving a generic value that's not a class

import { Resolvable } from '@archware/di';
import { asValue } from '@archware/di/register';

// 1. Use asValue to correlate some arbitrary token to a value
injector.register(asValue('PI', Math.PI));

// 2. Resolve using the token
const pi = injector.resolve('PI');

Providing a non-class value as a constructor parameter

import { Resolvable, Inject } from '@archware/di';
import { asValue } from '@archware/di/register';

// 1. Use asValue to correlate some arbitrary token to a value
injector.register(asValue('NAME', 'John'));

@Resolvable()
class Person {
  constructor(
    // 2. Pass the token to @Inject()
    @Inject('NAME') public name: string,
  ) { }
}

const person = injector.resolve(Person); // person.name will be 'John'

Overriding an interface implementation

import { Resolvable, Inject } from '@archware/di';
import { asImplementation } from '@archware/di/register';

interface Engine {
  start(): void;
}
abstract class Engine { }

@Resolvable()
class V8Engine implements Engine {
  start() { }
}

@Resolvable()
class ElectricEngine implements Engine {
  start() { }
}

@Resolvable()
class Car {
  constructor(engine: Engine) { }
}

@Resolvable()
class SportsCar {
  constructor(
    // 1. Provide the chosen implementation to @Inject() decorator
    @Inject(V8Engine) engine: Engine
  ) { }
}

// This will be overriden by @Inject()
injector.register(asImplementation(Engine, ElectricEngine));

// 2. Get the car with V8Engine
injector.resolve(SportsCar);

// This will still return a Car with ElectricEngine
injector.resolve(Car);

Support

We encourage you to create an issue if you want to:

  • raise a bug
  • request a feature
  • ask for usage advice
  • post a general question

We'll try out best to find a viable solution.

Contributing

Thanks for considering to contribute! Feel free to drop us a line at [email protected]

Contributing guide will be prepared soon.

Author

Created by archware.io software house

License

MIT, Copyright © 2022 Archware Limited and contributors