https://github.com/4lessandrodev/op-result
Lib to encapsulate operation result and log when activate info or debug option
https://github.com/4lessandrodev/op-result
lib logs results
Last synced: 8 months ago
JSON representation
Lib to encapsulate operation result and log when activate info or debug option
- Host: GitHub
- URL: https://github.com/4lessandrodev/op-result
- Owner: 4lessandrodev
- Created: 2022-11-02T20:06:43.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-02T21:00:55.000Z (over 3 years ago)
- Last Synced: 2025-07-03T01:47:37.956Z (9 months ago)
- Topics: lib, logs, results
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/op-result
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# op-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 {
// 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 {
// 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
```