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

ecmascript-ioc

v0.3.4

Published

IoC library mirroring Java Spring Framework's implementation

Downloads

573

Readme

ecmascript-ioc

This is a zero-dependency vanila TypeScript IoC library that mirrors the implementation of the Java Spring Framework IoC. You can use it anywhere: Node.js backend, Electron.js, IoT apps, React/ReactNative, Vue, Iframe embeded widgets, etc...

Features

  • Dependency Injection: Automatic dependency resolution and injection with scopes, circular dependencies handling and lazy initializations.
  • Annotations: Use decorators like @component, @repository, @service, @controller, and @autowired.
  • New ECMAScript Decorators: Use the native TypeScript5.0 decorators without reflect-metadata.

Installation

npm install ecmascript-ioc

Component settings

All dependency definition decorators have a common signature: you define the required component name and optional settings.

type ComponentSettings = {
  lazy: boolean;
  scope: "Singleton" | "Prototype";
};

function component(name: string | symbol, settings?: Partial<ComponentSettings>);
const defaultSettings: ComponentSettings = {
  lazy: false,
  scope: "Singleton";
};

Usage guides

Backend Three-tier architecture example:

import { autowired,
         component,
         repository,
         service,
         controller,
         postConstruct } from 'ecmascript-ioc';


@component(ReportGenerator.di_token, { lazy: true })
class ReportGenerator {
  static readonly di_token = Symbol.for("ReportGenerator");

  public generateTaxReport(username: string): void {
    console.log(`Prepare tax report for user: ${username}.`);
  }
}


@repository("UsersRepository")
class UsersRepository extends Repository {
  public delete(username: string): void {
    console.log(`Delete user: ${username} from DataBase.`);
  }

  @postConstruct
  private warmUpCache(): void {
    console.log('Fetching data from DataBase...');
  }
}


@service("UsersService")
class UsersService implements Service {
  @autowired("UsersRepository")
  private readonly repository!: UsersRepository;

  @autowired(ReportGenerator.di_token)
  private readonly reportGenerator!: ReportGenerator;

  public deleteUser(username: string): void {
    this.repository.delete(username);
    this.reportGenerator.generateTaxReport(username);
  }
}


@controller("UsersController")
class UsersController {
  @autowired("UsersService")
  private readonly service!: UsersService;

  public deleteUser(req: Request, res: Response) {
    this.service.deleteUser(req.query.username);
  }
}

Frontend Three-tier architecture example:

import React from "react";
import { AxiosInstance } from "axios";
import { observable, action } from "mobx";
import { autowired, component, repository, service } from 'ecmascript-ioc';


@component("RestHttpClient")
export class RestHttpClient extends HttpClient {
  protected readonly http: AxiosInstance;

  public delete(url: string): void {
    console.log(`RESTful: ${url}.`);
  }
}


@repository("UsersRepository")
export class UsersRepository extends Repository {
  @autowired("RestHttpClient")
  private readonly http!: RestHttpClient;

  public deleteUser(username: string): void {
    return this.http.delete(`/users?username=${username}`);
  }
}


@service("UsersService", { scope: 'Prototype' })
export class UsersService extends Service {
  @autowired("UsersRepository")
  private readonly repository!: UsersRepository;

  @observable accessor username: string = "";

  @action
  public setUsername(username: string): void {
    this.username = username;
  }

  public deleteUser(): void {
    return this.repository.delete(this.username);
  }
}


export function View(): JSX.Element {
  const service = useDependency<UsersService>("UsersService");
  return <div>Loading..<div/>;
}

React.js hook

import { useMemo } from "react";
import { Container } from "ecmascript-ioc";

export function useDependency<T>(dependencyName: string | symbol): T {
  return useMemo(() => {
    return Container.get<T>(dependencyName);
  }, [dependencyName]);
}