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

@prostojs/infact

v0.2.3

Published

Instance Factory and Instance Registry for Metadata based Dependency Injection scenarios

Downloads

1,876

Readme

prostojs/infact

Zero dependency Instance Factory and Instance Registry for Metadata based Dependency Injection scenarios. Infact = Instance factory.

It solves the programmatic dependency injection problem in a generic way. You must take care of metadata yourself.

Install

npm install @prostojs/infact

How to use it

import { Infact, createProvideRegistry } from '@prostojs/infact'

const infact = new Infact({
    // it is required to provide a `describeClass`
    // function that would read metadata
    // and provide minimum required meta to
    // infact instance
    describeClass: classConstructor => {
        return {
            // only injectable: true classes can be instantiated
            injectable: true,

            // you may want to save some instances in global registry
            // in order to share those among Infact instances
            global: false,

            // provide registry can override some
            // dependencies by string key or by class type itself
            provide: createProvideRegistry([
                ['string key', () => new SomeClass()],
                [SomeClass, () => new SomeClass()],
            ]),

            // list of properties (optional)
            // used for resolveProp
            properties: ['prop1', 'prop2']

            // you must provide constructor params descriptions
            // you may want to take types from design:paramtypes metadata
            constructorParams: [
                {
                    type: SomeClass,
                },
                // considering circular dependency
                // design:paramtypes would be undefined
                // but you still can provide a type via
                // circular function:
                {
                    type: undefined,
                    circular: () => CircularDepClass, 
                },
                // to utilize provided by string key dependencies
                // you may use `inject` property
                {
                    type: SomeClass,
                    inject: 'string key', 
                },
            ],
        }
    },

    // describeProp (optionally) used tp
    // read prop metadata
    describeProp: (classConstructor, key) => {
        return {
            // return property metadata
        }
    }

    // resolveParam is optional function that is used for constructor params.
    // you might want to use this function to inject some constants or whatever
    // additional logic based on some additional resolvers...
    resolveParam: ({ paramMeta, classMeta, index, customData }) => {
        // must return some value | undefined
        // let's say we want to inject every string param with its index value
        return paramMeta.type === String ? (index + '') : undefined
    },,

    // resolveProp is optional function that is used for instance props
    // you might want to use this function to inject some constants or whatever
    // additional logic based on some additional resolvers...
    resolveProp: ({ key, initialValue, propMeta, classMeta, customData }) => {
        // must return some value | undefined
        // let's say we want to inject every string param with its key
        return propMeta.type === String ? key : undefined
    },

    // storeProvideRegByInstance is optional flag
    // if true it stores computed "provideRegistry" for the original instance
    // which allowes to use "getForInstance" method inheriting the
    // "provideRegistry" that was used when the original instance was created
    storeProvideRegByInstance: true
})

// after all required metadata fields are mapped to
// TInfactClassMeta interface everything is ready
// to instantiate your classes instances:
const mainInstance = infact.get(MainClass)

// anotherInstance will be created (retrieved) with "provideRegistry" taken from mainInstance
// (only if options.storeProvideRegByInstance flag is true)
const anotherInstance = infact.getForInstance(mainInstance, ClildClass)