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

@thefoxjob/js-facade

v1.0.0

Published

Static interface to concrete classes via an IoC Service Container

Downloads

6

Readme

Js Facade

Static interface to concrete classes via an IoC Service Container.

Before you continue reading, you should know that this package is heavily inspired by Taylor Otwell's Facades in Laravel. Please go read the documentation, to gain a better understanding of what facades are and how they are intended to work.

Contents

How to install

npm install @aedart/js-facade

Quick start

Set the IoC instance

The Facade requires an IoC Service Container instance, so that it can resolve the facade's accessor (a string identifier) and obtain a concrete class instance.

There is no check of kind of IoC implementation you should use. But, your IoC is required to have a make method;

Required Make method

/**
 * Resolve the registered abstract from the container
 * 
 * @param {string} abstract
 * @param {Array.<*>} [parameters]
 * 
 * @return {Object}
 * 
 * @throw {Error}
 */
function make(abstract, parameters = []){
    // ... not shown ... //
};

Whenever the make method is invoked, the facade assumes that the IoC is able to return a concrete instance or fail.

As long as your desired IoC has such a method, you can use this facade. Alternatively, you can use my @aedart/js-ioc package.

Setting IoC instance on Facade

import Facade from '@aedart/js-facade';
import IoC from '@aedart/js-ioc';

// Somewhere in you bootstrap / setup logic, set the IoC on the Facade
Facade.ioc = IoC;

Create a facade

Given that you have a concrete class, and that you somehow have registered (bound) an instance to it in your IoC;

A simple Box class

'use strict';

class Box {
    set width(value){
        // ... not shown .../
    }
    
    get width(){
        // ... not shown .../
    }

    set height(value){
        // ... not shown .../
    }
    
    get height(){
        // ... not shown .../
    }
}

export default Box;

A facade for the Box class

'use strict';

import Facade from '@aedart/js-facade';

class BoxFacade extends Facade {
    constructor(){
        // Accessor - MUST match the identifier you used to
        // register your Box with, in your IoC.
        super('my-box-identifier');
    }
}

// You MUST export a initialised instance of this facade.
export default new BoxFacade();

Behind the scene, a Proxy is used by the facade to trap method calls. Those traps ensure to invoke the correct methods on the concrete class, via the IoC Service Container.

Using your facade

Whenever you need to use your concrete class, via a facade, you simply just import it and invoke the methods or properties that you require from it.

Using BoxFacade

'use strict';

// Import you facade from where you stored it...
import BoxFacade from './src/Facades/BoxFacade';

// ... somewhere in your application or inside a component ... //

BoxFacade.width = 25;
BoxFacade.height = 50;

console.log(BoxFacade.width, BoxFacade.height); // Output 25, 50 

Contribution

Have you found a defect ( bug or design flaw ), or do you wish improvements? In the following sections, you might find some useful information on how you can help this project. In any case, I thank you for taking the time to help me improve this project's deliverables and overall quality.

Bug Report

If you are convinced that you have found a bug, then at the very least you should create a new issue. In that given issue, you should as a minimum describe the following;

  • Where is the defect located
  • A good, short and precise description of the defect (Why is it a defect)
  • How to replicate the defect
  • (A possible solution for how to resolve the defect)

When time permits it, I will review your issue and take action upon it.

Fork, code and send pull-request

A good and well written bug report can help me a lot. Nevertheless, if you can or wish to resolve the defect by yourself, here is how you can do so;

  • Fork this project
  • Create a new local development branch for the given defect-fix
  • Write your code / changes
  • Create executable test-cases (prove that your changes are solid!)
  • Commit and push your changes to your fork-repository
  • Send a pull-request with your changes
  • Drink a Beer - you earned it :)

As soon as I receive the pull-request (and have time for it), I will review your changes and merge them into this project. If not, I will inform you why I choose not to.

Acknowledgement

Versioning

This package follows Semantic Versioning 2.0.0

License

BSD-3-Clause, Read the LICENSE file included in this package