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

@riotbyte/nest-service-locator

v0.2.0

Published

NestJS tag based service locator

Downloads

205

Readme

NestJS Service Locator

The service locator package allow you to define "service tags". Service tags can be used to create a collection of services that can be retreived using a service locator. This can be helpful when implementing certain design patterns (e.g. strategy), where a service uses a collection of different implementations.

Defining a tag

To define a new tag, you need to call the defineTag function:

import { defineTag } from '@riotbyte/nest-service-locator'

export const MyTag = defineTag('my-tag-name')

NOTE: You can define a tag with the same name, mulitple times. Keep in mind that each individual instance is a different tag, and tags with the same name, will not conflict with eachother.

This tag can now be applied using the decorator method on the defined tag:

@MyTag.decorator({})
class MyTaggedClass {}

NOTE: The empty object passed to the decorator function is used to supply the tag with metadata. By default, metadata can be an empty object, but you can specify the type of metadata for each tag induvidually.

Providing metadata

Tags can provide additional information about the class that it has been applied to. You can define the type of the metadata when defining a tag:

export const MyTag = defineTag<{ name: string }>('my-tag-name')

This example now requires an object with the name property when applying the tag:

@MyTag.decorator({ name: 'my-tagged-class' })
class MyTaggedClass {}

Restricting the tag

By default, the tag can be applied to any class. If a tag is expected to only be applied to a specific interface or type, you can define a tag like this:

import { defineTag } from '@riotbyte/nest-service-locator'

export const MyTag = defineTag<object, MyInterface>('my-tag-name')

Now the compiler will show an error when the class the tag is being applied to is not an instance of MyInterface:

// This is allowed since X is a valid implementation of MyInterface
@MyTag.decorator({})
class X implements MyInterface {
  myInterfacedFunction() {}
}

// This is not allowed since Y is not a valid implementation of MyInterface
@MyTag.decorator({})
class Y {}

Retreiving tagged services

Tagged services and their metadata can be retrieved using the service locator:

import { ServiceLocator, TaggedService } from '@riotbyte/nest-service-locator'

class GroupGreeter {
  constructor(private readonly locator: ServiceLocator) {}

  greet(greeterName: string): void {
    this.locator
      .tagged(GreeterTag)
      .find((tagged) => tagged.metadata.name === greeterName)
      ?.greet()
  }
}