@mondopower/result-types
TypeScript icon, indicating that this package has built-in type declarations

1.1.551 • Public • Published

Mondo Result Types

Library to use for result type of typescript functions and helper functions.

Install

npm i @mondopower/result-types

Usage

This library consists of two major result types.

1) ResultVoid

Used whenever the function will return void in the case of success or failure

import {raiseFailure, raiseSuccessVoid, ResultVoid} from '@mondopower/result-types'

enum FooErrorType {
  someError= 'someError'
  ...
}

function foo(flag: boolean): ResultVoid<FooErrorType> {
  if (flag)
    return raiseFailure({
      errorType: FooErrorType.someError,
      message: '<my error message>'
    })

  return raiseSuccessVoid()
}

const fooResponse = foo(flag)

if (fooResponse.isErrored)
  // Failure Handling
else
  // success handling

2) Result

Used whenever the function will return a value/object in the case of success or failure

import {raiseFailure, raiseSuccess, Result} from '@mondopower/result-types'

enum FooErrorType {
  someError= 'someError'
  ...
}

function foo(flag: boolean): Result<string, FooErrorType> {
  if (flag)
    return raiseFailure({
      errorType: FooErrorType.someError,
      message: '<my error message>'
    })

  return raiseSuccess('success-result')
}

const fooResponse = foo(flag)

if (fooResponse.isErrored)
  // Failure Handling
else {
  // success handling
  const result = fooResponse.data
}

Helpers

This library also includes some helper functions to manage success and failures which all can be found in the API documentation.

Dependencies (0)

    Dev Dependencies (15)

    Package Sidebar

    Install

    npm i @mondopower/result-types

    Weekly Downloads

    1,178

    Version

    1.1.551

    License

    Apache-2.0

    Unpacked Size

    23.3 kB

    Total Files

    7

    Last publish

    Collaborators

    • daryl-miller
    • mondo-ci