# decorators-factory

> Javascript package that makes the use of decorators a breath.

Latest version **1.1.2** (published 2018-12-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install decorators-factory
pnpm add decorators-factory
yarn add decorators-factory
bun add decorators-factory
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.2 |
| Published | 2018-12-11 |
| First published | 2018-11-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 56.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Keyser Söze |
| Maintainers | keyser_soze |
| Keywords | decorators, Javascript-decorators, at-syntax, factory-function, method, function, functionnal, transpiler, babel, typescript |

## Links

- npm: https://www.npmjs.com/package/decorators-factory
- Repository: https://github.com/pasdechancee/decorators-factory
- Homepage: https://github.com/pasdechancee/decorators-factory#readme
- Issues: https://github.com/pasdechancee/decorators-factory/issues
- npm.io page: https://npm.io/package/decorators-factory

## Dependencies (3)

- [time-factory](https://npm.io/package/time-factory.md) ^1.1.1
- [tools-factory](https://npm.io/package/tools-factory.md) ^1.0.2
- [loggers-factory](https://npm.io/package/loggers-factory.md) ^1.1.3

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.1.2 (latest) — 2018-12-11
- 1.1.1 — 2018-12-10
- 1.1.0 — 2018-12-10
- 1.0.2 — 2018-11-15
- 1.0.1 — 2018-11-15
- 1.0.0 — 2018-11-10

## README

# Decorators Factory

![NPM version](https://img.shields.io/npm/v/decorators-factory.svg?style=flat-square)
![Github issues](https://img.shields.io/github/issues/pasdechancee/decorators-factory.svg?style=flat-square)
![NPM downloads](https://img.shields.io/npm/dt/decorators-factory.svg)
![Github stars](https://img.shields.io/github/stars/pasdechancee/decorators-factory.svg?style=flat-square)

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](#install) · [Usage](#usage) · [Contributing](https://github.com/pasdechancee/decorators-factory/blob/master/CONTRIBUTING.md)

#### Decoration: [Functions](#decorate-simple-functions) · [Factory functions](#decorate-functions-from-factory) · [Methods](#decorate-methods)

## Install

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

```shell
npm install decorators-factory
```

You can also clone it into your project directory from [decorators-factory GitHub repository](https://github.com/pasdechancee/decorators-factory):

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

## Usage

### Setup

Quickly get up and started :

```js
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](https://github.com/pasdechancee/decorators-factory/tree/master/src/config.js).

- 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:

```js
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

```js
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

```js
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

```js
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.");
```

---

&copy; [MIT license](https://github.com/pasdechancee/decorators-factory/blob/master/LICENSE)
Contact: <pasdechancee.keyser-soze@protonmail.com>

---
_Source: https://npm.io/package/decorators-factory · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
