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

@fdosruiz/packetjs

v1.1.0

Published

Lightweight micro dependency injection container

Downloads

19

Readme

Packet JS

node install size npm downloads Build status MIT License Gihub package dependents Coverage Status

Packet JS is a lightweight micro-dependency injection container for JavaScript/Node applications, written in TypeScript and with lazy loading, instantiating each service on demand with dependency on each other.

Installation

Using npm:

$ npm install @fdosruiz/packetjs

Using yarn:

$ yarn add @fdosruiz/packetjs

Importing

// Using Node.js `require()`
const container = require('@fdosruiz/packetjs');

// Using ES6 imports
import container from '@fdosruiz/packetjs';

Basic usage

Register a service:

const container = require('@fdosruiz/packetjs');

container.add('Service', () => {
  return new SomeService();
});

In some other place:

const container = require('@fdosruiz/packetjs');

// Make the instance of the service on demand, with lazy loading.
const service = container.get('Service');

In both cases, require('@fdosruiz/packetjs') always get the same instance of the container.

Adding configuration properties

It is possible to add configuration properties, for service registration or to make the configuration available throughout the application.

const properties = require('./some-configuration-object-in-json-format');
container.addProps(properties);

To get the properties:

const props = container.getProps();

To get the properties when registering a service:

container.add('Service', ({ props }) => {
  return new SomeService(props.someParam);
});

To add new properties to existing ones:

container.addProps({
  newProperty: { foo: 'bar' }
});

Register multiples services, with dependencies on each other

For configuring the dependency Injection container, the order of registration of each service is indifferent. Each registered service will be called on demand when the main service is called.

On the other hand, the callback function receives the container and the properties as an object in the argument: { container, props }:

// Configuring some service
container.add('service', ({ container: c }) => {
    const dao = c.get('dao');
    return new Service(dao);
});

// Configuring a dependency with dao
container.add('dao', ({ container: c }) => {
    const db = c.get('db');
    return new dao(db);
});

// Configuring a database
container.add('db', ({ container: c, props: p }) => {
    return new someDatabase(
      process.env.USER,
      process.env.PASS,
      p.someConfigurationProperty
    );
});

In another place we can use the container like this:

// Instantiate the services on demand, with lazy loading for each service.
const foo = container.get('service').getBar(id);

Factory

If you need a factory of the registered services, you can use the getFactory method to always get a different instance of the service:

const service1 = container.getFactory('service');
const service2 = container.getFactory('service');
// service1 !== service2

Instead, with get method:

const service1 = container.get('service');
const service2 = container.get('service');
// service1 === service2

Instantiating new containers

For standalone containers, it is possible to create isolated instances by accessing the Core Container Class:

const { Container } = require('@fdosruiz/packetjs/lib/core');
const a = new Container();
const b = new Container();
// a !== b

Container API

| Method | Arguments | Description | |----------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| ------------------------------------------------------------ | | add(key, callback) | - key: Unique key for the new service or function- callback({ container, props }): Callback function with dependency injection logic.The callback function receives an object with the container and properties in its argument. | Add a new service or function to the container | | get(key) | - key: Unique key of the service or function to get | Always get the same instance for a concrete service | | getFactory(key) | - key: Unique key of the service or function to get | Always get a new instance for a concrete service | | addProps(props) | - props: Configuration properties object (JSON) | | | getProps() | | Gets the configuration properties object | | getContainer() | | Always get the same instance of the container. (static method) |

Credits

Packet JS is inspired by pimple, a small PHP dependency injection container.

License

MIT