# op-result

> A lib to encapsute operation result and manage logs. ## Logs

Latest version **1.0.4** (published 2022-11-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install op-result
pnpm add op-result
yarn add op-result
bun add op-result
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2022-11-02 |
| First published | 2022-11-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 13.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Alessandro Dev |
| Maintainers | alessandro-dev |
| Keywords | result, operation, result log, logs |

## Links

- npm: https://www.npmjs.com/package/op-result
- Repository: https://github.com/4lessandrodev/op-result
- Issues: https://github.com/4lessandrodev/op-result/issues
- npm.io page: https://npm.io/package/op-result

## Dependencies (2)

- [pino](https://npm.io/package/pino.md) ^8.7.0
- [pino-pretty](https://npm.io/package/pino-pretty.md) ^9.1.1

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [@luma.gl/experimental](https://npm.io/package/@luma.gl/experimental.md) — 77.1K weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads

## Recent versions

- 1.0.4 (latest) — 2022-11-02
- 1.0.3 — 2022-11-02
- 1.0.2 — 2022-11-02
- 1.0.1 — 2022-11-02
- 1.0.0 — 2022-11-02

## README

# ts-result

A lib to encapsute operation result and manage logs.
## Logs

process.env.LOG_LEVEL = 'info' | 'debug' | 'none'

Case LOG_LEVEL is defined as info all Result state will print basic information on terminal

Case LOG_LEVEL is defined as debug all Result state will show result state and file name execution

---

## How it works?
First you need to have an operation like example below

```ts

import { Payload, Ok, Fail, Error } from 'op-result';

class MyOperation {

    // operation
    execute(value: number): Payload<string, Error> {

        // success result
        if(value % 2 === 0) return Ok('success');

        // failure result
        return Fail({ message: 'invalid value' });
    }
}

const myOp = new MyOperation();

const result = myOp.execute(10);

// ok state
console.log(result.isOk());

> true

// data state
console.log(result.data());

> "success"

// error state
console.log(result.error());

> null

```
--- 
## Hooks
Lets see an example to signup

```ts

import { Payload, Ok, Fail, Error } from 'op-result';

class SignUp {

    execute(data: Auth): Payload<User, Error> {
        
        // create user instance
        const user = User.create(data);

        // check user instance
        if(user.isFail()) return Fail({ message: 'oops' });

        // success state
        return Ok(user.data());

    }
}

const mailer = new Mailer();

const auth = new SignUp();

const result = auth.execute({ name, email, password });

// This will be executed only case result is Ok
// sendEmail function will receive Auth arg data
result.on('OK').execute(mailer.sendEmail);

// This will be executed only case result is Fail
// console.log function will receive error state
result.on('FAIL').execute(console.log);

```

---

## Combine
You can check many results state with combine

```ts

import { Ok, Fail, Combine } from 'op-result';

const resultsA = [ Ok(), Ok(), Ok() ];

// check many results 
const resA = Combine(resultsA);

const isOkA = resA.isOk();

console.log(isOkA);

> true


const resultsB = [ Ok(), Fail(), Ok() ];

// check many results 
const resB = Combine(resultsB);

const isOkB = resB.isOk();

console.log(isOkB);

> false

```

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