decorators-factory

1.1.2 • Public • Published

Decorators Factory

NPM version Github issues NPM downloads Github stars

Javascript package that makes the use of decorators a breath ^_^

It runs on node.js and doesn't require any transpiler such as Babel or Typescript ! It supports sync/async (factory)functions/methods.

Supported decorators:

  • debug: logs inputs and outputs of your functions
  • time: computes the running time with precision
  • try_catch: throws & logs any error that occured
  • retry: retries a function if it fails

There's a lot of room for improvement so any help: question/issue/comment/documentation(none of the decorators are documented at the moment)/PR is welcome.

The module does trick nonetheless. So enjoy :) !!

Install · Usage · Contributing

Decoration: Functions · Factory functions · Methods

Install

Install the decorators factory in your project as an NPM dependency :

npm install decorators-factory

You can also clone it into your project directory from decorators-factory GitHub repository:

git clone https://github.com/pasdechancee/decorators-factory.git

Usage

Setup

Quickly get up and started :

const Decorator = require("decorators-factory");
const { decorate_functions, decorate_methods } = Decorator();

You can also custom your decorators with your own config if you want. The configuration will get merged with the default one.

  • Change the log levels of the decorators.
  • Enable/Disable decorators without changing your source code ! For instance if you're in production and you don't want to waste ressources on debugging everything, you can do this:
const Decorator = require("decorators-factory");
 
const custom_config = {
    debug: {
        enabled: process.env.NODE_ENV === "PRODUCTION" ? false : true,
        start_log_level: "debug", // you can change these log levels.
        try_log_level: "debug",
        catch_log_level: "error"
    }
};
 
const { decorate_functions, decorate_methods } = Decorator(custom_config);

Decorate simple functions

async function async_simple_function(arg) {
    // do awesome stuff...
}
 
function sync_simple_function(arg) {
    // do awesome stuff...
}
 
const decorated_functions = decorate_functions({
    caller: "main_app",
    logger: undefined,
    // undefined => console.log will be used but a winston logger is way better.
    // see https://github.com/pasdechancee/loggers-factory
    decorators_dict: {
        // sync/async => decorators you want to use => functions to decorate
        async: {
            "time, debug, try_catch": [async_simple_function]
        },
        sync: {
            "time, debug, try_catch": [sync_simple_function]
        }
    }
});
 
const {
    sync_simple_function: decorated_simple_function,
    async_simple_function: decorated_async_simple_function 
= decorated_functions;
 
// run your decorated functions.
decorated_simple_function("I'm an arg.");
decorated_async_simple_function("I'm an arg.");

Decorate functions from factory

function factory_function() {
    return decorate_functions({
        caller: "main_app",
        decorators_dict: {
            async: {
                "time,debug,try_catch": [nested_simple_function]
            }
        }
    });
 
    async function nested_simple_function(args) {
        // do awesome stuff...
    }
}
 
const { nested_simple_function } = factory_function();
 
// run your decorated functions.
nested_simple_function("I'm an arg.");

Decorate methods

class My_Class {
    async simple_method(args) {
        // do awesome stuff...
    }
}
 
decorate_methods({
    // caller is My_class by default.
    target: My_Class,
    decorators_dict: {
        async: {
            "time,debug,try_catch": ["simple_method"]
        }
    }
});
 
const my_class_instance = new My_class();
my_class_instance.simple_method("I'm an arg.");

© MIT license Contact: pasdechancee.keyser-soze@protonmail.com

Package Sidebar

Install

npm i decorators-factory

Weekly Downloads

3

Version

1.1.2

License

MIT

Unpacked Size

58.1 kB

Total Files

29

Last publish

Collaborators

  • keyser_soze