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

service-seeker

v1.0.0

Published

Service Locator TypeScript Library

Downloads

2

Readme

Service Seeker

[![npm package][npm-img]][npm-url] [![Build Status][build-img]][build-url] [![Downloads][downloads-img]][downloads-url] [![Issues][issues-img]][issues-url] [![Code Coverage][codecov-img]][codecov-url] [![Commitizen Friendly][commitizen-img]][commitizen-url] [![Semantic Release][semantic-release-img]][semantic-release-url]

Service Locator TypeScript Library

Install

npm install service-seeker

Usage

Defining providers

The provider configuration is an object literal with two properties:

  • The provide property holds the token that serves as the key for both locating a dependency value and configuring the injector.

  • The second property is a provider definition object, which tells the injector how to create the dependency value. The provider-definition key can be useClass, as in the example. It can also be useValue, or useFactory. Each of these keys provides a different type of dependency, as discussed in the following section.

app.provider.ts

import { Injector } from '@nx-clean/core';

class SomeService {
  constructor() {}
}

const injector = Injector.create([
  { provide: SomeService, useClass: SomeService },
]);

const instance = injector.get<SomeService>(SomeService);
SomeService {}

Injecting a configuration object

A common use case for object literals is a configuration object. The following configuration object includes an address of a web API endpoint.

app.provider.ts

import { Inject, Injectable, InjectionToken, Injector } from '@nx-clean/core';

interface ApiConfig {
  url: string;
}

const API_CONFIG: ApiConfig = {
  url: 'https://api.clean-architecture.design',
};

To provide and inject the configuration object, specify the object in the Injector as providers array.

app.provider.ts

const injector = Injector.create([
  { provide: API_TOKEN, useValue: API_CONFIG },
]);

Using an InjectionToken object

Define and use an InjectionToken object for choosing a provider token for non-class dependencies. The following example defines a token, APP_CONFIG of the type InjectionToken.

The optional type parameter, <AppConfig>, and the token description, api.config, specify the token's purpose.

app.provider.ts

const API_TOKEN = new InjectionToken<ApiConfig>('api.config');

Next, register the dependency provider in the service using the InjectionToken object of API_CONFIG.

app.provider.ts

const injector = Injector.create([
  { provide: API_TOKEN, useValue: API_CONFIG },
]);

Now get the configuration object using Injector's get method

app.provider.ts

const config = injector.get<ApiConfig>(API_TOKEN);
{
  url: 'https://api.clean-architecture.design';
}

Now, inject the configuration object into the constructor with @Inject() parameter decorator.

app.provider.ts

@Injectable()
class SomeService {
  constructor(@Inject(API_TOKEN) public config: ApiConfig) {}
}

Specifying an alternative class provider

Different classes can provide the same service. For example, the following code tells the injector to return a BetterLogger instance when the component asks for a logger using the Logger token.

app.provider.ts

abstract class OneService {}

@Injectable()
class SomeService implements OneService {
  constructor(@Inject(API_TOKEN) public config: ApiConfig) {}
}

const injector = Injector.create([
  { provide: API_TOKEN, useValue: API_CONFIG },
  { provide: OneService, useClass: SomeService },
]);

const instance = injector.get<OneService>(OneService);
SomeService {config: {…}}
  config:
    url: "https://api.clean-architecture.design"

Configuring class providers with dependencies

If the alternative class providers have their own dependencies, specify both providers in the providers metadata property of the parent module or component.

app.provider.ts

@Injectable()
class InjectableClass {
  constructor(public oneService: OneService) {}
}

const injector = Injector.create([
  { provide: API_TOKEN, useValue: API_CONFIG },
  { provide: OneService, useClass: SomeService },
  { provide: InjectableClass, useClass: InjectableClass },
]);

const instance = injector.get<InjectableClass>(InjectableClass);
InjectableClass {oneService: SomeService}
  oneService: SomeService
    config:
      url: "https://api.clean-architecture.design"

Completed example code

app.provider.ts

import { Inject, Injectable, InjectionToken, Injector } from '@nx-clean/core';

interface ApiConfig {
  url: string;
}

const API_CONFIG: ApiConfig = {
  url: 'https://api.clean-architecture.design',
};

const API_TOKEN = new InjectionToken<ApiConfig>('api-token');

abstract class OneService {}

@Injectable()
class SomeService implements OneService {
  constructor(@Inject(API_TOKEN) public config: ApiConfig) {}
}

@Injectable()
class InjectableClass {
  constructor(public oneService: OneService) {}
}

const injector = Injector.create([
  { provide: API_TOKEN, useValue: API_CONFIG },
  { provide: OneService, useClass: SomeService },
  { provide: InjectableClass, useClass: InjectableClass },
]);

const instance = injector.get<InjectableClass>(InjectableClass);