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

@searchfe/inject-js

v1.6.4

Published

A Dependency Injection library

Downloads

2,328

Readme

inject-js

Build Status semantic-release Coveralls

A tiny dependency Injection library for TypeScript. https://searchfe.github.io/inject-js/

Install

Can be installed via npm

npm install --save @searchfe/inject-js

inject-js uses Reflect Metadata to determine dependencies, so you need make sure your tsconfig.json contains:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

And, you'll need a polyfill for the Reflect API, such as:

Usage

Following are some use cases, more details please refer to the API Reference.

@injectable

@injectable is used to decorate the Service class, so that its dependencies can be resolved by inject-js and it can be injected to other services.

// car.ts
import { injectable } from 'inject-js';
import { Wheel } from './wheel';

@injectable
export class Car {
    constructor(private wheel: Wheel) {}
}

// wheel.ts
import { injectable } from 'inject-js';

@injectable
export class Wheel {
    constructor() {}
}

// index.ts application entry
import { Container } from 'inject-js';
import { Car } from './car';
import { Wheel } from './wheel;

const di = new Container();
di.addService(Car);
di.addService(Wheel);
const car = di.create(Car);

@inject

@inject(token) is used to decorate a dependency (constructor argument) to specify which provider should be used. The token argument is the unique identifier of a Provider within the container. token is typically used for cases where there's no Service class actually defined, or Metadata API is not available.

import { inject } from 'inject-js';
import { Wheel } from './wheel';

@injectable
export class Car {
    constructor(@inject('Wheel') private wheel: Wheel) {}
}

// index.ts application entry
import { Container } from 'inject-js';
import { Car } from './car';
import { Wheel } from './wheel';

const di = new Container();
di.addService('Wheel', Wheel);
di.addService(Car);
const car = di.create(Car);

@service

If the container instance is available in the context where Services are defined, @service can be used as a shorthand of .addService():

const container = new Container();

@service(container)
class FooService {}

// Equivalent to:

const container = new Container();

@injectable
class FooService {}

container.addService(FooService)

Container

The Container class maintains a set of Providers and the corresponding Tokens. There're several ways to register Providers:

  • .addProvider(): register a factory class with a .create(): Service method. All other ways are implemented using .addProvider() internally.
  • .addFactory(): register a factory function which returns a Service instance.
  • .addService(): register a concrete Service class.
  • .addValue(): register a value.