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

synth-di

v0.3.0

Published

A promise-enabled dependency-injection module designed for Synth, but can be used for your own projects.

Downloads

23

Readme

Synth-DI

A promise-enabled dependency-injection module designed for Synth, but can be used for your own projects.

Build Status Code Climate Test Coverage

Usage

Install it.

npm install synth-di

Load it.

var DI = require('synth-di');
// Create a new dependency system
var di = new DI();

What is a service?

A service is a function that can be requested by another service.

They're just ordinary JS functions, except:

  • Each service must have a unique name (unique for your app), unless being executed immediately.
  • Each service has 0 or more dependencies.
  • When a service is executed, Synth-DI will invoke its dependencies first, along with its dependencies' dependencies, and so on.
  • A service declares its dependencies by requesting them as parameters. Unlike ordinary JavaScript functions, this means that the names of the parameters are significant, and their order doesn't matter!
  • A service need not be a function though (I lied earlier). It can also be a named piece of data that is provided at time of execution.
  • If a service is not a function, then it can't have any dependencies.
  • A service can optionally return a promise. If so, the service won't be considered "ready" until the promise resolves. The result of the promise is then passed in to its caller.
  • When a service is executed, a promise is always returned.
  • Each service is only called once per execution. So if two services A and B depend on C, C is resolved once and returned to both A and B.

An elaborate Example

Let's say you're writing an Express server and want to fetch some data for an admin user, and then send a response. It needs two things: The user (guaranteed to be an admin), and a connection to the DB.

var DI = require('synth-di');
var di = new DI();

// service name specified as first parameter
di.register('adminUser', function (user) {
  if (!user || !user.admin) {
    throw "The specified user is not an administrator";
  }

  return user;
});

// The order you register services doesn't matter
di.register('user', function (db, authToken) {
  // The 'req' service is actually an object that will be passed in at time of execution
  // Returns a promise that returns a user object (or null if no user found)
  return db.collection('users').find({
    id: userid
  });
});

var dbConnection = require('mongojs')(process.env.MONGO_DB);
// service name extracted from given named-function
di.register(function db () {
  return dbConnection;
});

// Now that we have our services registered, let's get that admin user and data!
app.get('/api/users/:userid/secrets', function (req, res) {
  var authToken = req.cookies.authToken;
  // Execute an anonymous service that depends on adminUser and db services
  di.exec(function (adminUser, db) {
    // Send back the requested data
    db.collection('secrets').findAll({ user_id: user.id }, function (err, data) {
      res.send(data);
    });
  });
});

API

di.register([optional serviceName:String], [service:Function])

Registers a service/function that can be used by another service/function.

The service name can be specified as the first parameter, or as the name of the function passed in.

di.exec([serviceName:String], [optional services:Object])

Executes the specified service. The requested service should already be registered before executing it.

It returns a promise that then returns the result of the call.

The optional 2nd paramter

An optional 2nd parameter can be provided to provide service dependencies at execution time (overriding already registered services).

The keys are the names of the services, and the values are passed in as those services, untouched.

Unlike a registered service, if a value is a function, it will NOT be invoked first, the function itself will be passed in.

License

MIT

Credit