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

jslang-injector

v1.0.3

Published

Simple JavaScript service injector.

Downloads

61

Readme

jslang-injector

Simple JavaScript service injector for Browser or Node. Depends only on underscore. Compatible with IE 9 (no proxies).


Usage

const injector = require('jslang-injector');

// Define cache service:
function cacheService() {
    return {
        // ...
    };
}

// Define product service:
class ProductService {
    // ...
}

// Define pizza service:
class PizzaService {

    constructor(cacheService, productService) {
        // ...
    }

}
// Set inject options as static property:
PizzaService.__injectOptions = ['asProvider', (di) => new PizzaService(di.cacheService(), di.productService())];

// Define cheese pizza service:
class CheesePizzaService {

    constructor(pizzaService) {
        // ...
    }
    
    bake() {
        // ...
    }

}

let di = injector.create();

// Register cache service:
di.cacheService = injector.service(['asFunction', cacheService]);

// Register product service:
di.productService = injector.service(['asClass', ProductService]);

// Register pizza service:
di.pizzaService = injector.service(PizzaService); // inject options defined in static property `__injectOptions`

// Register cheese pizza service:
di.cheesePizzaService = injector.service(['asProvider', (di) => new CheesePizzaService(di.pizzaService())]);

return di.cheesePizzaService().bake();

Quick Guide

This simple package provides two resolve behaviors and four ways to define resolver.

Ways to define resolver

  1. Via function call:

    function cacheService() {
        return {
            // ...
        };
    }
    
    di.cacheService = injector.service(['asFunction', cacheService]);

    In this way di.cacheService() returns resolved instance of cacheService() call.

  2. Via class builder:

    class ProductService {
        // ...
    }
    
    di.productService = injector.service(['asClass', ProductService]);

    In this way di.productService() returns resolved instance of new ProductService() call.

  3. Via provider function:

    function namesService() {
        return {
            // ...
        };
    }
    
    di.namesService = injector.service(['asProvider', (di) => {
    
        // Any configuration here...
        const options = {};
    
        return namesService(di.cacheService(), options);
    }]);

    In this way di.namesService() returns resolved instance of user defined provider function call.

  4. Via static property:

    class PizzaService {
    }
    PizzaService.__injectOptions = ['asProvider', (di) => new PizzaService(di.cacheService(), di.productService())];
    
    di.pizzaService = injector.service(PizzaService);

    In this way di.pizzaService() returns resolved instance of PizzaService.__injectOptions instructions.

    Static injectOptions could be:

    1. A string: PizzaService.__injectOptions = 'asClass';.
    2. A short array: PizzaService.__injectOptions = ['asClass'];.
    3. A full array: PizzaService.__injectOptions = ['asClass', PizzaService];.

Resolve behaviors

  1. As service (injector.service(['asFunction', cacheService])) -- would be resolved once and cached for later usage.

  2. As factory (injector.factory(['asFunction', cacheService])) -- would be resolved on every di.cacheService() call.


ExpressJS middleware

Sample usage with express framework. In this case used middleware to bind req.di object.

const express = require('express');
const injector = require('jslang-injector');
const inject = require('jslang-injector/src/express/middleware/inject');

const CacheService = require('./path/to/cache-service');
const PizzaService = require('./path/to/pizza-service');

// Define available services bind handler:
inject.bindHandler = (req, serviceKey) => {
    switch (serviceKey) {
        case 'cache':
            req.di.cache = injector.service(CacheService);
            break;
        case 'pizza':
            req.di.pizza = injector.service(PizzaService);
            break;
        default:
            throw new Error(`Invalid service key: '${serviceKey}'!`);
    }
};


const app = express();

app.use('/hello-pizza', inject(['cache', 'pizza']), (req, res) => {
//                      ^ 'inject.bindHandler' will be called here

    // Now we can use services:
    let pizza = req.di.cache().get('pizza');
    if (!pizza) {
        pizza = req.di.pizza().bake();
    }
    return res.send(pizza.toHtml());
});