🚀 Socket Launch Week Day 4:Socket MCP Adds Org Alerts, Threat Feed Review, and Package Inspection.Learn more
Sign In

xcatalog

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xcatalog

Dependency injection container for JavaScript

latest
Source
npmnpm
Version
1.1.0
Version published
Maintainers
1
Created
Source

xcatalog

Simple dependency injection container for nodejs4+

Installation

$ npm install xcatalog --save

Setting up

There are to different ways to load references into the xcatalog:

Direct configuration

xcatalog.set(id, type, ref, deps)

"use strict";

//A.js
class A {
    constructor (str) {
        this.bar = str;
    }
}

function foo (a, b) {
    return a + " " + b;
}

//config.js
const xcatalog = require("xcatalog");
xcatalog
    .set("MY_TEXT", "constant", "BANANA")
    .set("a", "singleton", A, ["MY_TEXT"])
    .set("foo", "factory", foo, ["MY_TEXT", "MY_TEXT"]);;

console.log(xcatalog("a").bar); // "BANANA"
console.log(xcatalog("foo")); // "BANANA BANANA"

Annotations

"use strict";

//A.js
class A {
    constructor () {
        this.foo = "BANANA";
    }
}

A.$xcatalog = { id: "a", type: "singleton" };

//B.js
class B {
    constructor (a) {
        this.bar = a.foo;
    }
}

B.$xcatalog = { id: "b", type: "singleton", inject: ["a"] };

//config.js
const xcatalog = require("xcatalog");

//xcatalog.load(require("./A")).load(require("./B"));
xcatalog.load(A).load(B);

console.log(xcatalog("b").bar); // "BANANA"

Load directories

Use xcatalog.loaddir(path) to load all resouces with $catalog annotation in the given path.

This function will require every .js file in the path and sub directories.

Getting instances

"use strict";
const xcatalog = require("xcatalog");

//Get service instance already built with dependencies
const myService = xcatalog("myService");

Dealing with Promises

If any reference returns a promise the xcatalog cannot be used until is ready.

xcatalog.ready() returns a promise, that promise is fulfilled when the xcatalog is fully available or rejected is any pending promise es rejected.

"use strict";
const xcatalog = require("xcatalog");
const promise = Promise.resolve("BANANA");

xcatalog.set("VALUE", "constant", promise);

//Get service instance already builded with dependencies
xcatalog.ready().then(function () {
    console.log(xcatalog("VALUE")); // "BANANA"
});

Keywords

di

FAQs

Package last updated on 05 Mar 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts