New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

op-result

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

op-result

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

  • 1.0.4
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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


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


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


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

Keywords

FAQs

Package last updated on 02 Nov 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc