@dancastillo/nothrow
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

NoThrow

Description

A package to handle errors as a result and not throw errors

Installation

# npm
npm install @dancastillo/nothrow

# yarn
yarn install @dancastillo/nothrow

# pnpm
pnpm install @dancastillo/nothrow

Usage

import { Result } from '@dancastillo/nothrow'

API Documentation

successful

Constructs a successful result with no errors

const result = Result.successful({ success: true })
// result.data = { success: true }
// result.errors = undefined

failure

Constructs a failed result with no data

const result = Result.failure({ success: false })
// result.data = undefined
// result.errors = { success: false }

partialSuccess

Constructs a result with data and errors

const result = Result.partialSuccess({ success: true }, [{ error: 'Missing input' }, { error: 'Not found' }])
// result.data ={ success: true }
// result.errors = [{ error: 'Missing input' }, { error: 'Not found' }]

map

Map the data result and errors result to the wanted type. This method takes in two functions as its arguments

const mappedResult = result.map(
  (data: DataType) => {
    return <MappedDataType>{ id: data.id }
  },
  (errors: ErrorType) => {
    return <MappedErrorType[]>errors.map((err) => {
      error: err.message
    })
  }
)

mapData

Map the data result and errors result to the wanted type. This method takes in two functions as its arguments

const mappedResult = result.mapData((data: DataType) => {
  return <MappedDataType>{ id: data.id }
})

mapErrors

Map the data result and errors result to the wanted type. This method takes in two functions as its arguments

const mappedResult = result.mapErrors((errors: ErrorType) => {
  return <MappedErrorType[]>errors.map((err) => {
    error: err.message
  })
})

isSuccessful

Returns TRUE if the result was successful and no errors

isFailure

Returns TRUE if the result was N error reults and no data

isPartialSuccess

Returns FALSE if the result returned both a data result and error result

hasData

Helper method to determine if the Result has data value set

getData

Helper method to retrieve the Result data value set

hasErrors

Helper method to determine if the Result has error values set

getErrors

Helper method to retrieve the Result error values set

errorCount

Helper method to retrieve the Result error values count

License

MIT license.

Package Sidebar

Install

npm i @dancastillo/nothrow

Weekly Downloads

1

Version

0.0.1

License

MIT

Unpacked Size

36.7 kB

Total Files

39

Last publish

Collaborators

  • dancastillo