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

ligature

v0.1.0

Published

Service injection framework for typescript

Downloads

4

Readme

Ligature

Ligature is a service injection framework specifically designed for use with Typescript and Node.js.

As part of an web server startup it's common to have a few asynchronous tasks that must be performed in a particular order. For example, an application might connect to a database, fetch some initial data, and only then is it in a state where it can serve content.

Installation

$ npm install ligature

Ligature requires that experimentalDecorators and emitDecoratorMetadata both be set to true in the tsconfig.json.

Usage

Services

Define a service that extend the Services class and implement an init method.

import { Service } from 'ligature';

export default class FooService extends Service {

  init () {
    console.log('FooService has been initialized');
  }
  
  doSomething () {
  
  }

}

That service can then be injected into other services using the @inject decorator.

import { Service } from 'ligature';
import FooService from './foo';

@inject
foo: FooService;

export default class BarService extends Service {

  init () {
    console.log('BarService has been initialized');
    this.foo.doSomething();
  }
  
}

Then initialize all the services using the ServiceLoader.

import { ServiceLoader } from 'ligature';
import FooService from './foo';
import BarService from './bar';

ServiceLoader.getInstance().init([FooService, BarService]).then(() => {
  console.log('Services started!');
}, (err) => {
  console.log('Could not start services', err);
});

Ligature determines the correct order in which the services must be started, and invokes the init method on each on in the correct order.

Finally services can be accessed from the ServiceLoader with the get method.

let foo = ServiceLoader.getInstance().get(FooService);

Consumers

Ligature also provides a Consumer class, which is similar to a Service except Consumers cannot be injected into other Services or Consumers.

import { Consumer } from 'ligature';
import FooService from './foo';

@inject
foo: FooService;

export default class FooConsumer extends Consumer {

  init () {
    console.log('FooConsumer has been initialized');
  }
  
}

Passing options to Service init method

Any options map keyed off of the Service can be passed into the ServiceLoader's init method.

let initOptions = new Map();
initOptions.set(Express, { foo: 'bar' });

let loader = await ServiceLoader.getInstance().init([Express, ...routes], initOptions);

When the corresponding Service is initialized, its init method will be passed the specified options.

Advanced Usage

In addition to the init method, Ligature allows an optional done method on each service and consumer. The done method is executed in reverse order from init. For example, if services A, B, and C are initialized in the order A => B => C, then the done method is called in the order C => B => A.

An example of where the done method is useful is when creating a service to start an Express app (See the express-app in example). There's an Express service which exposes the express app, and routes are implemented as Consumers that depend on the Express service. Since error middleware must be registered last, that's handled within the done method to ensure it's registered last.