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

@plopezm/tsinject

v1.2.6

Published

Dependency injection & cross-cutting concern library. This library provides the necessary tools to implement any application using DI and interceptors.

Downloads

4

Readme

tsinject

Dependency injection & cross-cutting concern library. This library provides the necessary tools to implement any application using DI and interceptors.

Installing

npm install --save @plopezm/tsinject

How to use it

  1. Defining a simple class:
@Injectable
export class UserService {

    users: any = {};

    constructor(){
        this.users["plopez"] = {
            username: "plopez",
            password: "testing"
        }
    }

    get(username: string): any{
        return this.users[username];
    }

    create(user: User): any{
        this.users[user.username] = user;
        return user;
    }

}
  1. Use your singleton
import { UserService } from './services/user.service';
import { Inject } from '@plopezm/tsinject';

export class UserResource {

    @Inject()
    userService: UserService;

    constructor(){        
    }

}

Using an implementation gotten from a factory

Injecting from a factory is really easy. In this case we don't have to register any class in the InjectionFactory. This is due to @Produces("") decorator registers a new singleton using the name included. To use this factory object we have to use @Inject("") with the same name that you used in @Produces("").

import { Inject, Produces } from "@plopezm/tsinject";

export class UserResource {

    // It gets the default singleton registered
    @Inject()
    userService: UserService;

    // It gets the specific singleton created in the named produces method
    @Inject("MyFactoryService")
    userServiceFromFactory: UserService;

    ...
}

export class UserServiceFactory {
    // Creates a new object and registers it in the InjectionFactory automatically using the name included in @Produces
    @Produces("MyFactoryService")
    static produceUserService() {
        var userService = new UserService();
        userService.initialize(example, example2);
        return userService;
    }
}

Implementing Interceptors

Interceptors is the way that we use to implement cross-cutting functionalities. This library provides a way to implement our interceptors for a typescript nodejs application. This library supports multi-interceptor declaration, so you can declare multiple interceptors for a method.

  1. Implementing interceptors
    import { Interceptor, Intercepted, InterceptorComponent, NextInterceptor, InterceptedClass } from "@plopezm/tsinject";

    @Interceptor
    class ExampleInterceptor implements InterceptorComponent{
        invoke(next: NextInterceptor, classIntercepted: InterceptedClass, ...args: any[]): any {

            // Here the real method (or next interceptor) is executed
            // We have to get the result returned by next function.
            // next() could be a call to the real method or a call to the next interceptor
            let result = next(...args);

            // Finally we have to return the value that we want to return ( we can modify it, or args, ...)
            // In this example we are going to modify the original method output adding '[]'
            return `[${result}]`;
        }
    }
  1. Declaring the interceptor in the desired method
    @Intercepted(ExampleInterceptor)
    getNamedHelloString(name: string): string {
        return `Hello Mr ${name}`;
    }
  1. Executing our method
    let obj = new ExampleIntercepted();
    // The output will be '[Hello Mr Pablo]' instead of 'Hello Mr Pablo'
    const result = obj.getNamedHelloString('Pablo');

In addition is it possible to set more than one interceptors for a method. The order of declaration is important because it will detemine the execution order. For example:

    @Intercepted(ExampleInterceptor, ExampleInterceptor2)
    getNamedHelloWithSurnameString2(name: string, surname: string, surname2: string): string {
        return `Hello Mr ${name} ${surname} ${surname2}`;
    }

In this example the interceptor 'ExampleInterceptor' will execute in first place, then 'ExampleInterceptor2' and finally the method 'getNamedHelloWithSurnameString2'