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

servicedi

v1.0.0

Published

Node service dependency injection

Downloads

5

Readme

Service DI

Node.js service dependency injection made easy.

Build Status Coverage Status Maintainability JavaScript Style Guide

Installation

Just run the following command:

npm install servicedi --save

How It Works

Basically, an application can create a ServiceProvider instance which acts as an Inversion of Control container. You can register constants, factory functions, or constructors with it and it provides an "injector" that resolves dependencies when a getter is called or it is destructured. See below for documentation and usage examples.

Classes

ServiceProvider class

  • constructor(): The constructor, nothing special
  • cleanup(): Runs all registered cleanup functions (finalizers) in reverse registration order and clears registered services
  • get(name): Gets a registered service instance by name, throws an Error if it cannot find a registered service with that name
  • injector: Proxy object that allows you to get services using destructuring or direct getters
  • register(name, factory, options): Registers a service, see below for registration options
  • registerConst(name, val, options): Registers a constant value as a service (by name), see below for registration options
  • registerConstructor(Class, options): Registers a constructor as a service (converts the name to camelCase, ex: MyService is registered as myService), see below for registration options

Registration Options

  • cleanup: Function, acts as a finalizer to run when ServiceProvider.prototype.cleanup() is called
  • persist: Boolean value (default: false), whether to store the constructed instance of a service after it's been constructed (not applicable for constants)
  • preload: Boolean value (default: false), whether to immediately construct a service instance (implies persist: true, not applicable for constants)

Usage

Here's a quick example that demonstrates the main idea:

const { ServiceProvider } = require('servicedi')
const sp = new ServiceProvider()

Then you register a factory and constant:

sp.register('myService', () => ({ hello: () => console.log('Hello, world!') }))
sp.registerConst('myConstant', 50)

We can resolve them using destructuring:

const { myService, myConstant } = sp.injector

Or we can resolve them using a "getter":

const myService2 = sp.injector.myService

And every call to a factory constructs a new instance so this will print NOT EQUAL!:

if (myService !== myService2) {
  console.log('NOT EQUAL!')
}

But we can make a persistent (singleton) service as well by passing { persist: true } as an option:

sp.register('myPersistentService', () => ({ hello: () => console.log('Persistent hello') }), { persist: true })

We can also make it preload the service (run the factory immediately) by passing { preload: true } as an option. This implies { persist: true } though, so keep that in mind.

sp.register('myPreloadedService', () => ({ hello: () => console.log('Preloaded hello') }), { preload: true/*, persist: true*/ })

There's also syntactic sugar for registering a service constructor:

sp.registerConstructor(MyClassService)

When you're done, if you're using finalizers, you can clean up all the services and clear the ServiceProvider using:

sp.cleanup()

Here's a full example:

// Include the library and create a service provider
const { ServiceProvider } = require('servicedi')
const sp = new ServiceProvider()

// Let's define a service that needs a "barService"
class FooService {
  constructor({ barService }) {
    console.log('FooService ctor')
    this.barService = barService
  }

  doFoo() {
    console.log('FOO')
    this.barService.doBar()
  }
}

// Let's define another service
class BarService {
  constructor() {
    console.log('BarService ctor')
  }

  doBar() {
    console.log('BAR')
  }
}

// Let's define a controller (that would probably be used by routing) that needs
// a "fooService", "barService", and "LIFE"
class MyController {
  constructor({ fooService, barService, LIFE }) {
    console.log('MyController ctor')
    this.life = LIFE
    this.fooService = fooService
    this.barService = barService
  }

  doFooBar() {
    console.log('FOOBARBAR')
    console.log(this.life)
    this.fooService.doFoo()
    this.barService.doBar()
  }
}

// TEST CODE
// Let's register the services (and constant for LIFE) using all 3 ways
sp.registerConst('LIFE', 42)
sp.registerConstructor(BarService, { persist: true, cleanup: () => console.log('CLEAN BAR') })
sp.register('fooService', injector => new FooService(injector), { cleanup: () => console.log('CLEAN FOO')})

// Let's load controller and run the logic
const ctrl = new MyController(sp.injector)
ctrl.doFooBar()

// And cleanup after (to see if our cleanup functions get called)
sp.cleanup()

Pull Requests, Issues, etc.

Feel free to submit any feature requests, pull requests, or issues and I may get to them when I can.

License

MIT License

Copyright © 2019 Caleb Everett

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.