# fhooks

> Dependency Injection for functions via hooks

Latest version **0.0.2** (published 2019-07-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install fhooks
pnpm add fhooks
yarn add fhooks
bun add fhooks
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.2 |
| Published | 2019-07-06 |
| First published | 2019-07-06 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 191.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | arvitaly |

## Links

- npm: https://www.npmjs.com/package/fhooks
- Repository: https://github.com/arvitaly/fhooks
- Homepage: https://github.com/arvitaly/fhooks#readme
- Issues: https://github.com/arvitaly/fhooks/issues
- npm.io page: https://npm.io/package/fhooks

## Recent versions

- 0.0.2 (latest) — 2019-07-06
- 0.0.1 — 2019-07-06

## README

# fhooks

Dependency Injection for functions via hooks

[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Coverage percentage][coveralls-image]][coveralls-url]
[![experimental](http://badges.github.io/stability-badges/dist/experimental.svg)](http://github.com/badges/stability-badges)

# About

In OO-programming we have multiple ways for dependency injection: constructor injection, method injection, decorators, etc. But, for functional programming it is always proceed via arguments, because we want to have pure function, cleaned from side effects. But, sometimes we want to use functions with side effects and not pass arguments through all tree of calls. So, i wrote this library, inspired on React-hooks. For to use it, we should change our codebase onetime, every function should be wrapped in special format, but next time, when you will need for additional injection, you will just add it very simply.

For example, we have some function:

```typescript
function A() {
    doSomething1();
    doSomething2();
}
```

And we want to add log between two lines:

```typescript
function A() {
    doSomething1();
    log("Some logs...");
    doSomething2();
}
```

But, we don't want global function `log`, we want use some `Logger`, like Winston, singleton for all project. Now, we should pass logger as argument to function.

```typescript
function A(logger) {
    doSomething1();
    logger.log("Some logs...");
    doSomething2();
}
```

It means, we need to change function-interface and pass logger every time, when we want to call function.

Now, we use `fhooks` and change function, like this:

```typescript
const LoggerContext = createContext();
const A = ({ useContext }: ContextInjector) => () => {
    const logger = useContext(LoggerContext);
    doSomething1();
    logger.log("Some logs...");
    doSomething2();
};
```

In future, if we will need extra dependency, we will not change the function's signature, all dependencies will pass by `useContext`.

Now, we can test our function use a testing or development `Context`

# Install

    npm install fhooks --save

    or

    yarn add fhooks

# Usage

```typescript
import { createContextExecutor, createContext, ContextInjector } from "fhooks";
const StringContext = createContext<string>();
const LoggerContext = createContext<typeof logger>();
// init executor
const executor = createContextExecutor();
// create some logger
const globalLogger = {
    log: (...args: any[]) => console.log(...args),
};
executor.provideContext(LoggerContext, globalLogger);
// function A
const A = ({ call, useContext }: ContextInjector) => () => {
    const logger = useContext(LoggerContext);
    logger.log("Run function A");
    return call(B, "fromA");
};
// function B
const B = ({ call, useContext }: ContextInjector) => (param1: string) => {
    const logger = useContext(LoggerContext);
    logger.log("Run function B");
    const stringValue = useContext(StringContext);
    return "returnFromB::" + param1 + "::" + stringValue;
};

const App = ({ call, useContext, provideContext }) => () => {
    const logger = useContext(LoggerContext);
    logger.log("Run App");
    provideContext(StringContext, "SomeString");
    return call(A);
};

const result = executor.run(App);
// "returnFromB::fromA::SomeString"
```

# API

```typescript
class ContextExecutor {
    provideContext = <T>(context: Context<T>, value: T): void;
    run<F extends (...args: any[]) => any>(fn: InjectableFunction<F>, ...args: Parameters<F>): ReturnType<F>;
}

class ContextInjector {
    call = <F extends (...args: any[]) => any>(fn: InjectableFunction<F>, ...args: Parameters<F>): ReturnType<F>;
    provideContext = <T>(context: Context<T>, value: T): void;
    useContext = <T>(context: Context<T>): T;
}

class Context<T = any> {

}

function createContext<T>(params: IContextParams<T> = {}): Context<T>;

```

# Test

    npm install
    npm test

[npm-image]: https://badge.fury.io/js/fhooks.svg
[npm-url]: https://npmjs.org/package/fhooks
[travis-image]: https://travis-ci.org/arvitaly/fhooks.svg?branch=master
[travis-url]: https://travis-ci.org/arvitaly/fhooks
[daviddm-image]: https://david-dm.org/arvitaly/fhooks.svg?theme=shields.io
[daviddm-url]: https://david-dm.org/arvitaly/fhooks
[coveralls-image]: https://coveralls.io/repos/arvitaly/fhooks/badge.svg
[coveralls-url]: https://coveralls.io/r/arvitaly/fhooks

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