A package to handle results. Success, errors or partial success as results and not throw errors
# npm
npm install @dancastillo/nothrow
# yarn
yarn install @dancastillo/nothrow
# pnpm
pnpm install @dancastillo/nothrow
import {
type Result,
createSuccessfulResult,
createFailureResult,
createPartialSuccessfulResult,
} from '@dancastillo/nothrow'
Constructs a successful result with no errors
const result = createSuccessfulResult({ success: true })
// result.data = { success: true }
// result.errors = []
Constructs a failure result with no data
const result = createFailureResult([{ error: true }])
// result.data = null
// result.errors = [{ success: false }]
Constructs a result with data and errors
const result = createPartialSuccessReuslt({ success: true }, [{ error: 'Missing input' }, { error: 'Not found' }])
// result.data ={ success: true }
// result.errors = [{ error: 'Missing input' }, { error: 'Not found' }]
Map the data result and errors result to the wanted type. This method takes in two functions as its arguments
const mappedResult = result.mapTo(
(data: DataType) => {
return <MappedDataType>{ id: data.id }
},
(errors: ErrorType) => {
return <MappedErrorType[]>errors.map((err) => {
error: err.message
})
}
)
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 }
})
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
})
})
Returns boolean
Returns boolean
Returns boolean