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

di-injectable

v1.1.1

Published

A simple dependency injection library for TypeScript

Downloads

21

Readme

DI-Injectable

A simple Dependency Injection (DI) library for TypeScript supporting Singleton and Transient service lifetimes.

Installation

First, install the package via npm or yarn:


npm install di-injectable

yarn add di-injectable

Usage

Setting Up Services

  • Define Services: Create your service classes and use the @Injectable decorator and use ServiceLifetime enum to register your services as Singleton or Transient..
  • Resolve Services: Use the ServiceProvider to resolve instances of your services.

Example

Let's walk through a complete example.

  1. Define Services Create some simple services and use the @Injectable decorator.
// src/services/logger.ts
import { Injectable } from 'di-injectable'; 

@Injectable(ServiceLifetime.Singleton)
export class Logger {
  log(message: string) {
    console.log(`Logger: ${message}`);
  }
}
// src/services/userService.ts
import { Injectable, Inject } from 'di-injectable';
import { Logger } from './logger';

@Injectable()
export class UserService {
  constructor(@Inject(Logger) private logger: Logger) {}

  getUser() {
    this.logger.log('Getting user...');
    return { id: 1, name: 'John Doe' };
  }
}
  1. Resolve Services Use the ServiceProvider to resolve instances of your services.
// src/app.ts
import { ServiceProvider } from 'di-injectable';
import { UserService } from './services/userService';

const serviceProvider = new ServiceProvider();

const userService = serviceProvider.resolve<UserService>(UserService);
const user = userService.getUser();
console.log(user);

Explanation

  • Defining Services:
    • The Logger service is a simple logger class.
    • The UserService class depends on the Logger service. The @Inject decorator is used to inject the Logger service
    • into the UserService constructor.
  • Registering Services:
    • We register the Logger service as a Singleton, meaning only one instance of Logger will be created and shared.
    • We register the UserService as a Transient by default, meaning a new instance of UserService will be created every time it is resolved.
  • Resolving Services:
    • We create a ServiceProvider instance.
    • We resolve an instance of UserService using the serviceProvider. The UserService will have the Logger instance injected into it due to the @Inject decorator.

Service Lifetimes

  • Singleton: Only one instance of the service is created and shared.
  • Transient: A new instance of the service is created every time it is requested.

API Reference

  • ServiceProvider:
    • resolve<T>(token: any): T: Resolves an instance of the service.
    • Injectable: Decorator to mark a class as injectable as register it.
    • Inject: Decorator to inject dependencies into the constructor.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Steps to Use

  1. Create Your Services: Define your services using the @Injectable decorator and use ServiceLifetime enum to register your services as Singleton or Transient.
  2. Resolve Services: Use ServiceProvider to resolve instances of your services.